-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteToTextFile.java
More file actions
132 lines (91 loc) · 5.44 KB
/
WriteToTextFile.java
File metadata and controls
132 lines (91 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
/**
*
* @author 565174
*/
public class WriteToTextFile {
//ATTRIBUTES
private BufferedWriter bufferedWriter;
private File savedEmployeesFile;
//CONSTRUCTOR
public WriteToTextFile()
throws IOException {
savedEmployeesFile = new File("src/readMe.txt");
bufferedWriter = null;
}
//METHODS
public void writeEmployeesToFile(MyHashTable employeeHashTable) //Also writes employee Locations
throws IOException{
//Deletes readMe.txt so that previously recorded employees are deleted. All necessary data is stored in the hash table.
//This has to happen before bufferedWriter is initialized! For some reason, having bufferedWriter OPEN the file makes it invulnerable!
savedEmployeesFile.delete();
//Setup BufferedWriter, setup a new one each time because it closes at the end of the method
bufferedWriter = new BufferedWriter(new FileWriter(savedEmployeesFile, true));
//Write Number of Entities
bufferedWriter.write(Integer.toString(employeeHashTable.numberOfEntities) + "%");
bufferedWriter.newLine();
//For Each Bucket:
for(int currentBucket = 0; currentBucket < employeeHashTable.buckets.length; currentBucket++){
//For Each employee in the bucket
for (int currentEmployee = 0; currentEmployee < employeeHashTable.buckets[currentBucket].size(); currentEmployee++){
//Create write string and fill with all info of employeeToPrint
EmployeeInfo employeeToPrint = employeeHashTable.buckets[currentBucket].get(currentEmployee);
String writeString;
//Prepare generic employee info
String empNum = String.valueOf(employeeToPrint.getEmployeeNumber());
String firstName = employeeToPrint.getFirstName();
String lastName = employeeToPrint.getLastName();
String deductionRate = Double.toString(employeeToPrint.getDeductionRate());
String gender = employeeToPrint.getGender();
String workLocation = employeeToPrint.getWorkLocation();
//Prepare PTE or FTE specific information
if (employeeToPrint instanceof FTEmp){
String yearlySalary = Double.toString(((FTEmp) employeeToPrint).getYearlySalary());
//Create writeString
writeString = "F" + "%" + empNum + "%" + firstName + "%" + lastName + "%" + deductionRate + "%" + gender + "%" + workLocation + "%" + yearlySalary + "%";
}
else{//employeeToPrint is an instance of PTE
String hourlyWage = Double.toString(((PTEmp) employeeToPrint).getHourlyWage());
String hoursPerWeek = Double.toString(((PTEmp) employeeToPrint).getHoursPerWeek());
String weeksPerYear = Double.toString(((PTEmp) employeeToPrint).getWeeksPerYear());
//Create writeString
writeString = "P" + "%" + empNum + "%" + firstName + "%" + lastName + "%" + deductionRate + "%" + gender + "%" + workLocation + "%" + hourlyWage + "%" + hoursPerWeek + "%" + weeksPerYear + "%";
}
//Write writeString to file
bufferedWriter.write(writeString);
bufferedWriter.newLine();
}
}
//Write Num work locations
//Write a % before this number to separate the number from the newline text
bufferedWriter.write("%" + Integer.toString(MainJFrame.workLocationArray.length) + "%");
bufferedWriter.newLine();
//Write work locations
for(int location = 0; location < MainJFrame.workLocationArray.length; location++){
//Write a % before work location to separate the work location from the newline text
String writeString = "%" + MainJFrame.workLocationArray[location] + "%";
bufferedWriter.write(writeString);
bufferedWriter.newLine();
}
//Looks like you have to close this guy to get the hash table actually written to the thing. This also means bufferedWriter must
//be instantialized each time this method runs!
bufferedWriter.close();
}
public void writeBlankEmployeesAndLocationsToFile()//File must be instantiated
throws IOException{
savedEmployeesFile.delete();
bufferedWriter = new BufferedWriter(new FileWriter(savedEmployeesFile, true));
//Write 0% nl %0% (No employees, no locations)
bufferedWriter.write("0%");
bufferedWriter.newLine();
bufferedWriter.write("%1%");
bufferedWriter.newLine();
bufferedWriter.write("%Earth%");
bufferedWriter.close();
}
}