/** * Jeg har skrevet litt på dette prosjektet etter å ha arbeidet med modul 9. * Jeg har innført superklassen person, slik at jeg med subklassene kan skille * mellom lærere og studenter. Dette tas hensyn til i noen lab-class-metoder * ved at jeg bruker "instanceof"-metoden. Jeg har også lagt til litt * interaktivitet (inspirert av zuul-spillet). * Dette kan testes ved å prøve å legge til flere studenter i en full klasse. * * Jeg bruker i dette prosjektet flere av metodene som sto under læremåla * for modulen, men ikke alle. Jeg har også jobbet med zuul-spillet mens jeg har * jobbet med arv, men jeg ønsket ikke å levere dette prosjektet enda ettersom * det er blitt ganske omfattende og krever litt forklaring og finpuss. * * Ellers har jeg i modul ni lært om "substitution". Det vil si at ved å bruke * super- og subklasser kan en lagre forskjellige objekter i variabler. * Under har jeg noen utsagn fra dette prosjektet for å illustrere: * Person p1 = new Student(); - dette ville være en godkjent kommando, for person kan lagre alle typer * personer, inkl. subklassene student og teacher * Person p2 = new Person(); - dette er også en godkjent kommando, for person er en person * Teacher t1 = new Person(); - dette er derimot IKKE en godkjent kommando, for en ny person kan være både person, * teacher og student. Dermed passer ikke dette nødvendigvis inn i Teacher-variabelen t1. * *Dersom dette ikke er nok for å få godkjent modul 9 kan jeg selvsagt *utvide eller levere deler av zuul-prosjektet mitt, skrive mer utfyldende tekst *eller ta en tur innom kontoret en dag. :) * *@author Arild Høyland *@version 0.3 * * ----------------------------------------------------------------------------------------------------------------- *The LabClass class represents an enrolment list for one lab class. It stores * the time, room and participants of the lab, as well as the instructor's name. * * @author Michael Kölling and David Barnes * @version 2011.07.31 */ import java.util.*; public class LabClass extends School { private String room; private String time; private int capacity; private Person teacher; private String className = "Biology 101"; /** * Create a LabClass with a maximum number of enrolments. All other details * are set to default values. * @param maxNumberOfStudents student capacity for this class */ public LabClass(int maxNumberOfStudents) { super(); room = "unknown"; time = "unknown"; students = new ArrayList(); capacity = maxNumberOfStudents; } /** * Enrolls a person in this LabClass. * @param person (student) to be added to this class */ public String enrollStudent(Person person) { if (person instanceof Student) { if (students.size() >= capacity) { return"The class is full, you cannot enroll. Do you wish to expand the class?"; } else { students.add(person); return"Student ' " + person.getName() + "' added to class."; } } else { return"This isn't a student. You can only enroll students."; } } /** * Enrolls a student in a class, after having changed capacity using setCapacityText(). * Used in enrollStudent(Person person) * @param person - person to be enrolled in class */ private void enrollStudentText(Person person) { boolean finished = false; while(!finished) { Scanner inputStudent = new Scanner(System.in); System.out.println("Do you wish to add the student to the class?"); String svarStudent = inputStudent.nextLine().toLowerCase(); if (svarStudent.equals("yes")) { students.add(person); System.out.println("Student '" + person.getName() + " has been added to the class. Number of students in class is now " + students.size() + ".\n Application terminated."); finished = true; } else if(svarStudent.equals("no")) { System.out.println("Student has NOT been added to the class. Number of students in class is now: " + students.size() + ".\n Application terminated."); finished = true; } else { System.out.println("I could not understand you. Please reply either 'yes' or 'no'."); } } } /** * Return the number of students currently enrolled in this LabClass. * @return number of students in class */ public int numberOfstudents() { return students.size(); } /** * Change capacity of the class * @param capacity max number of students in class */ public void setCapacity(int capacity) { this.capacity = capacity; } /** * Get capacity of this class * @return max number of students in class */ public int getCapacity() { return capacity; } /** * Set the room number for this LabClass. * @param roomNumber sets room for this class */ public void setRoom(String roomNumber) { room = roomNumber; } /** * Gets the room this class is in * @return room this class is held in */ public String getRoom() { return room; } /** * Set the time for this LabClass. The parameter should define the day * and the time of day, such as "Friday, 10am". * @param timeAndDayString sets the time and day of this class */ public void setTime(String timeString) { time = timeString; } /** * Gets the time and day of the class in the format: DAY, HH:MM * @return day and time of class */ public String getTime() { return time; } /** * Gets the name of this class * @return returns name of class */ public String getClassName() { return className; } /** * Sets name of this class * @param string with new name of class */ public void setClassName(String className) { this.className = className; } public void printStudents() { for(Person person : students) { person.print(); } } /** * Print out a class list with other LabClass details to the standard * terminal. */ public void printList() { System.out.println("Lab class: " + room); System.out.println("Schedule: " + time); System.out.println("Class capacity: " + capacity); if(this.teacher == null) { System.out.println("No teacher assigned!"); } else { System.out.println("Teacher: " + this.teacher.getName()); } System.out.println("Class list:"); for(Person person : students) { person.print(); } System.out.println("Number of students: " + numberOfstudents()); } /** * Adds a teacher to this class * @param person teacher to add to this class */ public void addTeacher(Person person) { if(person instanceof Teacher) { if(this.teacher != null) { boolean finished = false; while(finished != true) { Scanner input = new Scanner(System.in); System.out.println("\nThere is already a teacher assigned, do you wish to replace this teacher?"); String svar = input.nextLine().toLowerCase(); if(svar.equals("yes")) { addTeacherText(person); finished = true; } else if(svar.equals("no")) { finished = true; System.out.println("Teacher has not been changed. Application terminated."); } else { System.out.println("I could not understand you. Please reply either 'yes' or 'no'."); } } } else { this.teacher = person; } } else { System.out.println("This isn't a teacher. You can only add teachers."); } } /** * Adds teacher by asking the user for an input in text format * Used (called by) addTeacher(Person person) * @param person teacher to be added to this class */ private void addTeacherText(Person person) { this.teacher = person; System.out.println("Teacher has been changed. New teacher is " + person.getName() + ". Application terminated."); } public void removeTeacher() { teacher = null; } }