我正在创建一个类似于学生注册系统的程序。我可以添加尽可能多的学生,并可以通过搜索他们的ID号找到学生。但我仍然坚持删除和更新学生的代码。有什么建议?
import java.io.Serializable;
import java.util.ArrayList;
public class School implements Serializable
{
ArrayList<Student>students;
/**
Creates an arrayList that holds student objects
*/
public School()
{
students = new ArrayList<Student>();
}
/**
Adds a student to the arrayList
*/
public void addStudent(Student a)
{
students.add(a);
}
public String toString()
{
String content = "";
if(!students.isEmpty())
{
for(int i = 0; i<students.size(); i++)
{
content = content + students.get(i).getID() + " " + students.get(i).getName() + " " + students.get(i).getMajor() + " " + students.get(i).getQPA() + "\n";
}
}
return content;
}
public void remove(Student a)
{
remove(a);
}
}
import java.io.Serializable;
/**
This class creates a persistent student object with set parameters.
*/
public class Student implements Serializable
{
private int studID;
private String name;
private String major;
private double QPA;
/**
Creates a student object with an id, name, major, and QPA
@param aNum as an int
@param aName as a String
@param aMajor as a String
@param aQPA as a double
*/
public Student(int aNum, String aName, String aMajor, double aQPA)
{
studID = aNum;
name = aName;
major = aMajor;
QPA = aQPA;
}
/**
Gets the student's id
@return studID as an int
*/
public int getID()
{
return studID;
}
/**
Gets the student's name
@return name as String
*/
public String getName()
{
return name;
}
/**
Gets the student's major
@return major as a String
*/
public String getMajor()
{
return major;
}
/**
Gets the student's QPA
@return QPA as a double
*/
public double getQPA()
{
return QPA;
}
}
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class GUI extends JFrame
{
private static final int FRAME_WIDTH = 700;
private static final int FRAME_HEIGHT = 500;
private JPanel panel;
private JButton add;
private JButton remove;
private JButton update;
private JButton clear;
private JLabel Name;
private JTextField nameField;
private JLabel Major;
private JTextField majorField;
private JLabel ID;
private JTextField idField;
private JLabel QPA;
private JTextField qpaField;
private JTextArea area;
private JButton find;
School firstSchoolOfJava = new School();
public GUI()
{
createButtons();
createLabel();
createTextField();
createPanel();
setSize(FRAME_WIDTH,FRAME_HEIGHT);
}
public void createButtons()
{
add = new JButton("Add");
update = new JButton("Update");
clear = new JButton("Clear");
remove= new JButton("Remove");
find = new JButton("Find");
class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
Student newStudent = new Student(Integer.parseInt(idField.getText()), nameField.getText(), majorField.getText(), Double.parseDouble(qpaField.getText()));
firstSchoolOfJava.addStudent(newStudent);
System.out.println(firstSchoolOfJava.toString());
}
}
ButtonListener listener = new ButtonListener();
add.addActionListener(listener);
class ButtonListener1 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if(!firstSchoolOfJava.equals((idField.getText())))
{
}
}
}
ButtonListener1 listener1 = new ButtonListener1();
update.addActionListener(listener1);
class ButtonListener2 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
idField.setText(null);
nameField.setText(null);
majorField.setText(null);
qpaField.setText(null);
}
}
ButtonListener2 listener2 = new ButtonListener2();
clear.addActionListener(listener2);
class ButtonListener3 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if(!firstSchoolOfJava.equals(idField.getText()))
{
}
System.out.println(firstSchoolOfJava.toString());
}
}
ButtonListener3 listener3 = new ButtonListener3();
remove.addActionListener(listener3);
class ButtonListener4 implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if(!firstSchoolOfJava.equals(idField.getText()))
{
System.out.println("You found the account: " + idField.getText() + nameField.getText() + majorField.getText() + qpaField.getText());
}
}
}
ButtonListener4 listener4 = new ButtonListener4();
find.addActionListener(listener4);
}
public void createLabel()
{
Name = new JLabel("Name");
Major = new JLabel("Major");
ID = new JLabel("Student ID");
QPA = new JLabel("QPA");
}
public void createTextField()
{
final int FIELD_WIDTH = 10;
nameField = new JTextField(FIELD_WIDTH);
majorField = new JTextField(FIELD_WIDTH);
idField = new JTextField(FIELD_WIDTH);
qpaField = new JTextField(FIELD_WIDTH);
area = new JTextArea();
}
public void createPanel()
{
panel = new JPanel();
panel.add(ID);
panel.add(idField);
panel.add(Name);
panel.add(nameField);
panel.add(Major);
panel.add(majorField);
panel.add(QPA);
panel.add(qpaField);
panel.add(add);
panel.add(remove);
panel.add(update);
panel.add(clear);
panel.add(find);
panel.add(area);
add(panel);
}
}
答案 0 :(得分:0)
public void remove(Student a)
{
students.remove(a);
}
您应该从students
中删除。你有remove(a)
你可以这样做以获得学生
public void actionPerformed (ActionEvent event)
{
if(!firstSchoolOfJava.equals(idField.getText()))
{
// input should first be parsed to an int (id type is an int)
int id = Integer.parseInt(idField.getText());
Student s;
for (int i = 0; i < firstChoolOfJava.getStudents().size(); i++){
// check to see if the ids match. If they do, remove that student
if ((firstChoolOfJava.getStudents().get(i).getID()) == id) {
s = firstChoolOfJava.getStudents().get(i);
firstSchoolOfJava.remove(s); // you're removing this student
break;
}
}
}
System.out.println(firstSchoolOfJava.toString());
}
让此代码生效您需要getStudents
访问者方法School
public ArrayList<Student> getStudents(){
return students;
}
更新学生
public void actionPerformed (ActionEvent event)
{
String name = nameField.getText();
String major = majorField.getText();
double gpa = Double.parseDouble(gpaField.gtText());
int id = Integer.parseInt(idField.getText());
Student s = new Student(id, name, major, gpa); // or whatever your constructor looks like.
Student s1;
for (int i = 0; i < firstSchoolOfJava.getStudents().size(); i++){
s1 = firstSchoolOfJava.getStudents().get(i);
if (s1.getId() == id){
firstSchoolOfJava.getStudents().set(i, s);
break;
}
}
}
注意:上面的代码正在考虑所有字段都正确填写。如果您只想更新其他字段为空的字段,则需要检查哪些字段为空。
答案 1 :(得分:0)
你的问题需要更清楚。你想删除所有的学生吗?或者您想根据唯一的学生ID删除学生?为了帮助您入门,这里有一些基本的伪代码:
public boolean removeStudent(int uniqueID)
{
for(loop thru students)
{
if(listOfStudents.get(i).getId() == uniqueID)
{
listOfStudents.remove(i);
updateView();
return true;
}
}
return false;
}
此外,当您添加学生时,请执行以下操作:
/*
* Adds a student to the arrayList
*/
public void addStudent(Student a)
{
students.add(a); <-- You call add() from the ArrayList of students.
}
但是,当您删除学生时,请执行以下操作:
public void remove(Student a)
{
remove(a); <-- You do not call the ArrayList's remove() method at all. Instead,
you call the local remove() method defined in your School class.
}
而是执行以下操作:
public void remove(Student a)
{
students.remove(a); <-- Call the ArrayList's remove() method instead.
}
另外,更新学生是什么意思?我假设您的意思是根据更新的学生列表更新显示器或GUI?否则,我很困惑,因为无论何时添加或删除学生,ArrayList都会“更新”。
public void updateStudent(Student student, double GPA, String major)
{
for(loop thru list of students)
{
if(listOfStudents.get(i).equals(student))
{
listOfStudents.get(i).setGPA(GPA);
listOfStudents.get(i).setMajor(major);
}
}
}