私はこれが厄介な実装であることを知っていますが、基本的にこのコードを持っており(私はすべてを書きました)、適切なメニュー選択を使用するときにリストから学生またはインストラクターを削除できる必要があります。メニューオプション3と4だけでなく、コード内の他のすべてが機能します。削除しようとすると、オブジェクトにまったく同じ情報を入力します。これがコードです。3つのクラスはすべて以下のとおりです。
ドライバークラス:
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
private ArrayList<Student> students;
private ArrayList<Instructor> instructors;
public static void main(String[] args) {
Driver aDriver = new Driver();
aDriver.run();
}
public Driver() {
students = new ArrayList<Student>();
instructors = new ArrayList<Instructor>();
}
private void run() {
Student aStudent;
Instructor anInstructor;
Scanner inp = new Scanner(System.in);
int choice = -1;
String str = "Enter a menu option:\n";
str += " 0: Quit\n";
str += " 1: Add new student\n";
str += " 2: Add new instructor\n";
str += " 3: Delete existing student\n";
str += " 4: Delete existing instructor\n";
str += " 5: Print list of students\n";
str += " 6: Print list of instructors\n";
str += "Your choice: ";
do {
System.out.print(str);
choice = inp.nextInt();
switch(choice) {
case 0:
System.out.println("Thanks! Have a great day!");
break;
case 1:
aStudent = getStudentInfo();
addStudent(aStudent);
break;
case 2:
anInstructor = getInstructorInfo();
addInstructor(anInstructor);
break;
case 3:
aStudent = getStudentInfo();
deleteStudent(aStudent);
break;
case 4:
anInstructor = getInstructorInfo();
deleteInstructor(anInstructor);
break;
case 5:
printStudents();
break;
case 6:
printInstructors();
break;
default:
System.out.println("Invalid menu item " + choice);
}
}
while(choice != 0);
}
public Student getStudentInfo() {
Student aStudent;
String name = null;
String id = null;
double GPA = 0.0;
Scanner inp = new Scanner(System.in);
System.out.print("\n\nEnter the student's name: ");
name = inp.nextLine();
System.out.print("Enter the student's ID: ");
id = inp.nextLine();
System.out.print("Enter the student's GPA: ");
GPA = inp.nextDouble();
aStudent = new Student(name, id, GPA);
return aStudent;
}
public Instructor getInstructorInfo() {
Instructor anInstructor;
String name = null;
String id = null;
String dept = null;
String email = null;
Scanner inp = new Scanner(System.in);
System.out.print("\n\nEnter the instructor's name: ");
name = inp.nextLine();
System.out.print("Enter the instructor's ID: ");
id = inp.nextLine();
System.out.print("Enter the instructor's department: ");
dept = inp.nextLine();
System.out.print("Enter the instructor's email address: ");
email = inp.nextLine();
anInstructor = new Instructor(name, id, dept, email);
return anInstructor;
}
public void addStudent(Student aStudent) {
students.add(aStudent);
}
public void addInstructor(Instructor anInstructor) {
instructors.add(anInstructor);
}
public void deleteStudent(Student aStudent) {
students.remove(aStudent);
}
public void deleteInstructor(Instructor anInstructor) {
instructors.remove(anInstructor);
}
public void printStudents() {
System.out.println("\n\n" + Student.printHeader());
for(int i = 0; i < students.size(); i++) {
System.out.print(students.get(i));
}
System.out.print("\n\n");
}
public void printInstructors() {
System.out.print("\n\n" + Instructor.printHeader());
for(int i = 0; i < instructors.size(); i++) {
System.out.print(instructors.get(i));
}
System.out.print("\n\n");
}
}
学生クラス:
public class Student {
private String name;
private String id; //String to allow for the possibility of leading zeroes
private double GPA;
public Student() {
name = "TestFirst TestLast";
id = "00000";
GPA = -1.00;
}
public Student(String name1, String id1, double GPA1) {
name = name1;
id = id1;
GPA = GPA1;
}
public static String printHeader() {
String str = String.format("%-25s%-7s%-6s\n", "Name", "ID", "GPA");
return str;
}
public String toString() {
String str = String.format("%-25s%-7s%-6.3f\n", name, id, GPA);
return str;
}
public String getName() {
return name;
}
public void setGPA(double GPA2) {
GPA = GPA2;
}
}
インストラクタークラス:
public class Instructor {
private String name;
private String id;
private String dept;
private String email;
public Instructor() {
name = "TestFirst TestLast";
id = "-00001";
dept = "TestDept";
email = "test@test.net";
}
public Instructor(String name1, String id1, String dept1, String email1) {
name = name1;
id = id1;
dept = dept1;
email = email1;
}
public static String printHeader() {
String str = String.format("%-30s%-6s%-15s%-15s\n", "Name", "ID", "Department", "Email Address");
return str;
}
public String toString() {
String str = String.format("%-30s%-6s%-15s%-15s\n", name, id, dept, email);
return str;
}
public String getName() {
return name;
}
}