i want to add a function that allow me to delete a students
first this is the this the file student.java that contain the class student , then the file called vstudent contain the ittrator and the array list to loop on the all students,then there is a file called Mainsystem contain the files handilling and functions to add students ,so i want to add fuction that allow me to delete a student.
import java.io.*;
import java.util.Scanner;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class student implements Serializable {
private int id;
private String name, email;
private int mobilenumber;
public student(int id, String name, String email, int mobilenumber) {
this.id = id;
this.name=name;
this.email = email;
this.mobilenumber = mobilenumber;
}
public int getId() {
return id;
}
@Override
public String toString() {
return "student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", mobilenumber=" + mobilenumber +
'}';
}
}
import java.io.*;
import java.io.File.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.io.Serializable;
import java.util.Scanner;
public class vstudent extends Object implements Serializable {
private List<student> coll;
public vstudent() {
coll = new ArrayList<student>();
}
public void addstudent(student stu){
coll.add(stu);
}
@Override
public String toString() {
String total = "\n";
Iterator<student> i = coll.iterator();
while (i.hasNext()){
student s = (student) i.next();
total= total+s.toString();
}
return total;
}
}
import java.util.List;
import java.util.Scanner;
import java.io.Serializable;
import java.io.*;
import java.util.Iterator;
class librarianmenu {
public static void librarianMenu() {
System.out.println("---------------(Librarian) - Library Management System-----------");
System.out.println("\nEnter 0 for load a library" + "\nEnter 1 for save and quit" + "\nEnter 2 to add book"
+ "\nEnter 3 to list all books in library" + "\nEnter 4 to list all librarians");
}
}
public class Mainsystem {
static String fileName = null;
static library lib = new library();
static vstudent stu = new vstudent();
static librs librs = new librs();
static Scanner in = new Scanner(System.in);
static Boolean running = true;
public static void DisplayMainMenu() {
System.out.println("------------ Library Management System ------------");
System.out.println("Enter #: Switch to Admin Mode");
System.out.println("Enter $: Switch to Student Mode");
System.out.println("Enter <: Exit");
}
public static void main(String[] args) {
DisplayMainMenu();
Scanner in = new Scanner(System.in);
boolean shouldContinue = true;
while (true) {
String optionSelected = in.next();
if (!optionSelected.equals("<")) {
if (optionSelected.equals("#")) {
Admin admin = new Admin();
System.out.print("Enter Password: ");
String tempPassword = in.next();
System.out.println(tempPassword);
if (tempPassword.equals(admin.getPassword())) {
boolean isCorrectPassword = true;
while (isCorrectPassword) {
admin.displayMenu();
int adminInput = in.nextInt();
switch (adminInput) {
case 0: {
System.out.println("Enter the file name to load");
loadScript(in.next());
break;
}
case 1: {
addstudent();
break;
}
case 2: {
addlibrarians();
break;
}
case 3: {
System.out.println(stu.toString());
break;
}
case 4: {
saveandquit();
break;
}
case 5: {
System.out.print("Enter new password:");
String pass = in.next();
admin.setPassword(pass);
break;
}
case 6: {
isCorrectPassword = false;
DisplayMainMenu();
}
}
}
} else {
DisplayMainMenu();
}
} else if (optionSelected.equals("$")) {
librarianmenu menu = new librarianmenu();
while (running) {
librarianmenu.librarianMenu();
int studentInput = in.nextInt();
switch (studentInput) {
case 0: {
System.out.println("Enter the file name to load");
loadScript(in.next());
break;
}
case 1: {
saveandquit();
break;
}
case 2: {
addBook();
break;
}
case 3: {
System.out.println(lib.toString());
break;
}
case 4: {
System.out.println(librs.toString());
}
}
}
} else { //User chooses to Exit
break;
}
}
}
}
private static void addBook () {
int id;
String title, author;
int Available_quantity;
int issued_quantity;
System.out.println("\nEnter book name");
title = in.next();
System.out.println("\nEnter book Author");
author = in.next();
System.out.println("\nEnter book id");
id = in.nextInt();
System.out.println("\nEnter Available quantity");
Available_quantity = in.nextInt();
System.out.println("\nEnter issued_quantity");
issued_quantity = in.nextInt();
Book b = new Book(id, title, author, Available_quantity, issued_quantity);
lib.addBook(b);
}
private static void addstudent () {
int id;
String name, email;
int mobilenumber;
System.out.println("\nEnter student name");
name = in.next();
System.out.println("\nEnter student email");
email = in.next();
System.out.println("\nEnter student id");
id = in.nextInt();
System.out.println("\nEnter mobile number ");
mobilenumber = in.nextInt();
student s = new student(id, name, email, mobilenumber);
stu.addstudent(s);
}
private static void addlibrarians () {
int id;
String name;
int password;
System.out.println("\nEnter librarian name");
name = in.next();
System.out.println("\nEnter librarian id");
id = in.nextInt();
System.out.println("\nEnter password ");
password = in.nextInt();
librarians libras = new librarians(id, name, password);
librs.addlibrs(libras);
}
private static void saveandquit () {
System.out.println("Enter file name:");
fileName = in.next() + ".ser";
running = false;
FileOutputStream fos = null;
ObjectOutputStream out = null;
try {
fos = new FileOutputStream(fileName);
out = new ObjectOutputStream(fos);
out.writeObject(lib);
out.writeObject(stu);
fos.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void loadScript (String name){
FileInputStream fis = null;
ObjectInputStream in = null;
File file = new File(name + ".ser");
if (file.exists()) {
try {
fis = new FileInputStream(file);
in = new ObjectInputStream(fis);
try {
lib = (library) in.readObject();
stu = (vstudent) in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
fis.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("\nThe file does not exist!");
}
}
}
As I can see , You use List from which you want to delete element, In Java if you want to delete element you need to provide its id , Syntax :
StudentList.remove(idStudent)
for you i think you want to find a way to get the ID of the student you want to delete this is a way:
private static void deleteStudent () {
System.out.println("\nEnter ID of the student you want to delete ");
idStudent = in.nextInt();
for( Student st : stu){
if(st.getID()==idStudent){
stu.remove(idStudent);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.