Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package ass;
- /**
- *
- * @author NgT
- */
- import java.io.*;
- import java.util.*;
- public class Ass {
- public void listAllStudent() {
- File file = new File("D:\\Assignment.std");
- ArrayList<Student> std = new ArrayList<>();
- int count = 0;
- std = readAll(file);
- for (Student s : std) {
- count++;
- System.out.println(count + ".ID: " + s.getId() + " Name: " + s.getName() + " Score: " + s.getScore());
- }
- }
- public void addNewStudent() {
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter Student Id: ");
- int Id = Integer.parseInt(scanner.nextLine());
- System.out.println("Enter Student name: ");
- String name = scanner.nextLine();
- System.out.println("Enter Student score: ");
- float score = Float.parseFloat(scanner.nextLine());
- Student student = new Student(Id, name, score);
- String std = student.getId() + "|" + student.getName() + "|" + student.getScore() + "\r\n";
- try {
- String filename = "D:\\Ass.std";
- File file = new File(filename);
- RandomAccessFile raf = new RandomAccessFile(file, "rw");
- raf.seek(raf.length());
- raf.writeBytes(std);
- raf.close();
- } catch (Exception e) {
- System.out.println(e.getMessage());
- }
- }
- public ArrayList<Student> readAll(File n) {
- ArrayList arrStudent = new ArrayList();
- try {
- RandomAccessFile file = new RandomAccessFile(n, "r");
- String r = "";
- while ((r = file.readLine()) != null) {
- Student std = new Student();
- String[] str = r.split("\\|");
- std.setId(Integer.parseInt(str[0].toString()));
- std.setName(str[1].toString());
- std.setScore(Float.parseFloat(str[2].toString()));
- arrStudent.add(std);
- }
- file.close();
- } catch (IOException | NumberFormatException ex) {
- System.out.println(ex.getMessage());
- }
- return arrStudent;
- }
- public void searchID() {
- System.out.println("Input student Id: ");
- Scanner sc = new Scanner(System.in);
- int id = Integer.parseInt(sc.nextLine());
- File file = new File("D:\\Ass.std");
- ArrayList<Student> std = new ArrayList<>();
- boolean se = true;
- std = readAll(file);
- for (Student s : std) {
- if (id == s.getId()) {
- System.out.println("ID: " + s.getId() + "\nName: " + s.getName() + "\nScore: " + s.getScore());
- se = false;
- break;
- }
- }
- if (se) {
- System.out.println("Student not found!!");
- }
- }
- public void removeID() {
- System.out.println("Input student Id: ");
- Scanner sc = new Scanner(System.in);
- int id = Integer.parseInt(sc.nextLine());
- File file = new File("D:\\Ass.std");
- ArrayList<Student> std = new ArrayList<>();
- boolean rm = false;
- for (Student s : std) {
- if (id == s.getId()) {
- int choice;
- System.out.println("Are you sure deleting this student? (1.Yes 2.No");
- choice = new Scanner(System.in).nextInt();
- if (choice == 1) {
- std.remove(s);
- }
- rm = true;
- }
- }
- if (rm == false) {
- System.out.println("Can not find student with id ");
- }
- }
- public void displayAd() {
- System.out.println("1. Add new students");
- System.out.println("2. Search for student based on his/her id");
- System.out.println("3. List all of the students");
- System.out.println("4. Delete student based on student id");
- System.out.println("Choose option: ");
- String input;
- Scanner scan = new Scanner(System.in);
- input = scan.nextLine();
- switch (input) {
- case "1":
- addNewStudent();
- break;
- case "2":
- searchID();
- break;
- case "3":
- listAllStudent();
- break;
- case "4":
- removeID();
- break;
- default:
- break;
- }
- }
- public static void main(String[] args) {
- // TODO code application logic here
- Ass ass = new Ass();
- ass.displayAd();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment