-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFromTextFile.java
More file actions
138 lines (99 loc) · 4.76 KB
/
ReadFromTextFile.java
File metadata and controls
138 lines (99 loc) · 4.76 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
133
134
135
136
137
138
/*
* 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 John
*/
public class ReadFromTextFile {
//ATTRIBUTES
public BufferedReader bufferedReader;
public File savedEmployeesFile;
//CONSTRUCTOR
public ReadFromTextFile() //Instantiates bufferedReader! Remember to close using closeBufferedReader() when using!!!!
throws IOException {
savedEmployeesFile = new File("src/readMe.txt");
bufferedReader = new BufferedReader(new FileReader(savedEmployeesFile));
}
//METHODS
//"P" or "F"%empNum%"fName"%"lName"%dedRate%yearlySalary OR HourlyWage%Hours/week%wk/yr
public void buildHashTableFromTextFile(MyHashTable employeeHashTable) //Also builds work location string array
throws IOException, NumberFormatException{
//Make employee hash
String numberOfEntitiesString = readOneSection();
int numberOfEntities = Integer.parseInt(numberOfEntitiesString);
for(int employee = 1; employee <= numberOfEntities; employee++){
EmployeeInfo addEmployee = readDataForOneEmployee();
employeeHashTable.addToTable(addEmployee);
}
//Make String array of work locations
//Read an extra one section to read the hidden "new line" text
readOneSection();
int numberOfWorkLocations = Integer.parseInt(readOneSection());
//make new array of work locations strings
MainJFrame.workLocationArray = new String[numberOfWorkLocations];
for(int workLocation = 0; workLocation < numberOfWorkLocations; workLocation++){
//Empty readonesection to get rid of newline text
readOneSection();
MainJFrame.workLocationArray[workLocation] = readOneSection();
}
}
public String readOneCharacter()
throws IOException{
String character = Character.toString((char)bufferedReader.read());
return character;
}
public String readOneSection()//Reads the last % sign!!! So skip is not necessary
throws IOException{
String characterJustRead = readOneCharacter();
String readSection = "";
do{
if(characterJustRead.equals("\uffff") == true){
return "This text will throw numberFormatException which will trigger a system reset upon the newline / blank string input:) ;)))))";
}
readSection += characterJustRead;
characterJustRead = readOneCharacter();
}while(characterJustRead.equals("%") == false);
return readSection;
}
public EmployeeInfo readDataForOneEmployee()
throws IOException{
//Declare Employee
EmployeeInfo employee;
//Read first Character to idenfity if PTE or FTE
String partTimeOrFullTime = readOneSection();
//Read Employee Number
int employeeNumber = Integer.parseInt(readOneSection());
//Read First Name
String employeeFirstName = readOneSection();
//Read last Name
String employeeLastName = readOneSection();
//Read deduction rate
double employeeDeductionRate = Double.parseDouble(readOneSection());
//Read gender
String gender = readOneSection();
//Read Work Location
String workLocation = readOneSection();
//Read remaining data depending on PTE or FTE
if(partTimeOrFullTime.equals("\r\nP") == true){//If Employee is Part Time
double hW = Double.parseDouble(readOneSection());
double hPW = Double.parseDouble(readOneSection());
double wPY = Double.parseDouble(readOneSection());
//Create Employee
employee = new PTEmp(employeeNumber, employeeFirstName, employeeLastName, employeeDeductionRate, gender, workLocation, hW,hPW,wPY);
}
else{//If employee is full time
double yearlySalary = Double.parseDouble(readOneSection());
//Create Employee
employee = new FTEmp(employeeNumber, employeeFirstName, employeeLastName, employeeDeductionRate, gender, workLocation, yearlySalary);
}
return employee;
}
public void closeBufferedReader()
throws IOException{
bufferedReader.close();
}
}