Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import java.util.EnumSet;
- import java.util.Scanner;
- public class Main_Class
- {
- static Scanner t=new Scanner(System.in);
- static ArrayList<Student> myList = new ArrayList<Student>();
- enum MenuOpt
- {
- ADD, REMOVE, SEARCH, PRINT, SAVE, LOAD, EXIT
- }
- public static void main(String[] args)
- {
- show_menu();
- }
- //options menu
- public static void show_menu()
- {
- String opt="Exit";
- do
- {
- System.out.println("\n\n");
- for (MenuOpt meniu : EnumSet.allOf(MenuOpt.class))
- {
- System.out.println(meniu);
- }
- System.out.println("Type your choice as written in the menu above!");
- opt=t.nextLine();
- MenuOpt mo = getEnumFromString(MenuOpt.class, opt);
- System.out.println(mo);
- switch (mo)
- {
- case ADD:
- { add(); break; }
- case REMOVE:
- { remove(); break; }
- case SEARCH:
- { search(); break; }
- case PRINT:
- { print(); break; }
- case SAVE:
- { save(); break; }
- case LOAD:
- { load(); break; }
- default:
- { opt="Exit"; break; }
- }
- }while(true);
- }
- //menu methods
- public static void add()
- {
- String nume;
- int age;
- Float mark;
- System.out.println("\nType data for new Student:\nName: ");
- nume=t.nextLine();
- System.out.println("\nAge:");
- age=t.nextInt();
- System.out.println("\nMark");
- mark=t.nextFloat();
- myList.add(new Student(nume, age, mark));
- t.nextLine();
- System.out.println("Student added!");
- }
- public static void remove()
- {
- System.out.println("\nType name of Student to remove: ");
- String name=t.nextLine();
- boolean found=false;
- for(Student s : myList)
- {
- if (s.getName().equals(name))
- {
- myList.remove(s);
- System.out.println("Done!");
- found=true;
- break;
- }
- }
- if (found==false)
- System.out.println("Student not found!");
- }
- public static String search()
- {
- String nume;
- int age;
- Float mark;
- System.out.println("\nType data for Student to search:\nName: ");
- nume=t.nextLine();
- System.out.println("\nAge:");
- age=t.nextInt();
- System.out.println("\nMark");
- mark=t.nextFloat();
- Student myStudent = new Student(nume, age, mark);
- for(Student s : myList)
- {
- if (s.equals(myStudent))
- {
- return "Student found!";
- }
- }
- return "Student not found!";
- }
- public static void print()
- {
- for(Student s : myList)
- {
- System.out.println(s);
- }
- }
- public static void save()
- {
- try
- {
- ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("date"));
- for(Student s : myList)
- {
- out.writeObject(s);
- }
- out.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public static void load()
- {
- try
- {
- FileInputStream fis = new FileInputStream("date");
- ObjectInputStream in = new ObjectInputStream(fis);
- while (fis.available()>0 )
- {
- myList.add((Student) in.readObject());
- }
- in.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- //create enum from a string to switch it in the menu
- public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string) {
- if( c != null && string != null ) {
- try {
- return Enum.valueOf(c, string.trim().toUpperCase());
- } catch(IllegalArgumentException ex) {
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement