Nguythang

Course

Feb 23rd, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ass;
  7.  
  8. /**
  9.  *
  10.  * @author NgT
  11.  */
  12. import java.io.*;
  13. import java.util.*;
  14.  
  15. public class Ass {
  16.  
  17.     public void listAllStudent() {
  18.         File file = new File("D:\\Assignment.std");
  19.  
  20.         ArrayList<Student> std = new ArrayList<>();
  21.         int count = 0;
  22.         std = readAll(file);
  23.         for (Student s : std) {
  24.             count++;
  25.             System.out.println(count + ".ID: " + s.getId() + " Name: " + s.getName() + " Score: " + s.getScore());
  26.  
  27.         }
  28.     }
  29.  
  30.     public void addNewStudent() {
  31.  
  32.         Scanner scanner = new Scanner(System.in);
  33.         System.out.println("Enter Student Id: ");
  34.         int Id = Integer.parseInt(scanner.nextLine());
  35.         System.out.println("Enter Student name: ");
  36.         String name = scanner.nextLine();
  37.         System.out.println("Enter Student score: ");
  38.         float score = Float.parseFloat(scanner.nextLine());
  39.         Student student = new Student(Id, name, score);
  40.  
  41.         String std = student.getId() + "|" + student.getName() + "|" + student.getScore() + "\r\n";
  42.  
  43.         try {
  44.             String filename = "D:\\Ass.std";
  45.             File file = new File(filename);
  46.             RandomAccessFile raf = new RandomAccessFile(file, "rw");
  47.  
  48.             raf.seek(raf.length());
  49.  
  50.             raf.writeBytes(std);
  51.  
  52.             raf.close();
  53.  
  54.         } catch (Exception e) {
  55.             System.out.println(e.getMessage());
  56.         }
  57.     }
  58.  
  59.     public ArrayList<Student> readAll(File n) {
  60.         ArrayList arrStudent = new ArrayList();
  61.         try {
  62.             RandomAccessFile file = new RandomAccessFile(n, "r");
  63.             String r = "";
  64.             while ((r = file.readLine()) != null) {
  65.                 Student std = new Student();
  66.                 String[] str = r.split("\\|");
  67.                 std.setId(Integer.parseInt(str[0].toString()));
  68.                 std.setName(str[1].toString());
  69.                 std.setScore(Float.parseFloat(str[2].toString()));
  70.                 arrStudent.add(std);
  71.             }
  72.             file.close();
  73.         } catch (IOException | NumberFormatException ex) {
  74.             System.out.println(ex.getMessage());
  75.         }
  76.         return arrStudent;
  77.     }
  78.  
  79.     public void searchID() {
  80.         System.out.println("Input student Id: ");
  81.         Scanner sc = new Scanner(System.in);
  82.         int id = Integer.parseInt(sc.nextLine());
  83.         File file = new File("D:\\Ass.std");
  84.  
  85.         ArrayList<Student> std = new ArrayList<>();
  86.         boolean se = true;
  87.         std = readAll(file);
  88.         for (Student s : std) {
  89.             if (id == s.getId()) {
  90.                 System.out.println("ID: " + s.getId() + "\nName: " + s.getName() + "\nScore: " + s.getScore());
  91.                 se = false;
  92.                 break;
  93.             }
  94.         }
  95.         if (se) {
  96.             System.out.println("Student not found!!");
  97.         }
  98.     }
  99.  
  100.     public void removeID() {
  101.         System.out.println("Input student Id: ");
  102.         Scanner sc = new Scanner(System.in);
  103.         int id = Integer.parseInt(sc.nextLine());
  104.         File file = new File("D:\\Ass.std");
  105.  
  106.         ArrayList<Student> std = new ArrayList<>();
  107.         boolean rm = false;
  108.        
  109.         for (Student s : std) {
  110.             if (id == s.getId()) {
  111.                 int choice;
  112.                 System.out.println("Are you sure deleting this student? (1.Yes  2.No");
  113.                 choice = new Scanner(System.in).nextInt();
  114.                 if (choice == 1) {
  115.                     std.remove(s);
  116.                 }
  117.                 rm = true;
  118.             }
  119.         }
  120.         if (rm == false) {
  121.             System.out.println("Can not find student with id ");
  122.         }
  123.     }
  124.  
  125.     public void displayAd() {
  126.  
  127.         System.out.println("1. Add new students");
  128.         System.out.println("2. Search for student based on his/her id");
  129.         System.out.println("3. List all of the students");
  130.         System.out.println("4. Delete student based on student id");
  131.         System.out.println("Choose option: ");
  132.         String input;
  133.         Scanner scan = new Scanner(System.in);
  134.         input = scan.nextLine();
  135.         switch (input) {
  136.             case "1":
  137.                 addNewStudent();
  138.                 break;
  139.             case "2":
  140.                 searchID();
  141.                 break;
  142.             case "3":
  143.                 listAllStudent();
  144.                 break;
  145.             case "4":
  146.                 removeID();
  147.                 break;
  148.             default:
  149.                 break;
  150.         }
  151.  
  152.     }
  153.  
  154.     public static void main(String[] args) {
  155.         // TODO code application logic here
  156.         Ass ass = new Ass();
  157.         ass.displayAd();
  158.     }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment