gpt4 book ai didi

java - 尝试创建一个我可以进一步操作的学生数据库

转载 作者:行者123 更新时间:2023-12-01 19:32:41 25 4
gpt4 key购买 nike

我正在尝试创建一个数据库(作为 ArrayList),用于存储学生的信息(对象数组),但是我遇到了“addStudent()”函数的问题,因为它似乎只利用最新的通过调用函数创建的数组。我知道我可能错过了眼前的一些东西,但我已经开始过度思考,我相信并认为我需要一双新的眼睛。任何帮助是极大的赞赏。我还想问,如果您对我需要实现的代码有任何指示,以便执行我试图通过该程序实现的操作,请继续友善地指出这些。多谢你们。我的代码是:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Record {

// Create an array to hold the Student's info & List to hold those student's
static Object[] student = new Object[10];
static List<Object> database = new ArrayList<Object>();

// Create Student's info variables
private int studentID;
private String lastName;
private String firstName;
private long phoneNumber;
private String major;
private double gpa;
private int birthYear;
private String birthMonth;
private int birthDay;
private String homeAddress;

/* For FUTURE tasks */
public void setStudentID( int idGiven ) {

studentID = idGiven;
}

public void setLastName( String lastNameGiven ) {

lastName = lastNameGiven;
}

public void setFirstName( String firstNameGiven ) {

firstName = firstNameGiven;
}

public void setPhoneNumber( long numGiven ) {

phoneNumber = numGiven;
}

public void setMajor( String majorGiven ) {

major = majorGiven;
}

public void setGPA( double gpaGiven ) {

gpa = gpaGiven;
}

public void setBirthYear( int birthYearGiven ) {

birthYear = birthYearGiven;
}

public void setBirthMonth( String birthMonthGiven ) {

birthMonth = birthMonthGiven;
}

public void setBirthDay( int birthDayGiven ) {

birthDay = birthDayGiven;
}

public void setHomeAddress( String homeAddressGiven ) {

homeAddress = homeAddressGiven;
}

// Function to create new student when one is entered
static public Object[] newStudent( int newStudentID, String newLastName, String newFirstName, long newPhoneNumber, String newMajor, double newGPA, int newBirthYear, String newBirthMonth, int newBirthDay, String newHomeAddress )
{

student[0] = newStudentID;
student[1] = newLastName;
student[2] = newFirstName;
student[3] = newPhoneNumber;
student[4] = newMajor;
student[5] = newGPA;
student[6] = newBirthYear;
student[7] = newBirthMonth;
student[8] = newBirthDay;
student[9] = newHomeAddress;

database.addAll(Arrays.asList(student));



return student;
}

// Create Function to add the student array to the database list
static public List<Object> addStudent ( Object[] s ) {

student = s;
database.add(student);

return database;
}

// Test Program
public static void main( String [] args )
{
Object[] student1 = Record.newStudent(00000000, "Thomas", "Luke", 9038189888L, "Computer Science", 4.00, 1997, "February", 28, "169 Karen Dr.");
Object[] student2 = Record.newStudent(00000001, "Doe", "Jane", 5802679999L, "Mathematics", 3.62, 1990, "March", 1, "100 Circle Dr.");
Object[] student3 = Record.newStudent(00000002, "Baker", "Joe", 9805776452L, "Safety", 2.75, 1998, "December", 7, "200 Circle Dr.");
//Object[] student4 = Record.newStudent(00000003, "Williams", "Doug", 9407778888L, "Business", 1.95, 2000, "May", 7, "501 West Maple St.");

Record.addStudent(student1);
Record.addStudent(student2);
Record.addStudent(student3);
//Record.addStudent(student4);

//Print out Student1's Info from the created student & from its index in the List containing all students
System.out.println(Arrays.toString(student1));
System.out.println(database.get(0));

/* PERFORM TASKS:
* 1. delete record(s) from student id
* 2. list all student info in the order they were saved
* 3. Reorder/organize student info ascending & descending by id, gpa, or birthday
* 4. list students in a given major or given id
*/
}


}

最佳答案

“如果”你绝对只能使用数组来存储学生信息,这对我来说似乎是错误的,那么在newStudent中,你必须每次创建一个新数组时间

static public Object[] newStudent( int newStudentID, String newLastName, String newFirstName, long newPhoneNumber, String newMajor, double newGPA, int newBirthYear, String newBirthMonth, int newBirthDay, String newHomeAddress )
{

Object[] student = new Object[10];
student[0] = newStudentID;
student[1] = newLastName;
student[2] = newFirstName;
student[3] = newPhoneNumber;
student[4] = newMajor;
student[5] = newGPA;
student[6] = newBirthYear;
student[7] = newBirthMonth;
student[8] = newBirthDay;
student[9] = newHomeAddress;

database.add(student);

return student;
}

然后您应该将整个数组(按原样)添加到 ArrayList

然而,这对我来说似乎违反直觉。相反,您应该从“学生”的概念开始,例如......

public class Student {

private int studentID;
private String lastName;
private String firstName;
private long phoneNumber;
private String major;
private double gpa;
private int birthYear;
private String birthMonth;
private int birthDay;
private String homeAddress;

public Student(int studentID, String lastName, String firstName, long phoneNumber, String major, double gpa, int birthYear, String birthMonth, int birthDay, String homeAddress) {
this.studentID = studentID;
this.lastName = lastName;
this.firstName = firstName;
this.phoneNumber = phoneNumber;
this.major = major;
this.gpa = gpa;
this.birthYear = birthYear;
this.birthMonth = birthMonth;
this.birthDay = birthDay;
this.homeAddress = homeAddress;
}

public int getStudentID() {
return studentID;
}

public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}

public long getPhoneNumber() {
return phoneNumber;
}

public String getMajor() {
return major;
}

public double getGpa() {
return gpa;
}

public int getBirthYear() {
return birthYear;
}

public String getBirthMonth() {
return birthMonth;
}

public int getBirthDay() {
return birthDay;
}

public String getHomeAddress() {
return homeAddress;
}

@Override
public String toString() {
return "Student{" + "studentID=" + studentID + ", lastName=" + lastName + ", firstName=" + firstName + ", phoneNumber=" + phoneNumber + ", major=" + major + ", gpa=" + gpa + ", birthYear=" + birthYear + ", birthMonth=" + birthMonth + ", birthDay=" + birthDay + ", homeAddress=" + homeAddress + '}';
}

}

然后可以通过某种“数据库”进行管理......

public class Database {
private List<Student> records;

public Database() {
records = new ArrayList<>(25);
}

public void add(Student student) {
// First make sure there are a no duplictes
records.add(student);
}

public Student get(int at) {
return records.get(at);
}

public Student remove(int at) {
return records.remove(at);
}

public int size() {
return records.size();
}
}

就我个人而言,我会使用Database来创建Student的新实例,但我可能已经偏离主题了

然后你可以使用它,比如......

Database database = new Database();

database.add(new Student(00000000, "Thomas", "Luke", 9038189888L, "Computer Science", 4.00, 1997, "February", 28, "169 Karen Dr."));
database.add(new Student(00000001, "Doe", "Jane", 5802679999L, "Mathematics", 3.62, 1990, "March", 1, "100 Circle Dr."));
database.add(new Student(00000002, "Baker", "Joe", 9805776452L, "Safety", 2.75, 1998, "December", 7, "200 Circle Dr."));

//Print out Student1's Info from the created student & from its index in the List containing all students
for (int index = 0; index < database.size(); index++) {
System.out.println(database.get(index));
}

这会打印出类似...的内容

Student{studentID=0, lastName=Thomas, firstName=Luke, phoneNumber=9038189888, major=Computer Science, gpa=4.0, birthYear=1997, birthMonth=February, birthDay=28, homeAddress=169 Karen Dr.}
Student{studentID=1, lastName=Doe, firstName=Jane, phoneNumber=5802679999, major=Mathematics, gpa=3.62, birthYear=1990, birthMonth=March, birthDay=1, homeAddress=100 Circle Dr.}
Student{studentID=2, lastName=Baker, firstName=Joe, phoneNumber=9805776452, major=Safety, gpa=2.75, birthYear=1998, birthMonth=December, birthDay=7, homeAddress=200 Circle Dr.}

关于java - 尝试创建一个我可以进一步操作的学生数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59245471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com