[英]How can I ask a user if they want to modify information and then apply the change?
This is a basic Java practice question that I have seen, but I wanted to step it up a notch and include functionalities that ask the user whether or not they want to modify an employee's information and if so which one, then apply the requested modifications to the employee and display the updated employees information one more time. 我已经看到了这是一个基本的Java练习问题,但我想提高一个级别,并包括一些功能,这些功能询问用户是否要修改员工的信息,如果要修改,则将其应用于并再次显示更新的员工信息。
Here is my code thus far: 到目前为止,这是我的代码:
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
and the EmployeeTest class 和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
After displaying the employee information I would like to prompt the user to choose whether or not to make a change to an employee's salary. 显示员工信息后,我想提示用户选择是否更改员工的工资。 If so, then which employee.
如果是这样,那么是哪个雇员。 After the change is made I would like to display the the modified results.
进行更改后,我想显示修改后的结果。 What would be the easiest way to handle this?
处理此问题的最简单方法是什么?
After some tweaking to my nested-ifs statements I figured out a way to achieve what I was looking for. 在对nested-ifs语句进行了一些调整之后,我找到了一种实现所需功能的方法。 I can't say that I was looking for answers to homework as I am no longer in school.
我不能说我一直在寻找家庭作业的答案,因为我不再上学了。 I was simply looking for an easier way to achieve my goal by taking the path of least resistance...something that would have made coding easier and more concise.
我只是在寻找一种最简单的方法来实现目标,那就是走最小的阻力之路……这会使编码更容易,更简洁。 Although my answer is slightly bulkier than what I would have liked, it does still accomplish the task at hand.
尽管我的回答比我想要的要大一些,但它仍然可以完成手头的任务。 I will be working more to improve the application, but here is what I came up with:
我将做更多工作来改善应用程序,但是这是我想出的:
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
public class "Employee" did not change for post above. 上面的帖子未更改公共类“雇员”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.