// Simple encapsulation in Java with simple terminal I/O import java.io.*; class Student { private String ssn; private String name; private int age; public String get_ssn () { return ssn; } public String get_name () { return name; } public long get_age () { return age; } public void set_ssn (String new_ssn) { ssn = new String(new_ssn); } public void set_name (String new_name) { name = new String(new_name); } public void set_age (int new_age) { if (new_age > 0 && new_age <= 110) age = new_age; else System.err.println("Age is not legal"); } // Provide a constructor for Student for proper initialization Student (String sn, String s, int y) { set_ssn(sn); set_name(s); set_age(y); } // display function public void display () { System.err.println("Data for Student with Ssn: " + ssn); System.err.println("\tName: " + name + " Age: " + age); } } class Database { private Student Students[]; Database (int max) { Students = new Student[max]; } private int num_Students = 0; // function to locate a Student on the list; return -1 if not there public int find_Student (String sn) { for (int i = 0; i < num_Students; i++) // note the comparison below does NOT use == if ((Students[i].get_ssn()).equals(sn)) return i; return -1; } // function to add a new Student to the list; if duplicate (sn) // reject with an error message public void add_Student (Student x) { if (num_Students == Students.length) { System.err.println("Error -- database is full"); return; } int there = find_Student(x.get_ssn()); if (there != -1) { System.err.println("Error -- that ssn number exists already"); return; } Students[num_Students] = x; num_Students = num_Students + 1; } // function to remove a Student; use list compaction if you wish // error message if sn is not on the list public void remove_Student (String sn) { int there = find_Student(sn); if (there == -1) { System.err.println("Cannot delete -- not on the list"); return; } // compaction of remaining elements... for (int i=there+1; i < num_Students; i++) Students[i-1] = Students[i]; num_Students = num_Students - 1; } // function to display a Student if it is on the list public void display_Student (String sn) { int there = find_Student(sn); if (there == -1) { System.err.println("Cannot display -- not on the list"); return; } Students[there].display(); } public void display_all() { for (int i = 0; i < num_Students; i++) Students[i].display(); } } class StudentDB { public static void main (String args[]) throws IOException { Database D1 = new Database(100); String m, inputstring; String sn; int y; String com; //DataInputStream ds = new DataInputStream(System.in); BufferedReader din = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter command: "); com = din.readLine().trim(); while (! com.equalsIgnoreCase("QUIT")) { if (com.equalsIgnoreCase("ADD")) { System.out.print("Enter ssn: "); sn = din.readLine(); System.out.print("Enter name: "); m = din.readLine(); System.out.print("Enter age: "); inputstring = din.readLine(); y = Integer.parseInt(inputstring); D1.add_Student(new Student(sn, m, y)); } else if (com.equalsIgnoreCase( "REMOVE")) { System.out.print("Enter ssn: "); sn = din.readLine(); D1.remove_Student(sn); } else if (com.equalsIgnoreCase("DISPLAY")) { System.out.print("Enter ssn: "); sn = din.readLine(); D1.display_Student(sn); } else if (com.equalsIgnoreCase("SHOWALL")) { D1.display_all(); } else System.out.println("Error case"); System.out.print("Enter next command: "); com = din.readLine(); } } }