-
Notifications
You must be signed in to change notification settings - Fork 0
/
Job.java
175 lines (154 loc) · 6.82 KB
/
Job.java
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.io.*;
import java.nio.file.*;
import java.text.*;
import java.util.*;
public class Job {
// Declare private member variables
private String jobNumber;
private String jobTitle;
private String jobPosterName;
private String jobPosterAddress;
private String jobPostedDate;
private String jobExperienceLevel; // Four job experience levels: Junior, Medium, Senior, Executive
private String jobType; // Four job types: Full-time, Part-time, Internship, Volunteer
private String[] jobRequiredSkills; // a list of skills; each skill is 1 word (e.g. C#) or a maximum of two words (e.g, Software Architecture)
private double jobSalary;
private String jobDescription;
private String fileName = "jobs.txt"; // default file name
// a setter for filename
public void setFileName(String fileName) {
this.fileName = fileName;
}
// constructor
public Job(String number, String title, String posterName, String posterAddress, String date, String experienceLevel, String type, String[] skills, double salary, String description) {
// Initialize member variables
jobNumber = number;
jobTitle = title;
jobPosterName = posterName;
jobPosterAddress = posterAddress;
jobPostedDate = date;
jobExperienceLevel = experienceLevel;
jobType = type;
jobRequiredSkills = skills;
jobSalary = salary;
jobDescription = description;
}
// Define a method to add job information to a TXT file
public boolean addJob() {
// Add the information of a new job to a TXT file
// If the job information meets the defined conditions
// the information should be added to the TXT file and the function should return true.
// If the job information does not meet the conditions,
// the information should not be added to the TXT file and the function should return false.
if (isValidJob()) { // Check if job is valid
// Try to write job details to a text file
try {
// Open the text file in append mode
BufferedWriter writer = new BufferedWriter(new FileWriter("jobs.txt", true));
// Write job details to the text file
writer.write(toString());
writer.newLine();
// Close the text file
writer.close();
// Return true indicating job was added successfully
return true;
} catch (IOException e) {
// Print stack trace in case of exception
e.printStackTrace();
}
}
// Return false indicating job wasn't added
return false;
}
public boolean updateJob() {
// Update the information of a given job in a TXT file
// If the job's new information meets the defined conditions,
// the job information should be updated in the TXT file and the function should return true.
// If the job's new information does not meet the following conditions,
// the job information should not be updated in the TXT file and the function should return false.
if (isValidJob()) { // Check if job is valid before updating
// Read existing lines from the text file
try {
List<String> lines = Files.readAllLines(Paths.get("jobs.txt"));
List<String> updatedLines = new ArrayList<>();
// Loop through each line
for (String line : lines) {
if (line.startsWith(jobNumber)) {
// Replace old line with new job details
updatedLines.add(toString());
} else {
// Keep old line
updatedLines.add(line);
}
}
// Write updated lines to the file
BufferedWriter writer = new BufferedWriter(new FileWriter("jobs.txt"));
for (String updatedLine : updatedLines) {
writer.write(updatedLine);
writer.newLine();
}
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
// Checks to ensure all aspects of a job are valid
private boolean isValidJob() {
return isValidJobNumber() && isValidJobPostedDate() && isValidJobPosterAddress() && isValidJobSalary() && isValidJobType() && isValidJobRequiredSkills();
}
// Validate job number using regex
private boolean isValidJobNumber() {
return jobNumber.matches("^[1-5]{5}[A-Z]{3}\\W$");
}
// Validate posted date using SimpleDateFormat
private boolean isValidJobPostedDate() {
try {
new SimpleDateFormat("yyyy-MM-dd").parse(jobPostedDate);
return true;
} catch (ParseException e) {
return false;
}
}
// Validate job poster address by checking number of comma separated parts
private boolean isValidJobPosterAddress() {
String[] parts = jobPosterAddress.split(", ");
return parts.length == 3;
}
// Validate salary based on job experience level
private boolean isValidJobSalary() {
if (jobExperienceLevel.equals("Senior") || jobExperienceLevel.equals("Executive")) {
return jobSalary >= 100000;
} else if (jobExperienceLevel.equals("Junior")) {
return jobSalary >= 40000 && jobSalary <= 70000;
}
return true;
}
// Validate job type based on job experience level
private boolean isValidJobType() {
if (jobType.equals("Part-time")) {
return !jobExperienceLevel.equals("Senior") && !jobExperienceLevel.equals("Executive");
}
return true;
}
// Validate skills: number of skills and words per skill
private boolean isValidJobRequiredSkills() {
if (jobRequiredSkills.length >= 1 && jobRequiredSkills.length <= 3) {
for (String skill : jobRequiredSkills) {
if (skill.split(" ").length > 2) {
return false;
}
}
return true;
}
return false;
}
// Override toString to provide custom string representation of Job
@Override
public String toString() {
String skills = String.join(", ", jobRequiredSkills);
return jobNumber + "," + jobTitle + "," + jobPosterName + "," + jobPosterAddress + "," + jobPostedDate + "," + jobExperienceLevel + "," + jobType + "," + skills + "," + jobSalary + "," + jobDescription;
}
}