【发布时间】:2020-05-06 16:55:22
【问题描述】:
我的程序真的需要帮助。我是 Arraylist 的新手,我不知道如何通过 ID 号搜索和显示对象。
所以程序有4个类:Main、Person(父类)Student和Employee(都是Person的子类)
现在我的主类有一个菜单,可以执行以下操作:
- 添加学生(每个学生的 ID 必须是唯一的)
- 添加员工(每个员工的 ID 必须唯一)
- 搜索学生(按学生 ID 然后显示)
- 搜索员工(按员工编号然后显示)
- 显示全部(显示所有螺柱和员工)
- 退出
我之前已经通过数组搜索和显示来完成此操作,但现在我们必须使用 Arraylist,它更好,但我是新手。
非常感谢任何帮助和建议。谢谢!
这是我的代码:
主类:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Student> stud = new ArrayList<Student>();
ArrayList<Employee> emp = new ArrayList<Employee>();
while (true) {
int select;
System.out.println("Menu");
System.out.println("1. Add Student");
System.out.println("2. Add Employee");
System.out.println("3. Search Student");
System.out.println("4. Search Employee");
System.out.println("5. Display All");
System.out.println("6. Quit");
select = sc.nextInt();
switch (select) {
case 1:
addStud(stud);
break;
case 2:
addEmp(emp);
break;
case 3:
srchStud();
break;
case 4:
srchEmp();
case 5:
displayAll(stud, emp);
break;
case 6:
return;
default:
System.out.println("Invalid Option");
}
}
}
public static void addStud(ArrayList<Student> stud) {
String name, address, course;
int age, cNum, studNum, year;
int addMore;
System.out.println("ADD STUDENT");
do {
System.out.println("Student No: ");
studNum = sc.nextInt();
sc.nextLine();
for(int x = 0; x < stud.size(); x++){ // Please help fix, this accepts another ID if already existing to prevent duplicate
if(studNum == stud[x].getStudNum()) { // this code works with array of object but not on arraylist,
System.out.println("The Student ID: " +studNum+ " already exist.\nEnter New Student ID: ");
studNum = sc.nextInt();
sc.nextLine();
x = -1;
}
}
System.out.println("Name: ");
name = sc.nextLine();
System.out.println("Age: ");
age = sc.nextInt();
sc.nextLine();
System.out.println("Address: ");
address = sc.nextLine();
System.out.println("Contact No: ");
cNum = sc.nextInt();
sc.nextLine();
System.out.println("Course: ");
course = sc.nextLine();
System.out.println("Year: ");
year = sc.nextInt();
sc.nextLine();
stud.add(new Student(name, address, age, cNum, studNum, year, course));
System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);
}
public static void addEmp(ArrayList<Employee> emp) {
String name, address, position;
int age, cNum, empNum;
double salary;
int addMore;
System.out.println("ADD Employee");
do {
System.out.println("Employee No: ");
empNum = sc.nextInt();
sc.nextLine();
for(int x = 0; x < emp.size(); x++){
if(empNum == emp[x].getEmpNum()) {
System.out.println("The Employee ID: " +empNum+ " already exist.\nEnter New Student ID: ");
empNum = sc.nextInt();
sc.nextLine();
x = -1;
}
}
System.out.println("Name: ");
name = sc.nextLine();
System.out.println("Age: ");
age = sc.nextInt();
sc.nextLine();
System.out.println("Address: ");
address = sc.nextLine();
System.out.println("Contact No: ");
cNum = sc.nextInt();
sc.nextLine();
System.out.println("Position: ");
position = sc.nextLine();
System.out.println("Salary: ");
salary = sc.nextInt();
sc.nextLine();
emp.add(new Employee(name, address, age, cNum, empNum, salary, position));
System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);
}
public static void displayAll(ArrayList<Student> stud, ArrayList<Employee> emp){ // Definitely not working with Arraylist
System.out.println("Student ID\tStudent Name\tStudent Course\tStudent Year");
for (int x = 0; x < stud.size(); x++) {
System.out.println(stud[x].getStudNum() + "\t\t\t\t" + stud[x].getName() + "\t\t\t\t" + stud[x].getCourse() + "\t\t\t\t" + stud[x].getYear());
}
}
}
人员类别:
public class Person {
private String name, address;
private int age, cNum;
public Person() {
}
public Person(String name, String address, int age, int cNum) {
this.name = name;
this.address = address;
this.age = age;
this.cNum = cNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getcNum() {
return cNum;
}
public void setcNum(int cNum) {
this.cNum = cNum;
}
}
学生班:
public class Student extends Person{
private int studNum, year;
private String course;
public Student(String name, String address, int age, int cNum, int studNum, int year, String course) {
super(name, address, age, cNum);
this.studNum = studNum;
this.year = year;
this.course = course;
}
public int getStudNum() {
return studNum;
}
public void setStudNum(int studNum) {
this.studNum = studNum;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
员工等级:
public class Employee extends Person {
private int empNum;
private double salary;
private String position;
public Employee(String name, String address, int age, int cNum, int empNum, double salary, String position) {
super(name, address, age, cNum);
this.empNum = empNum;
this.salary = salary;
this.position = position;
}
public int getEmpNum() {
return empNum;
}
public void setEmpNum(int empNum) {
this.empNum = empNum;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
【问题讨论】:
标签: java arrays loops oop arraylist