公共类UserDeletedStudentFile { 学生信息学生信息; //声明一个StudentInformation对象 CollectStudentInformation文件; //声明一个CollectStudentInformation对象
// This constructor makes a copy of the StudentInformation object passed by the infoObject argument...
public UserDeletedStudentFile(StudentInformation infoObject)
{
studentInfo = new StudentInformation(infoObject);
}
// This no-arg constructor allows access from the DemoStudentAdmin.java class...
public UserDeletedStudentFile() {};
// getStudentInformation() returns a copy of the StudentInformation object with student name and ID number...
public StudentInformation getStudentInformation()
{ return new StudentInformation(studentInfo); }
// deleteStudentFile() deletes a student file as needed by the user...
public void deleteStudentFile() throws IOException
{
String userInput = ""; // For user input
int userToFileTransition = 0; // Subracting one from the user's choice to read the record needed
int selectionNumber = 0; // Number for user to choose
int indexNumber = 0;
// Open a new studentDirectory.dat and populate the ArrayList<>...
file = new CollectStudentInformation("studentFileDirectory.dat");
// Create an ArrayList<>...
ArrayList<StudentInformation> studentFiles = new ArrayList<>();
// Populate the ArrayList<> with the information from the StudentInformation class...
for(int y = 0; y < file.getNumberOfRecords(); y++)
{
file.moveFilePointer(y);
studentInfo = file.readStudentFile();
studentFiles.add(studentInfo);
}
// Ask the user if they wish to see one record, all records of currently enrolled students or Quit the menu option...
while(selectionNumber < 1 || selectionNumber > 3)
{
userInput = JOptionPane.showInputDialog(null, "\n\nPlease choose from one of the following options:" +
"\nPress '1' for the student record number" +
"\nPress '2' for the Mars University directory" +
"\nPress '3' to return to the main menu.");
selectionNumber = Integer.parseInt(userInput);
}
// Display the selection choices...
if(selectionNumber == 1)
{
// Get a record number from the user, subtract 1 and display...
userInput = JOptionPane.showInputDialog(null, "Enter the record number you wish to delete: ");
userToFileTransition = Integer.parseInt(userInput);
indexNumber = (userToFileTransition - 1); // pulling from the array, not the random access file...
// Read the record at that location...
studentInfo = studentFiles.get(indexNumber);
// Display the student information for the user to verify...
System.out.println("\n" + studentInfo.getfirstName());
System.out.println(studentInfo.getLastName());
System.out.println(studentInfo.getStudentID());
// Remove the student file...
studentFiles.remove(indexNumber);
// Testing for the correct number of elements in the arrayList...
for(StudentInformation student : studentFiles)
System.out.println(student);
//Create an CollectStudentInformation object...
file = new CollectStudentInformation("studentFileDirectory.dat");
for(int x = 0; x < studentFiles.size(); x++)
file.writeStudentFile(studentFiles.get(x));
}
// Clear the ArrayList<> of all values...
studentFiles.clear();
// close the file...
file.close();
// Return to the main() to access the menu...
DemoStudentAdmin.main(null);
所以...每个类(创建,读取,修改和删除)都以相同的方式创建,只有'Delete'类没有编写更新的ArrayList(studentFiles)。相反,它将删除请求的对象并将该元素替换为以下对象的副本。 trimToSize()在这个实例中也不起作用。
再次感谢您的帮助!