Advertisement
pexea12

[Thang] Student

Feb 23rd, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. public class StudentList {
  2.  
  3.     private ArrayList<Student> students = new ArrayList<Student>();
  4.     private File file;
  5.    
  6.     private ArrayList<Student> readAll() {
  7.         ArrayList arrStudent = new ArrayList();
  8.         try {
  9.             RandomAccessFile randomAccess = new RandomAccessFile(file, "r");
  10.             String r = "";
  11.             while ((r = file.readLine()) != null) {
  12.                 Student std = new Student();
  13.                 String[] str = r.split("\\|");
  14.                 std.setId(Integer.parseInt(str[0].toString()));
  15.                 std.setName(str[1].toString());
  16.                 std.setScore(Float.parseFloat(str[2].toString()));
  17.                 arrStudent.add(std);
  18.             }
  19.             randomAccess.close();
  20.         } catch (IOException | NumberFormatException ex) {
  21.             System.out.println(ex.getMessage());
  22.         }
  23.         return arrStudent;
  24.     }
  25.  
  26.     public StudentList() {
  27.           file = new File("D:\\Ass.std");
  28.           students = readAll();
  29.     }
  30.  
  31.     public StudentList(string directory) {
  32.          file = new File(directory);
  33.          students = readAll();
  34.     }
  35.  
  36.     public void list() {
  37.         int count = 0;
  38.         for (Student s : std) {
  39.             System.out.println((++count) + ".ID: " + s.getId() + " Name: " + s.getName() + " Score: " + s.getScore());
  40.  
  41.         }
  42.     }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement