Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*; //? Error Here, 'Package javax.swing does not exist' <----- ERROR HERE
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.util.LinkedList;
- import java.util.StringTokenizer;
- public class Employment_Info {
- //? declare attributes
- String name, gender, marital_status, jobClass;
- int age, health;
- double wage;
- //* constructor with param
- public Employment_Info(String name, String gender, String marital_status, String jobClass, int age, int health, double wage) {
- this.marital_status = marital_status;
- this.age = age;
- this.gender = gender;
- this.health = health;
- this.jobClass = jobClass;
- this.name = name;
- this.wage = wage;
- }
- //? mutator
- public void setGender(String gender) {
- this.gender = gender;
- }
- public void setMarital_status(String marital_status) {
- this.marital_status = marital_status;
- }
- public void setName(String name) {
- this.name = name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public void setHealth(int health) {
- this.health = health;
- }
- public void setJobClass(String jobClass) {
- this.jobClass = jobClass;
- }
- public void setWage(double wage) {
- this.wage = wage;
- }
- //? accessor
- public String getGender() {
- return gender;
- }
- public String getMarital_status() {
- return marital_status;
- }
- public String getJobClass() {
- return jobClass;
- }
- public double getWage() {
- return wage;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public int getHealth() {
- return health;
- }
- @Override
- public String toString() {
- return "Employment_Info{" +
- "name='" + name + '\'' +
- ", gender='" + gender + '\'' +
- ", marital_status='" + marital_status + '\'' +
- ", jobClass='" + jobClass + '\'' +
- ", age=" + age +
- ", health=" + health +
- ", wage=" + wage +
- '}';
- }
- public static void main (String[]args)
- {
- try {
- //create reader to read input file
- BufferedReader in = new BufferedReader(new FileReader("C:/Users/deena/IdeaProjects/SWC4133_Lab Exercise 1/src/employeeList.txt"));
- //create linkedlist
- LinkedList<Employment_Info> employeeList = new LinkedList<>();
- String indata = null; //represent a line of data in input file
- Employment_Info employment_info;
- //read file
- while((indata = in.readLine())!=null)
- {
- System.out.println (indata);
- StringTokenizer st = new StringTokenizer(indata, ";");
- String name = st.nextToken();
- String gender = st.nextToken();
- String marital_status = st.nextToken();
- String jobClass = st.nextToken();
- int age = Integer.parseInt(st.nextToken());
- int health = Integer.parseInt(st.nextToken());
- double wage = Double.parseDouble(st.nextToken());
- //create object
- employment_info = new Employment_Info(name, gender, marital_status,jobClass, age, health, wage);
- //add object into employeeList
- employeeList.add (employment_info);
- }
- in.close(); //close reader
- int menu = 0;
- do{
- menu = Integer.parseInt(JOptionPane.showInputDialog("Choose menu: \n 1: Add new data \n 2: Edit data \n 3: Delete data \n 4: Display data"));
- if (menu == 1)
- {
- String name = JOptionPane.showInputDialog("Enter name");
- String gender = JOptionPane.showInputDialog("Enter gender");
- String marital_status = JOptionPane.showInputDialog("Enter marital status");
- String jobClass = JOptionPane.showInputDialog("Enter job class");
- int age = Integer.parseInt(JOptionPane.showInputDialog("Enter age"));
- int health = Integer.parseInt(JOptionPane.showInputDialog("Enter health"));
- double wage = Double.parseDouble(JOptionPane.showInputDialog("Enter wage"));
- //create object
- employment_info = new Employment_Info(name, gender, marital_status, jobClass, age, health, wage);
- //add object into employeeList
- employeeList.add (employment_info);
- }
- else if (menu == 2)
- {
- //ask user which object they want to edit
- //enter employee name
- String name = JOptionPane.showInputDialog("Enter name");
- //find the object
- for (int i = 0; i<employeeList.size(); i++)
- {
- //get the object from employeeList
- employment_info = employeeList.get(i); //use method to retrieve object
- if (employment_info.getName().equalsIgnoreCase(name)) //if true
- {
- int choice = 0;
- do {
- choice = Integer.parseInt(JOptionPane.showInputDialog("Choose menu: \n 1: Edit gender \n 2: Edit Marital Status\n 3: Edit Job Class \n 4: Edit age \n 5: Edit Health \n 6: Edit Wage"));
- if (choice == 1) //edit gender
- {
- String newgender = JOptionPane.showInputDialog("Enter new gender");
- //update object using mutator method
- employment_info.setGender(newgender);
- //update object in employeeList
- employeeList.set(i, employment_info);
- }
- else if (choice == 2) //edit marital status
- {
- String newmarital_status = JOptionPane.showInputDialog("Enter new marital status");
- //update object using mutator method
- employment_info.setMarital_status(newmarital_status);
- //update object in employeeList
- employeeList.set(i, employment_info);
- }
- else if (choice == 3) //edit job class
- {
- String newjobClass = JOptionPane.showInputDialog("Enter new job class");
- employment_info.setJobClass(newjobClass);
- employeeList.set(i, employment_info);
- }
- else if (choice == 4) //edit age
- {
- int newage = Integer.parseInt(JOptionPane.showInputDialog("Enter new age"));
- employment_info.setAge(newage);
- employeeList.set(i, employment_info);
- }
- else if (choice == 5) //edit health
- {
- int newhealth = Integer.parseInt(JOptionPane.showInputDialog("Enter new health"));
- employment_info.setHealth(newhealth);
- employeeList.set(i, employment_info);
- }
- else if (choice == 6) //edit wage
- {
- double newwage = Double.parseDouble(JOptionPane.showInputDialog("Enter new wage"));
- employment_info.setWage(newwage);
- employeeList.set(i, employment_info);
- }
- } while (choice !=7);
- }
- }
- }
- else if (menu == 3)
- {
- //ask user which object they want to delete
- //enter employee name
- String name = JOptionPane.showInputDialog("Enter name");
- //find the object
- for (int i = 0; i<employeeList.size(); i++)
- {
- //get object
- employment_info = employeeList.get(i);
- if (employment_info.getName().equalsIgnoreCase(name))
- {
- employeeList.remove(i); //use method remove
- }
- }
- }
- else if (menu == 4)
- {
- //display data
- System.out.println ("List all employee in Employee List");
- for (int i = 0; i<employeeList.size(); i++)
- {
- System.out.println (employeeList.get(i));
- }
- }
- }
- while (menu != 5);
- }
- catch (Exception ex)
- {
- System.out.println (ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment