我一直在尝试创建一个小程序,提示用户输入一个雇员姓名和工资将其添加到arrayList,然后在屏幕上显示选项(例如0:退出,1:添加,2:显示),读取输入然后继续根据输入。显示只是(例如姓氏:史密斯薪水:14000英镑。只需要一些帮助指出我正确的方向。我目前有3个班级员工,员工名单和员工测试
此类提示用户输入。
import java.util.Scanner;
public class Employee {
private String Last_Name;
private int Salary;
public Employee(){
Scanner inputValues = new Scanner(System.in);
System.out.print("Enter employee last name: ");
Last_Name = inputValues.next();
System.out.print("Enter Employee Salary: " + "£");
Salary = inputValues.nextInt();
}
public void Display(){
System.out.printf("Name: " + Last_Name + " " + "Salary: " + Salary);
System.out.println("\n");
}
}
这个班级应该是将员工添加到一个arraylist但我不确定我是否正确地做了。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class EmployeeList extends Employee{
private List <Employee> employee = new ArrayList<Employee>();
public EmployeeList(Employee person) {
employee.add(person);
}
public void DisplayEmployees(){
System.out.println("Employee:" + employee.size());
displayList(employee);
}
public static void displayList(List employee) {
}
}
这是主要方法
的地方import java.util.Scanner;
public class EmployeeTest {
public static void main(String[] args) {
Employee employee = new Employee();
employee.Display();
EmployeeList empList = new EmployeeList(employee);
empList.DisplayEmployees();
Scanner scanner = new Scanner(System.in);
System.out.println("0: quit, 1: add, 2: display");
String employees = scanner.next();
/* if (employees.equals("1")){
//not sure how to go back to displaying the user prompts
break;
} */
}
}
答案 0 :(得分:3)
我能想到的一些提示:
EmployeeList
不应展开Employee
。 OOP的主要规则是,如果B 是 A,则A类扩展B类。这显然不是这里的情况 - employeeList不是员工,而是员工列表(在我的请注意,您不需要上课,只需List<Employee>
)我将逻辑与数据分开。意思是 - Employee
类应该只保存员工的数据,而不是处理扫描和获取用户的输入。构造函数在我看来应该很简单,例如:
public Employee(String name, int salary) {
this.name = name;
this.salary = salary;
}
获取数据的逻辑应该在此类之外,在EmployeeHandler
或主要本身。既然你把它放在员工里面,当你的一些逻辑存在于员工中而某些逻辑存在于主要人员中时,你就会遇到麻烦。
toString
课程中覆盖Employee
完成打印)答案 1 :(得分:1)
public class Employee {
private String Last_Name;
private int Salary;
public Employee(){
public String getLName(){
return Last_Name;
}
public void setLName(){
this.Last_Name = Last_Name;
}
public int getSalary(){
return salary;
}
public void setSalary(){
this.salary = salary;
}
}
}
然后在您的main方法中,您可以创建员工对象。
public static void main(String[] args){
Employee employee = new Employee();
Scanner scanner = new Scanner(System.in);
employee.setLName = scanner.next();
employee.setSalary = scanner.nextInt();
}
如果我是你,我只会让一个arraylist来容纳所有员工。我会提示x次输入选项并添加到arraylist的末尾。 arraylist将被创建为
ArrayList<Employee> employeeList = new ArrayList<Employee>();
添加到它使用添加
employeeList.add(employee);
这应该可以让你入门
编辑:
OOPS,犯了几个错误。使用以下内容编辑。请注意,它是employee.setLastName(value),因为方法setLastName是employee类的一部分,并且必须传递一个值,因为我们已在employee类中定义了该值。
Employee employee = new Employee();
Scanner scanner = new Scanner(System.in);
String tempName = scanner.next();
int tempSalary = scanner.nextInt();
employee.setLastName(tempName);
employee.setSalary(tempSalary);
编辑2:
尝试按如下方式打印arraylists。没有测试它。让我知道它是如何工作的。
for (int i = 0; i< employeelist.size(); i++){
Employee temp = values.get(i);
System.out.println("Last Name: " + temp.getLname() + "Salary: " + temp.getSalary());
}
答案 2 :(得分:0)
我修改了我的员工类,看起来像这样:
public class Employee {
public String lastName;
private int salary;
public Employee(){
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString(){
return "Employee Last Name: " + lastName + "\n" + "Employee Salary: " + salary;
}
}
我修改了EmployeeTest类:
这是我的EmployeeTest课程:
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeTest {
static ArrayList<Employee> employeeList = new ArrayList<Employee>();
public static void main(String[] args) {
for(int i=0;i<5;i++){
addEmployees(employeeList);
}
System.out.println(employeeList.toArray());
}
public static void addEmployees(ArrayList<Employee> employeeList){
Scanner scanner = new Scanner(System.in);
System.out.println("0: quit, 1: add, 2: display");
String options = scanner.next();
if(options.equals("1")){
System.out.print("Enter employee last name: ");
String lastname = scanner.next();
System.out.print("Enter Employee Salary: " + "£");
int salary = scanner.nextInt();
Employee employee = new Employee();
EmployeeList.add(employee);
}
else if(options.equals("2")){
for (Employee employee : employeeList){
/*System.out.println("Name: " + employee.getLastName() + ", " + "Salary: " + employee.getSalary());*/
System.out.println(employee);
}
}
else{
System.exit(0);
}
}
}
然而,当我按2作为我的选项时,它不会显示我的arraylist中的内容。
我读过一个toString方法用于获取这些细节,以便我可以将它们打印到屏幕上。并且使用此for循环获取列表中的每个项目以显示它们。我在这里错过了什么吗?为此拖延道歉,我只想让它起作用。