1. Engineering
  2. Computer Science
  3. complete the addpersonfromkeyboard and removeperson methods inside of the roster...

Question: complete the addpersonfromkeyboard and removeperson methods inside of the roster...

Question details

complete the addPersonFromKeyboard and removePerson methods inside of the Roster class.


ROSTER.JAVA

package classroster;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Roster {
   private int size;
   private static final int DEFAULT_SIZE = 10;
   private Person[] people;
   //private Student[] students;
   //private Instructor[] instructors;
   private String className;
   private String fileName;
  
   public Roster() {
       // default constructor
       size = 0;
       people = new Person[DEFAULT_SIZE];
       className = "a class";
       fileName = null;
   }
   public Roster(String cn, String fn) {
       // constructor with given course name and input file
       this();
       className = cn;
       fileName = fn;
       readFile();
   }  
   public void addStudent(Student s) {
       // method that adds the input Student to the people array
       if (size >= people.length)
           doubleArray();
       people[size] = s;
       size++;
   }
   public void addInstructor(Instructor i) {
       // method that adds the input Instructor to the people array
       if (size >= people.length)
           doubleArray();
       people[size] = i;
       size++;
   }  
   public void addPersonFromKeyboard () {
       // method that prompts the user to enter either a student or instructor
       // then the user inputs the appropriate information for that person type
       // finally adds the new person to the array
      
       // complete for homework #2
      
   }
   public void removePerson (String id) {
       // method that removes the Person from the array with the input id
      
       // complete for homework #2
      
   }
   private void doubleArray () {
       // private method used to double the size of the array when needed
       Person[] newPeople = new Person[people.length*2];
       for (int i = 0; i < size; i++) {
           newPeople[i] = people[i];
       }      
       people = newPeople;
   }
   public String toString() {
       // returns a string representation of this roster
       String toReturn = className+"\n";
       for (int i = 0; i < size; i++) {
           if (people[i] instanceof Student)
               toReturn += "Student: "+people[i]+"\n";
           else
               toReturn += "Instructor: "+people[i]+"\n";
       }
       return toReturn;
   }
   private void readFile () {
       // private method that reads the file and stores into people array
       BufferedReader lineReader = null;
       try {
           //BufferedReader lineReader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(fileName)));
           FileReader fr = new FileReader(fileName);
           lineReader = new BufferedReader(fr);
           String line = null;
           while ((line = lineReader.readLine())!=null) {
               String name = lineReader.readLine();
               String id = lineReader.readLine();
               if (line.equals("student")) {
                   String gpaString = lineReader.readLine();
                   addStudent(new Student(name,id,Double.parseDouble(gpaString)));
               }
               else if (line.equals("instructor")) {
                   String email = lineReader.readLine();
                   addInstructor(new Instructor(name,id,email));
               }
               else {
                   System.err.println("error: unnknown person type");
               }
           }
       } catch (Exception e) {
           System.err.println("there was a problem with the file. either no such file or format error");
       } finally {
           if (lineReader != null)
               try {
                   lineReader.close();
               } catch (IOException e) {
                   System.err.println("could not close BufferedReader");
               }
       }
   } // end of readFile method
  
   public void writeFile () {
       // overloaded method: this calls doWrite with file used to read data
       // use this for saving data between runs
       doWrite(fileName);
   } // end of writeFile method

   public void writeFile(String altFileName) {
       // overloaded method: this calls doWrite with different file name
       // use this for testing write
       doWrite(altFileName);      
   }// end of writeFile method
  
   private void doWrite(String fn) {
       // this method writes all of the data in the persons array to a file
       try
       {

           FileWriter fw = new FileWriter(fn);
           BufferedWriter myOutfile = new BufferedWriter(fw);          
          
           for (int i = 0; i < size; i++) {
               Person person = people[i];
               if (person instanceof Student) {
                   myOutfile.write ("student\n");
                   myOutfile.write (person.getName()+"\n");
                   myOutfile.write (person.getId()+"\n");
                   myOutfile.write (((Student) person).getGpa()+"\n");
               }
               else if (person instanceof Instructor) {
                   myOutfile.write ("instructor\n");
                   myOutfile.write (person.getName()+"\n");
                   myOutfile.write (person.getId()+"\n");
                   myOutfile.write (((Instructor) person).getEmail()+"\n");
               }  
               else {
                   System.err.println("error in array, instance type not found");
               }
           }
           myOutfile.flush();
           myOutfile.close();
       }
       catch (Exception e) {
           e.printStackTrace();
           System.err.println("Didn't save to " + fn);
       }
   }  
}
MAIN.JAVA

package classroster;

public class Main {

   public static void main(String[] args) {
       // test creating roster from scratch
       Student s1 = new Student();
//       System.out.println(s1);
       Student s2 = new Student("audrey","123",3.2);
//       System.out.println(s2);
       Roster myClass = new Roster();
       System.out.println(myClass);
       myClass.addStudent(s1);
       myClass.addStudent(s2);
      
       Instructor i1 = new Instructor();
       Instructor i2 = new Instructor("mark","987","");
       myClass.addInstructor(i1);
       myClass.addInstructor(i2);      
      
       System.out.println(myClass);
       System.out.println();
       System.out.println();
       System.out.println();
      
      
      
      
       // test creating roster from textfile

       Student s4 = new Student("audrey","123",3.2);
//       System.out.println(s2);
       Roster csci3381 = new Roster("CSCI3381","./classroster/data.txt");
       System.out.println("\ntest reading text file");
       System.out.println(csci3381);
       csci3381.addStudent(s4);
      
       Instructor i4 = new Instructor("mark","987","");
       csci3381.addInstructor(i4);      
      
       System.out.println("\ntest combined data from file and data added manually");
       System.out.println(csci3381);      
       csci3381.writeFile("./classroster/dataWriteTest.txt");
      
       Roster testWrite = new Roster("Test Write","./classroster/dataWriteTest.txt");
       System.out.println("\ntest reading just created file");
       System.out.println(testWrite);
   }

}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution