Nguythang

studentList

Feb 23rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 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. import java.io.File;
  9. import java.io.IOException;
  10. import java.io.RandomAccessFile;
  11. import java.util.ArrayList;
  12. import java.util.Scanner;
  13.  
  14. /**
  15.  *
  16.  * @author NgT
  17.  */
  18. public class StudentList {
  19.  
  20.  
  21.     public void listAllStudent() {
  22.         File file = new File("D:\\Ass.std");
  23.  
  24.         ArrayList<Student> std = new ArrayList<>();
  25.         int count = 0;
  26.         std = readAll(file);
  27.         for (Student s : std) {
  28.             count++;
  29.             System.out.println(count + ":" + "\n" + "ID: " + s.getId() + "\n" + "Name: " + s.getName() + "\n" + "Score: " + s.getScore() + "\n");
  30.  
  31.         }
  32.     }
  33.  
  34.     public void addNewStudent() {
  35.  
  36.         Scanner scanner = new Scanner(System.in);
  37.         System.out.println("Enter Student Id: ");
  38.         int Id = Integer.parseInt(scanner.nextLine());
  39.         System.out.println("Enter Student name: ");
  40.         String name = scanner.nextLine();
  41.         System.out.println("Enter Student score: ");
  42.         float score = Float.parseFloat(scanner.nextLine());
  43.         Student student = new Student(Id, name, score);
  44.  
  45.         String std = student.getId() + "|" + student.getName() + "|" + student.getScore() + "\r\n";
  46.  
  47.         try {
  48.             String filename = "D:\\Ass.std";
  49.             File file = new File(filename);
  50.             try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {
  51.                 raf.seek(raf.length());
  52.  
  53.                 raf.writeBytes(std);
  54.             }
  55.  
  56.         } catch (Exception e) {
  57.             System.out.println(e.getMessage());
  58.         }
  59.     }
  60.  
  61.     public ArrayList<Student> readAll(File n) {
  62.         ArrayList arrStudent = new ArrayList();
  63.         try {
  64.             try (RandomAccessFile file = new RandomAccessFile(n, "r")) {
  65.                 String r = "";
  66.                 while ((r = file.readLine()) != null) {
  67.                     Student std = new Student();
  68.                     String[] str = r.split("\\|");
  69.                     std.setId(Integer.parseInt(str[0]));
  70.                     std.setName(str[1]);
  71.                     std.setScore(Float.parseFloat(str[2]));
  72.                     arrStudent.add(std);
  73.                 }
  74.             }
  75.         } catch (IOException | NumberFormatException ex) {
  76.             System.out.println(ex.getMessage());
  77.         }
  78.         return arrStudent;
  79.     }
  80.  
  81.     public void searchID() {
  82.         System.out.println("Input student Id: ");
  83.         Scanner sc = new Scanner(System.in);
  84.         int id = Integer.parseInt(sc.nextLine());
  85.         File file = new File("D:\\Ass.std");
  86.  
  87.         ArrayList<Student> std = new ArrayList<>();
  88.         boolean se = true;
  89.         std = readAll(file);
  90.         for (Student s : std) {
  91.             if (id == s.getId()) {
  92.                 System.out.println("ID: " + s.getId() + "\nName: " + s.getName() + "\nScore: " + s.getScore());
  93.                 se = false;
  94.                 break;
  95.             }
  96.         }
  97.         if (se) {
  98.             System.out.println("Student not found!!");
  99.         }
  100.     }
  101.  
  102.     public void deleteID() {
  103.         System.out.println("Input student Id: ");
  104.         Scanner sc = new Scanner(System.in);
  105.         int id = Integer.parseInt(sc.nextLine());
  106.         File file = new File("D:\\Ass.std");
  107.  
  108.         ArrayList<Student> std = new ArrayList<>();
  109.         boolean rm = true;
  110.  
  111.         for (Student s : std) {
  112.             if (id == s.getId()) {
  113.                 int choice;
  114.                 System.out.println("Are you sure deleting this student? (1.Yes  2.No");
  115.                 choice = new Scanner(System.in).nextInt();
  116.                 if (choice == 1) {
  117.                     std.remove(s);
  118.                 }
  119.                 rm = false;
  120.             }
  121.         }
  122.         if (rm) {
  123.             System.out.println("Can not find student with id ");
  124.         }
  125.     }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment