Advertisement
Guest User

Main Class

a guest
Nov 27th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileOutputStream;
  3. import java.io.ObjectInputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.util.ArrayList;
  6. import java.util.EnumSet;
  7. import java.util.Scanner;
  8.  
  9. public class Main_Class
  10. {
  11.     static Scanner t=new Scanner(System.in);
  12.    
  13.     static ArrayList<Student> myList = new ArrayList<Student>();
  14.    
  15.      enum MenuOpt
  16.     {
  17.         ADD, REMOVE, SEARCH, PRINT, SAVE, LOAD, EXIT
  18.     }
  19.    
  20.     public static void main(String[] args)
  21.     {
  22.        
  23.         show_menu();
  24.     }
  25.  
  26.    
  27.     //options menu
  28.     public static void show_menu()
  29.     {
  30.         String opt="Exit";
  31.         do
  32.         {
  33.             System.out.println("\n\n");
  34.             for (MenuOpt meniu : EnumSet.allOf(MenuOpt.class))
  35.             {
  36.                 System.out.println(meniu);
  37.             }
  38.            
  39.             System.out.println("Type your choice as written in the menu above!");
  40.             opt=t.nextLine();
  41.            
  42.             MenuOpt mo = getEnumFromString(MenuOpt.class, opt);
  43.             System.out.println(mo);
  44.            
  45.             switch (mo)
  46.             {
  47.             case ADD:
  48.             {   add(); break; }
  49.            
  50.             case REMOVE:
  51.             {   remove(); break; }
  52.            
  53.             case SEARCH:
  54.             {   search(); break; }
  55.            
  56.             case PRINT:
  57.             {   print(); break; }
  58.            
  59.             case SAVE:
  60.             {   save(); break; }
  61.            
  62.             case LOAD:
  63.             {   load(); break; }
  64.            
  65.             default:
  66.             {   opt="Exit"; break; }
  67.            
  68.             }
  69.            
  70.         }while(true);
  71.     }
  72.    
  73.    
  74.     //menu methods
  75.    
  76.     public static void add()
  77.     {
  78.         String nume;
  79.         int age;
  80.         Float mark;
  81.        
  82.         System.out.println("\nType data for new Student:\nName: ");
  83.         nume=t.nextLine();
  84.         System.out.println("\nAge:");
  85.         age=t.nextInt();
  86.         System.out.println("\nMark");
  87.         mark=t.nextFloat();
  88.        
  89.         myList.add(new Student(nume, age, mark));
  90.         t.nextLine();
  91.         System.out.println("Student added!");
  92.     }
  93.    
  94.     public static void remove()
  95.     {
  96.         System.out.println("\nType name of Student to remove: ");
  97.         String name=t.nextLine();
  98.        
  99.         boolean found=false;
  100.        
  101.         for(Student s : myList)
  102.         {
  103.             if (s.getName().equals(name))
  104.             {
  105.                 myList.remove(s);
  106.                 System.out.println("Done!");
  107.                 found=true;
  108.                 break;
  109.             }
  110.         }
  111.         if (found==false)
  112.             System.out.println("Student not found!");
  113.     }
  114.    
  115.     public static String search()
  116.     {
  117.         String nume;
  118.         int age;
  119.         Float mark;
  120.        
  121.         System.out.println("\nType data for Student to search:\nName: ");
  122.         nume=t.nextLine();
  123.         System.out.println("\nAge:");
  124.         age=t.nextInt();
  125.         System.out.println("\nMark");
  126.         mark=t.nextFloat();
  127.        
  128.         Student myStudent = new Student(nume, age, mark);
  129.        
  130.         for(Student s : myList)
  131.         {
  132.             if (s.equals(myStudent))
  133.             {
  134.                 return "Student found!";
  135.             }
  136.         }
  137.         return "Student not found!";
  138.     }
  139.    
  140.     public static void print()
  141.     {
  142.         for(Student s : myList)
  143.         {
  144.             System.out.println(s);
  145.         }
  146.     }
  147.    
  148.     public static void save()
  149.     {
  150.         try
  151.         {
  152.             ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("date"));
  153.            
  154.             for(Student s : myList)
  155.             {
  156.                 out.writeObject(s);
  157.             }
  158.             out.close();
  159.         }
  160.         catch (Exception e)
  161.         {
  162.             e.printStackTrace();
  163.         }
  164.        
  165.        
  166.     }
  167.    
  168.     public static void load()
  169.     {
  170.         try
  171.         {
  172.             FileInputStream fis = new FileInputStream("date");
  173.             ObjectInputStream in = new ObjectInputStream(fis);
  174.             while (fis.available()>0 )
  175.             {
  176.                 myList.add((Student) in.readObject());
  177.             }
  178.             in.close();
  179.         }
  180.        
  181.         catch (Exception e)
  182.         {
  183.             e.printStackTrace();
  184.         }
  185.     }
  186.    
  187.  
  188.     //create enum from a string to switch it in the menu
  189.     public static <T extends Enum<T>> T getEnumFromString(Class<T> c, String string) {
  190.         if( c != null && string != null ) {
  191.             try {
  192.                 return Enum.valueOf(c, string.trim().toUpperCase());
  193.             } catch(IllegalArgumentException ex) {
  194.             }
  195.         }
  196.         return null;
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement