2015-05-27 92 views
0

善待,學習。JAVA - 修改員工

這些都是我的類:員工,salaryworker,hourlyworker,PayrollTest

我需要能夠修改現有的員工,但我需要它能夠分辨出來,如果它每小時或工資工人因爲他們有不同的投入。我可以通過它的id找到員工,但不知道如何正確修改它。我迷失在如何做到這一點。

import java.util.*; 
import java.lang.*; 

public class PayrollTest { 

    //Array Lists for Hourly and Salary Workers. 
    public static hourlyworker hourlyEmp = new hourlyworker(null, 0, 0.0, 0.0, 0.0, 0, 0, 0); 
    public static ArrayList<hourlyworker> hourlyList = new ArrayList<hourlyworker>(); 
    //(String name, int dependents, double annualSalary, int month, int day, int year) { 

    public static salaryworker salaryEmp = new salaryworker(null, 0, 0.0, 0, 0, 0); 
    public static ArrayList<salaryworker> salaryList = new ArrayList<salaryworker>(); 

    //Arraylist Initializing 
    public static ArrayList emps = new ArrayList(); //<Employee> 

    //Initializing scanner for user input 
    public static Scanner scan = new Scanner(System.in); 

    //Fields needed for PayrollTest 
    private static char choice; 
    private static char typeChoice; 
    private static boolean switcher = true; 

    @SuppressWarnings("resource") 
    public static void main(String[] args) { 


     menuSelection(); 
     System.out.println(); 


    } 

    @SuppressWarnings("unchecked") 
    private static void loadHourlyEmployee() { 

     System.out.println("\nEnter full name, number of dependents, hourly rate, regular hours worked, overtime worked, and date hired (yyyy mm dd)"); 
     System.out.println("[one field per line, except date hired]: "); 
     hourlyEmp = new hourlyworker(null, 0, 0.0, 0.0, 0.0, 0, 0, 0); 

     hourlyEmp.setName(scan.nextLine()); 
     hourlyEmp.setDependents(scan.nextInt()); 
     hourlyEmp.setPayrate(scan.nextDouble()); 
     hourlyEmp.setHours(scan.nextDouble()); 
     hourlyEmp.setOvertimeHours(scan.nextDouble()); 
     hourlyEmp.setHireDay(scan.nextInt()); 
     emps.add(hourlyEmp); 

     System.out.println("\nYou entered an employee type for non-exempt: hourly."); 


    } 

    @SuppressWarnings("unchecked") 
    private static void loadSalaryEmployee() { 

     System.out.println("\nEnter full name, number of dependents, annual salary, and date hired (yyyy mm dd)"); 
     System.out.println("[one field per line, except date hired]: "); 
     salaryEmp = new salaryworker(null, 0, 0.0, 0, 0, 0); 

     salaryEmp.setName(scan.nextLine()); 
     salaryEmp.setDependents(scan.nextInt()); 
     salaryEmp.setAnnualSalary(scan.nextDouble()); 
     salaryEmp.setHireDay(scan.nextInt()); 
     emps.add(salaryEmp); 

     System.out.println("\nYou entered an employee type for exempt: salary."); 


    } 

    private static void menuSelection() { 
     do { 
      System.out.println("\n\n\tEmployee Database"); 
      System.out.println("\nEmployee Info Menu"); 
      System.out.println("\tEnter L to (L)oad Employee info"); 
      System.out.println("\tEnter M to (M)odify Employee info"); 
      System.out.println("\tEnter P to (P)rint Employee info"); 
      System.out.println("\tEnter Q to quit"); 
      System.out.print("Please enter your choice: "); 
      choice = scan.nextLine().toUpperCase().charAt(0); 

      //Choice validation 
      while ((choice != 'L') && (choice != 'M') && (choice != 'P') && (choice != 'Q')) { 

       System.out.println("Invalid choice! Please select (L)oad, (M)odify, (P)rint, or (Q)uit: "); 
       choice = scan.nextLine().toUpperCase().charAt(0); 
      } 



      //Switch Statement 
      switch (choice) { 
      case 'L' : 

       System.out.println("What is the employee type? S = salary H = hourly "); 
       System.out.print("\n[Please enter letter] : "); 
       typeChoice = scan.nextLine().toUpperCase().charAt(0); 

       //typeChoice validation 
       while ((typeChoice != 'S') && (typeChoice != 'H')) { 
        System.out.println("Invalid choice! Please select (S)alary, (H)ourly: "); 
        typeChoice = scan.nextLine().toUpperCase().charAt(0); 
       } 

       if (typeChoice == 'H') { 
        loadHourlyEmployee();} 

       else if (typeChoice == 'S') { 
        loadSalaryEmployee(); 
       } 

       break; 

      case 'M' : 

       modifyEmployee(); 


       break; 

      case 'P' : 
       printEmployees(); 
       break; 

      case 'Q' : 
       switcher = false; 
       System.out.println("\nProgram Terminated."); 
       break; 

      } 
     } 

     while (switcher != false); 


    } 



    private static void printEmployees() { 
     System.out.println("\nThere are " + emps.size() + " people in this database.\n"); 

     for (int i = 0; i < emps.size(); i++) { 
      System.out.println(emps.get(i)); 
     } 

     System.out.println("\nTotal = " + emps.size()); 
    } 
    private static void modifyEmployee() { 

     System.out.print("Employee id#? "); 
     int findID = scan.nextInt(); 

     boolean found = false; 
     int index = 0; 
     boolean deleteMod = false; 
     int index1 = 0; 

     while (index < emps.size() && !found) { 
      if (emps.get(index) != null) { 
       found = true; 
      } 

      deleteMod = true; 

      System.out.println("\nCurrent Info: "); 


     } 
    } 
} 
+1

你真的希望我們讀這麼多的代碼?請提供[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve) – Lrrr

+0

同時告訴我們你是否想要這樣做? – Lrrr

回答

2

使用instanceof來確定它是否是您想要的類。

if (emps.get(index) instanceof hourlyworker) { 
    // code for hourlyworker 
} else { 
    // code for salaryworker 
} 
1

在java中的instanceof運算符將幫助你在這裏。當你處於學習模式時,我會更喜歡你瞭解它,而不是我在這裏解釋。