2015-09-14 16 views
0

這是我見過的一個基本的Java實踐問題,但我希望將它提升一個檔次,幷包含一些功能,這些功能會詢問用戶是否要修改員工的信息,如果是這樣,那麼應用所請求的修改員工並再次顯示更新的員工信息。如何詢問用戶是否想修改信息並應用更改?

這裏是我的代碼至今:

public class Employee 
{ 
    private String firstName; 
    private String lastName; 
    private double monthlySalary; 
    private String decision; 

    // constructor to initialize first name, last name and monthly salary 
    public Employee(String first, String last, double salary) 
    { 
    firstName = first; 
    lastName = last; 

     if (salary >= 0.0) // determine whether salary is positive 
      monthlySalary = salary; 
    } // end three-argument Employee constructor 

    // set Employee's first name 
    public void setFirstName(String first) 
    { 
     firstName = first; 
    } // end method setFirstName 

    // get Employee's first name 
    public String getFirstName() 
    { 
     return firstName; 
    } // end method getFirstName 

    // set Employee's last name 
    public void setLastName(String last) 
    { 
     lastName = last; 
    } // end method setLastName 

    // get Employee's last name 
    public String getLastName() 
    { 
     return lastName; 
    } // end method getLastName 

    // set Employee's monthly salary 
    public void setMonthlySalary(double salary) 
    { 
     if (salary >= 0.0) // determine whether salary is positive 
      monthlySalary = salary; 
    } // end method setMonthlySalary 

    // get Employee's monthly salary 
    public double getMonthlySalary() 
    { 
     return monthlySalary; 
    } // end method getMonthlySalary 

    // set Employee's new monthly salary 
    public void setNewMonthlySalary(double salary) 
    { 
     monthlySalary = salary; 
    } // end method setMonthlySalary 

    // get Employee's new monthly salary 
    public double getNewMonthlySalary() 
    { 
     return monthlySalary; 
    } // end method getMonthlySalary 

} // end class Employee 

和EmployeeTest類

import java.util.Scanner; 

public class EmployeeTest 
{ 

    public static void main(String args[]) 
    { 
     Employee employee1 = new Employee("Bo", "Jackson", 8875.00); 
     Employee employee2 = new Employee("Cam", "Newton", 13150.75); 
     // create Scanner to obtain input from command window 
     Scanner input = new Scanner(System.in); 

     // display employees 
     System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n", 
       employee1.getFirstName(), employee1.getLastName(), 
       12 * employee1.getMonthlySalary()); 
     System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n", 
       employee2.getFirstName(), employee2.getLastName(), 
       12 * employee2.getMonthlySalary()); 

     // enter new employee salaries 
     System.out.println("Enter " + employee1.getFirstName() + "'s new salary:"); 
     double newSalary1 = input.nextDouble(); 
     employee1.setNewMonthlySalary(newSalary1); 
     System.out.println("Enter " + employee2.getFirstName() + "'s new salary:"); 
     double newSalary2 = input.nextDouble(); 
     employee2.setNewMonthlySalary(newSalary2); 

     // display employees with new yearly salary 
     System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n", 
       employee1.getFirstName(), employee1.getLastName(), 
       12 * newSalary1); 
     System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n", 
       employee2.getFirstName(), employee2.getLastName(), 
       12 * newSalary2); 
    } // end main 

} // end class EmployeeTest 

顯示僱員的信息後,我想提示用戶選擇是否要做出改變到員工的工資。如果是這樣,那麼哪個員工。更改完成後,我想顯示修改後的結果。處理這個問題最簡單的方法是什麼?

+3

考慮使用諸如「displayEmployee」,「newEmployee」,「updateEmployee」之類的方法,這可能會幫助您找出答案。 –

+0

我真的想知道如果我應該使用if語句或switch語句嗎?真的不需要命名方法的幫助。如果我能得到一個if語句的例子,它將完成四個步驟:1)提示用戶輸入yes或no; 2)如果是,則提示用戶選擇員工一或員工二; 3)然後,一旦僱員被選中,他們可以修改工資; 4)然後,顯示新的工資。 – NOSDUH

+0

在我看來,你想知道如果有人可以做你的功課。你應該「嘗試一些東西」(假設使用'if',因爲*規則* 2選擇=>'if',更多選擇=>'switch'),然後詢問是否遇到任何特定問題。祝你好運。 –

回答

0

在對我的嵌套ifs語句進行了一些調整之後,我想出了一種實現我所尋找的方法。我不能說我正在尋找家庭作業的答案,因爲我已經不在學校了。我只是尋找一種更簡單的方法,通過採取阻力最小的路徑來實現我的目標......這會使編碼更容易,更簡潔。雖然我的答案比我想要的稍大一點,但它仍然可以完成手頭的任務。我會更努力地改進應用程序,但這裏是我想出的:

import java.util.Scanner; 
import javax.swing.JOptionPane; 
import javax.swing.JDialog; 

public class EmployeeTest 
{ 

    public static void main(String args[]) 
    { 
     // create Scanner to obtain input from command window 
     Scanner input = new Scanner(System.in); 
     Employee employee1 = new Employee("Bo", "Jackson", 8875.00); 
     Employee employee2 = new Employee("Cam", "Newton", 13150.75); 

     // display employees 
     System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n", 
       employee1.getFirstName(), employee1.getLastName(), 
       12 * employee1.getMonthlySalary()); 
     System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n", 
       employee2.getFirstName(), employee2.getLastName(), 
       12 * employee2.getMonthlySalary()); 

     JDialog.setDefaultLookAndFeelDecorated(true); 
     int response1 = JOptionPane.showConfirmDialog(null, "Do you wnat to change an employee's salary?", 
       "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 

      if (response1 == JOptionPane.NO_OPTION)// if NO is clicked 
      { 
       System.out.println("See you next time"); 
      } else if (response1 == JOptionPane.YES_OPTION)// if YES is clicked 
      { 
       // option to change the salary for employee 1 
       int response2 = JOptionPane.showConfirmDialog(null, "Would you like to change " + employee1.getFirstName() + " " + employee1.getLastName() + "'s salary:", 
         "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
       if (response2 == JOptionPane.NO_OPTION)// if NO is clicked 
       { 
        // option to change the salary for employee 2 
        int response3 = JOptionPane.showConfirmDialog(null, "Would you like to change " + employee2.getFirstName() + " " + employee2.getLastName() + "'s salary:", 
          "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); 
        if (response3 == JOptionPane.NO_OPTION) 
          { 
           System.out.println("See you next time"); 
          } else if (response3 == JOptionPane.YES_OPTION)// if YES is clicked 
          { 
           // enter new salary for employee 2 
           System.out.println("Enter " + employee2.getFirstName() + " " + employee2.getLastName() + "'s new salary:"); 
           double newSalary2 = input.nextDouble(); 
           employee2.setNewMonthlySalary(newSalary2); 

           // display unchanged salary for employee 1 
           System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n", 
             employee1.getFirstName(), employee1.getLastName(), 
             12 * employee1.getMonthlySalary()); 

           // display new yearly salary for employee 2 
           System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n", 
             employee2.getFirstName(), employee2.getLastName(), 
             12 * newSalary2); 
          } else if (response3 == JOptionPane.CLOSED_OPTION)// if JOPTIONPANE is closed 
          { 
           System.out.println("JOptionPane closed"); 
          }// END RESPONSE 3 
       } else if (response2 == JOptionPane.YES_OPTION)// if YES is clicked 
       { 
        // enter new salary for employee 1 
        System.out.println("Enter " + employee1.getFirstName() + " " + employee1.getLastName() + "'s new salary:"); 
        double newSalary1 = input.nextDouble(); 
        employee1.setNewMonthlySalary(newSalary1); 

        // display new yearly salary for employee 1 
        System.out.printf("Employee 1: %s %s; Yearly Salary: %.2f\n", 
          employee1.getFirstName(), employee1.getLastName(), 
          12 * newSalary1); 
        // display unchanged salary for employee 2 
        System.out.printf("Employee 2: %s %s; Yearly Salary: %.2f\n", 
          employee2.getFirstName(), employee2.getLastName(), 
          12 * employee2.getMonthlySalary()); 
       } else if (response2 == JOptionPane.CLOSED_OPTION)// if JOPTIONPANE is closed 
       { 
        System.out.println("JOptionPane closed"); 
       }// END RESPONSE 2 
       { 

       } 

      } else if (response1 == JOptionPane.CLOSED_OPTION)// if JOPTIONPANE is clicked 
      { 
       System.out.println("JOptionPane closed"); 
      }// END RESPONSE 1 

    } // end main 

} // end class EmployeeTest 

公共類「員工」沒有改變以上職位。

相關問題