Untitled
unknown
plain_text
6 months ago
13 kB
4
Indexable
Never
import java.io.*; import java.util.*; class Main { public static void main(String[] args) throws Exception { System.out.println("Welcome to Student's Data Management System"); System.out.println(); Scanner sc = new Scanner(System.in); String username = "a"; int password = 1; System.out.print("Enter Username: "); String user = sc.next(); System.out.print("Enter password: "); int pass = sc.nextInt(); int c = 0; while (c == 0) { if (pass == password && user.equals(username)) { System.out.println("Log in Successful"); int choice = -1; ArrayList<Student> A = new ArrayList<>(); File file = new File("std.txt"); ObjectInputStream input = null; ObjectOutputStream output = null; ListIterator<Student> list = null; if (file.exists()) { try { input = new ObjectInputStream(new FileInputStream(file)); A = (ArrayList<Student>) input.readObject(); input.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } else { System.out.println("Creating a new file."); file.createNewFile(); } do { System.out.println("1. Add"); System.out.println("2. Display"); System.out.println("3. Search"); System.out.println("4. Delete"); System.out.println("5. Update"); System.out.println("0. Exit"); System.out.println("Please enter your choice: "); choice = sc.nextInt(); switch (choice) { case 1: System.out.println("How many Students: "); int n = sc.nextInt(); for (int i = 0; i < n; i++) { System.out.println("Enter Name: "); String name = sc.next(); System.out.println("Enter Id: "); String id = sc.next(); System.out.println("Enter Department: "); String dept = sc.next(); System.out.println("Enter Cgpa: "); float cgpa = sc.nextFloat(); System.out.println("Enter Address: "); String address = sc.next(); System.out.println("Enter Age: "); int age = sc.nextInt(); System.out.println("Enter ContactNumber: "); long contactNumber = sc.nextLong(); A.add(new Student(name, id, dept, cgpa, address, age, contactNumber)); } output = new ObjectOutputStream(new FileOutputStream(file)); output.writeObject(A); output.close(); break; case 2: try { input = new ObjectInputStream(new FileInputStream(file)); A = (ArrayList<Student>) input.readObject(); input.close(); System.out.println("---------------------------------"); list = A.listIterator(); while (list.hasNext()) { System.out.println(list.next()); } System.out.println("---------------------------------"); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } break; case 3: try { input = new ObjectInputStream(new FileInputStream(file)); A = (ArrayList<Student>) input.readObject(); input.close(); boolean found = false; System.out.println("How many Students: "); int n3 = sc.nextInt(); for (int i = 0; i < n3; i++) { System.out.println("Enter Student's Id To Search:"); String id = sc.next(); System.out.println("-------------------"); list = A.listIterator(); while (list.hasNext()) { Student S = list.next(); if (S.id.equals(id)) { System.out.println(S); found = true; } } } if (!found) { System.out.println("Data not found"); } System.out.println("-------------------"); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } break; case 4: try { input = new ObjectInputStream(new FileInputStream(file)); A = (ArrayList<Student>) input.readObject(); input.close(); boolean found = false; System.out.println("How many Students: "); int n1 = sc.nextInt(); for (int j = 0; j < n1; j++) { System.out.println("Enter Student's Id To Delete:"); String id = sc.next(); System.out.println("-------------------"); list = A.listIterator(); while (list.hasNext()) { Student S = list.next(); if (S.id.equals(id)) { list.remove(); found = true; } } } if (found) { output = new ObjectOutputStream(new FileOutputStream(file)); output.writeObject(A); output.close(); System.out.println("Delete Successful"); } else { System.out.println("Data not found"); } System.out.println("-------------------"); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } break; case 5: try { input = new ObjectInputStream(new FileInputStream(file)); A = (ArrayList<Student>) input.readObject(); input.close(); boolean found = false; System.out.println("How many Students: "); int n2 = sc.nextInt(); for (int k = 0; k < n2; k++) { System.out.println("Enter Student's Id to Update:"); String id = sc.next(); System.out.println("-------------------"); list = A.listIterator(); while (list.hasNext()) { Student S = list.next(); if (S.id.equals(id)) { System.out.println("Enter new Name: "); String new_name = sc.next(); System.out.println("Enter new Id: "); String new_id = sc.next(); System.out.println("Enter new Department: "); String new_dept = sc.next(); System.out.println("Enter new Cgpa: "); float new_cgpa = sc.nextFloat(); System.out.println("Enter new Address: "); String new_address = sc.next(); System.out.println("Enter new Age: "); int new_age = sc.nextInt(); System.out.println("Enter new ContactNumber: "); long new_contactNumber = sc.nextLong(); list.set(new Student(new_name, new_id, new_dept, new_cgpa, new_address, new_age, new_contactNumber)); found = true; } } } if (found) { output = new ObjectOutputStream(new FileOutputStream(file)); output.writeObject(A); output.close(); System.out.println("Data Updated Successfully"); } else { System.out.println("Data not found"); } System.out.println("-------------------"); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } break; case 0: System.out.println("Exiting the System"); System.exit(0); break; default: System.out.println("Wrong choice of Number!!"); System.out.println("Try Again!"); } } while (choice != 0); c = 1; } else { System.out.println("Wrong Information"); System.out.println("Please ReEnter username and password again!!"); System.out.print("Enter Username: "); user = sc.next(); System.out.print("Enter password: "); pass = sc.nextInt(); } } } } class Student implements Serializable { String name, id, dept, address; float cgpa; int age; long contactNumber; public Student(String name, String id, String dept, float cgpa, String address, int age, long contactNumber) { this.name = name; this.id = id; this.dept = dept; this.cgpa = cgpa; this.address = address; this.age = age; this.contactNumber = contactNumber; } @Override public String toString() { return "Name: " + name + "\nID: " + id + "\nDepartment: " + dept + "\nCGPA: " + cgpa + "\nAddress: " + address + "\nAge: " + age + "\nContact Number: " + contactNumber; } }