【发布时间】:2021-06-20 17:51:27
【问题描述】:
我是 Java 新手,我正在处理如何将对象附加到另一个对象以及如何将对象添加到其他类中的 ArrayList。我有 3 个班级学校,学科,员工。我必须将 2 个 Staffs 对象添加到 2 个 Subject 对象,然后将 4 个 Staff 对象添加到 School 的 ArrayList 中。这是我的代码
import java.util.ArrayList;
import java.util.Scanner;
public class School {
public String name;
public Staff headOfSchool;
private Staff schoolManager;
private ArrayList<Staff> staffList;
public ArrayList<Subject> subjectList;
public School(String name) {
this.name = name;
}
public School(Staff schoolManager){
this.schoolManager = schoolManager;
}
public School() {
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Staff> staffList = new ArrayList<>();
ArrayList<Subject> subjectList = new ArrayList<>();
System.out.println("Enter the school name: ");
String name = scan.next();
School school = new School(name);
}
}
public class Staff {
public int id;
public String name;
public String gender;
public String title;
public String email;
public int phone;
public String position;
public String location;
public Staff(int id, String name, String gender, String title,
String email, int phone, String position, String location) {
this.id = id;
this.name = name;
this.gender = gender;
this.title = title;
this.email = email;
this.phone = phone;
this.position = position;
this.location = location;
}
public void getStaffDetail() {
Scanner scan = new Scanner(System.in);
for(int i = 0; i < 4; i++){
System.out.println("Please enter title: ");
String title = scan.next();
System.out.println("Please enter name: ");
String name = scan.next();
System.out.println("Please enter ID: ");
int id = scan.nextInt();
System.out.println("Please enter gender: ");
String gender = scan.next();
System.out.println("Please enter phone number: ");
int phone = scan.nextInt();
System.out.println("Please enter email: ");
String email = scan.next();
System.out.println("Please enter location: ");
String location = scan.next();
System.out.println("Please enter position: ");
String position = scan.next();
switch (i){
case 0:
Staff staff = new Staff(id, name, gender, title,
email, phone, position, location);
case 1:
Staff staff1 = new Staff(id, name, gender, title,
email, phone, position, location);
case 2:
Staff staff2 = new Staff(id, name, gender, title,
email, phone, position, location);
case 3:
Staff staff3 = new Staff(id, name, gender, title,
email, phone, position, location);
}
public class Subject {
public String name;
public int code;
public String session;
public int year;
public Staff coordinator;
public String lecture;
public String lab;
public String campus;
public Subject(String name, int code, String session, int year,
String lecture, String lab, String campus) {
this.name = name;
this.code = code;
this.session = session;
this.year = year;
this.lecture = lecture;
this.lab = lab;
this.campus = campus;
}
public Subject(Staff coordinator) {
this.coordinator = coordinator;
}
public String getName() {
return name;
}
public int getCode() {
return code;
}
public String getSession() {
return session;
}
public int getYear() {
return year;
}
public String getLecture() {
return lecture;
}
public String getLab() {
return lab;
}
public String getCampus(){
return campus;
}
}
如果有人可以帮助我,我将不胜感激。非常感谢
【问题讨论】:
标签: java object oop arraylist attachment