Advertisement
Guest User

Untitled

a guest
Oct 21st, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.41 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map.Entry;
  6.  
  7. import java.util.Scanner;
  8.  
  9. public class CsvDatabase implements java.io.Serializable {
  10.  
  11.     static public class Student implements java.io.Serializable {
  12.         public int id = 0;
  13.         public String firstName;
  14.         public String lastName;
  15.         public String age;
  16.         public String homeTown;
  17.  
  18.         public Student() {
  19.  
  20.         }
  21.  
  22.         public Student(int id, String firstName, String lastName, String age, String homeTown) {
  23.             this.id = id;
  24.             this.firstName = firstName;
  25.             this.lastName = lastName;
  26.             this.age = age;
  27.             this.homeTown = homeTown;
  28.         }
  29.  
  30.         public String ToString() {
  31.             return new String(firstName + " " + lastName + " (age: " + age + ", town: " + homeTown + ")");
  32.         }
  33.     }
  34.  
  35.     static public class Subject implements java.io.Serializable {
  36.  
  37.         public String subject;
  38.         public List<Double> grades = new ArrayList<Double>();
  39.  
  40.         public Subject() {
  41.  
  42.         }
  43.  
  44.         public Subject(String subject, String grade) {
  45.             this.subject = subject;
  46.             this.grades.add(Double.parseDouble(grade));
  47.         }
  48.  
  49.     }
  50.  
  51.     public static void main(String[] args) {
  52.         String input;
  53.         Scanner scan = new Scanner(System.in);
  54.  
  55.         HashMap<Integer, Student> students = new HashMap<Integer, Student>();
  56.         HashMap<String, ArrayList<Subject>> grades = new HashMap<String, ArrayList<Subject>>();
  57.         ArrayList<Subject> list = new ArrayList<Subject>();
  58.  
  59.         try (ObjectInputStream ios = new ObjectInputStream(new FileInputStream(new File("res/Students.save")));
  60.                 ObjectInputStream ios2 = new ObjectInputStream(new FileInputStream(new File("res/Grades.save")))) {
  61.             students = (HashMap<Integer, Student>) ios.readObject();
  62.             grades = (HashMap<String, ArrayList<Subject>>) ios2.readObject();
  63.             System.out.println("Successfully loaded data from files.");
  64.         } catch (IOException | ClassNotFoundException e) {
  65.             System.out.println("File error");
  66.         }
  67.  
  68.         while (true) {
  69.             input = scan.nextLine();
  70.             if (input.equals(new String("end"))) {
  71.                 break;
  72.             }
  73.             String[] command = input.split(" ");
  74.             switch (command[0]) {
  75.             case "Insert-student":
  76.                 students.put(students.size(),
  77.                         new Student(students.size(), command[1], command[2], command[3], command[4]));
  78.                 System.out.println("Student added");
  79.                 break;
  80.             case "Insert-grade-by-id":
  81.                 list.add(new Subject(command[2], command[3]));
  82.                 grades.put(command[1], list);
  83.                 System.out.println("Grade added");
  84.                 break;
  85.             case "Delete-by-id":
  86.                 if (students.containsKey(Integer.parseInt(command[1]))) {
  87.                     students.remove(Integer.parseInt(command[1]));
  88.                     System.out.println("Student removed");
  89.                     if (grades.containsKey(command[1])) {
  90.                         grades.remove(command[1]);
  91.                         System.out.println("And it's existing grades removed");
  92.                     }
  93.                 } else {
  94.                     System.out.println("Student does not exist");
  95.                 }
  96.             case "Search-by-id":
  97.                 if (students.containsKey(Integer.parseInt(command[1]))) {
  98.                     String studInfo = students.get(Integer.parseInt(command[1])).ToString();
  99.                     System.out.println(studInfo);
  100.                     if (grades.containsKey(command[1])) {
  101.                         List<Subject> gradeInfo = grades.get(command[1]);
  102.                         for (Subject s : gradeInfo) {
  103.                             System.out.printf(" # %s: ", s.subject);
  104.                             for (double d : s.grades) {
  105.                                 System.out.print(d);
  106.                             }
  107.                         }
  108.  
  109.                     }
  110.                 } else {
  111.                     System.out.println("Students does not exist");
  112.                 }
  113.             case "Search-by-full-name":
  114.                 for (Entry<Integer, Student> entry : students.entrySet()) {
  115.                     Integer key = entry.getKey();
  116.                     Student value = entry.getValue();
  117.                     if (value.firstName.equals(command[1]) && value.lastName.equals(command[2])) {
  118.                         String studInfo = students.get(key).ToString();
  119.                         if (grades.containsKey(key)) {
  120.                             List<Subject> gradeInfo = grades.get(key);
  121.                             for (Subject s : gradeInfo) {
  122.                                 System.out.printf(" # %s: ", s.subject);
  123.                                 for (double d : s.grades) {
  124.                                     System.out.print(d);
  125.                                 }
  126.                             }
  127.                         }
  128.                     }
  129.                 }
  130.             }
  131.         }
  132.         try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("res/Students.save")));
  133.                 ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(new File("res/Grades.save")))) {
  134.             oos.writeObject(students);
  135.             oos2.writeObject(grades);
  136.             System.out.println("Successfully saved data to files.");
  137.         } catch (IOException e) {
  138.             System.out.println("File error");
  139.         }
  140.     }
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement