我必须使用java编写一个程序来存储和管理一组注册课程的学生。 对于每个学生,应存储他们的姓名,出生日期(年龄/毫米/年),地址和性别。 该计划需要将学生添加到课程或从课程中删除。程序用户必须能够按名称搜索学生(假设名称是唯一的)。
这是我到目前为止所做的:
import java.util.Scanner;
public class Student {
private String name;
private String address;
private String gender;
private String dob;
Scanner in = new Scanner(System.in);
public Student(String sName, String sAddress, String sGender, String sDob) {
name = sName;
address = sAddress;
gender = sGender;
dob = sDob;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getGender() {
return gender;
}
public String getDob() {
return dob;
}
public void setName(String name) {
System.out.println("Please enter the students name");
name = in.nextLine();
}
public void setAddress(String address) {
System.out.println("Please enter the students address");
address = in.nextLine();
}
public void setGender(String gender) {
System.out.println("Please enter the students gender");
gender = in.nextLine();
}
public void setDob(String dob) {
System.out.println("Please enter the students DOB");
dob = in.nextLine();
}
}
public class student_tester {
public static void main (String [] args) {
Student student01 = new Student ("", "" ,"", "");
student01.setName(null);
student01.setAddress(null);
student01.setGender(null);
student01.setDob(null);
Student student02 = new Student ("", "" ,"", "");
student02.setName(null);
student02.setAddress(null);
student02.setGender(null);
student02.setDob(null);
}
}
答案 0 :(得分:0)
一个好的做法是在Student类中维护一个唯一的标识符字段,例如学生ID。您可以使用main方法在驱动程序中维护一个List,即学生测试人员。 确保您在测试者课程或驾驶员课程中阅读输入,而不是在学生课程中。因此,您可以继续将每个学生对象添加到列表中。
import java.util.Scanner;
public class student_tester {
public static void main (String [] args) {
List<Student> allStudents =new ArrayList<Student>();
Scanner in = new Scanner(System.in);
while(true){
Student student01 = new Student ();
System.out.println("Please enter the students name");
student01.setName(in.nextLine());
System.out.println("Please enter the students address");
student01.setAddress(in.nextLine());
System.out.println("Please enter the students DOB");
student01.setDob(in.nextLine());
System.out.println("Please enter the students gender");
student01.setGender(in.nextLine());
}
}
您应该阅读测试人员课程中的输入并循环以继续添加新学生作为用户输入。
根据字段进行搜索:
例如,在给出学生姓名时搜索和删除。
public Student search( String studentName){
foreach(Student s : allStudents)
if(s.getName().equals(studentName){
// you can return this student object or print his other details
// System.out.println(s.getAddress()); etc
return s;
}
return null;
}
}
删除:
public void delete(String studentName)
foreach(Student s : allStudents)
if(s.getName().equals(studentName){
allStudents.remove(s);
}
}
}
您可以在Student类中删除包含4个参数的构造函数。并按照以下方式更改所有设置者:
public class Student {
// private String id .. maybe
private String name;
private String address;
private String gender;
private String dob;
//for clarity
public Student(){
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getGender() {
return gender;
}
public String getDob() {
return dob;
}
public void setName(String name) {
this.name=name;
}
public void setAddress(String address) {
this.address=address;
}
public void setGender(String gender) {
this.gender=gender;
}
public void setDob(String dob) {
this.dob=dob;
}
}