next up previous
Next: Verifying: Does it Do Up: Week 9 Previous: Design Summary

Implementing the Design -- Translating Into Java

Source Code


import java.io.*;
public class BigEx2{
    public static void main(String[] args) throws IOException{
	Course c = CourseList.addCourse("CSC107");
	c.addSection("Ms. Smetana", "SS2220", "M900W900F900");
	c.addSection("Mr. Janacek", "SF2110", "M1300W1100R1500");

	c = CourseList.addCourse("CSC108");
	c.addSection("Ms. Winkelmeyer", "SS4332", "M900W900F900");
	c.addSection("Mr. Ogunsuyi", "SF4306", "M1300W1100R1500");
	c.addSection("Ms. Pacheco", "SS4132", "T1100W1000R1400");

	c = CourseList.addCourse("CSC148");
	c.addSection("Ms. Qureshi", "SF9732", "M900W900F900");
	c.addSection("Mr. Singh", "SF4327", "M1300W1100R1500");

	Io.mainLoop();
    }
}





class Course{
    public static final int MAX_SECTIONS = 10;

    private String name;
    private Section [] sectionArray = new Section[MAX_SECTIONS];   // All sections in course
    private int numSections = 0;

    public  Course(String cname) { name = cname; }
    public boolean equals(String cname) { return cname.equals(name); }  // equality when names equal
    public Section getSection(int num) { return sectionArray[num]; }       // sections numbered as in array
    public String getName() { return name; }
    
    public void addSection(String instName, String lroom, String ltime){
	sectionArray[numSections] = new Section(instName, lroom, ltime);
	numSections++;
    }

    /* Here we create an array, fill it with existing Section references and return the reference
       to the array.  Note that the array that we've created only needs to be as long as the number
       of existing Sections, rather than MAX_SECTIONS.  */

    public Section [] getSections(){                                                         
	Section [] availSecs = new Section[numSections];
	for (int i = 0; i < numSections; i++){
	    availSecs[i] = sectionArray[i];
	}
	return availSecs;
    }

    public int getEnrollment(){        // Add up the number of students in each section
	int numStuds = 0;
	for(int i = 0; i < numSections; i++){
	    numStuds += sectionArray[i].getNumStudents();
	}
	return numStuds;
    }

    public String toString(){          // Concatenate the toString() of all sections
	String s =name + numSections;
	for (int i = 0; i < numSections; i++)
	    s += sectionArray[i];
	return s;
    }
}





class CourseList{
    public static final int MAX_COURSES = 100;

    private static int numCourses = 0;
    private static Course [] courseArray = new Course[MAX_COURSES];

    public static int getNumCourses() { return numCourses; }

    public static Course addCourse(String name){
	if (numCourses >= MAX_COURSES) {
	    Io.error("Too many Courses: " + numCourses + " out of maximum "  
		     + MAX_COURSES + "offered.");	    
	    return null;
	}
	courseArray[numCourses] = new Course(name);
	numCourses++;
	return courseArray[numCourses-1];
    }

    public static Course getCourse(String cname){    // Look up course by its name and return reference
	
	for (int i = 0; i < numCourses; i++){
	    if (courseArray[i].equals(cname)) return courseArray[i];
	}
	return null;
    }
    
    public static Course getCourse(int i) { return courseArray[i]; }  // return reference to Course

    /* Create new array, fill it with references to courses, return reference to array */
       public static Course [] getList(){
	Course [] a = new Course[numCourses];
	for (int i = 0; i < numCourses; i++){
	    a[i] = courseArray[i];
	}
	return a;
    }
}





























class Io {

 private static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    public static void write(String p) { System.out.println(p); }
    public static void error(String e) { System.out.println("Error: " + e); }
    public static void fatalError(String e) { throw new RuntimeException(e); }

    public static String getString() throws IOException { return in.readLine(); }
    public static int getInteger() throws IOException { return Integer.parseInt( in.readLine());  }
	// should really do something more robust here, in case user types alpha chars.
 
    public static double getDouble() throws IOException { return Double.parseDouble( in.readLine());  }
	// should really do something more robust here, in case user types alpha chars.

    public static String askString(String a) throws IOException{  // Display prompt, return String
	write(a);
	return getString();
    }

    public static int askInteger(String a) throws IOException{   // Display prompt, return int
	write(a);
	return getInteger();
    }

    public static double askDouble(String a) throws IOException{ // Display prompt, return double
	write(a);
	return getDouble();
    }

    public static void  mainMenu(){   // Display user options
	write("Choose one of the following: ");
	write("0 Change/Display Student Infomation");
	write("1 Display Administrative Information");
	write("2 Quit");
    }  

    public static void mainLoop() throws IOException{  // Process user options
	boolean finished = false;
 	while (!finished){
	    mainMenu();
	    int choice = askInteger("Type an integer");
	    if (choice == 0){
		studentLoop();
	    } else if (choice == 1){
		adminLoop();
	    }else if (choice == 2){
		finished = true;
	    } 
	}
    }

    public static void studentMenu(){  // Display submenu
	write("0 Input Student Personal Information");
	write("1 Display Student Information");
	write("2 Pay Fees");
	write("3 Change Address");
	write("4 Add course");
	write("5 Back to Main Menu");
    }

 public static void studentLoop()throws IOException{
	boolean finished = false;
	while (!finished){
	    studentMenu();
	    int choice = askInteger("Type an integer");
	    if (choice == 0){  // Create new Student object, display assigned student number
		write("Your student number is: " +  
		      StudentList.addStudent(askString("Name?"), 
					     askString("Date of Birth?"),
					     askString("Address?")));
	       
	    } else if (choice == 1){  // Get student number, display student's information
		showInfo(StudentList.getStudent(askInteger("Student Number?")));
	    } else if (choice == 2){  // Get student number, display fees owing, pay fees
		Student s = StudentList.getStudent(askInteger("Student Number?"));
		showFeesOwing(s);
		s.payFees(askDouble("Amount to pay?"));
		showFeesOwing(s);
	    } else if (choice == 3){  // Get student number, display address, get new address
		Student s = StudentList.getStudent(askInteger("Student Number?"));
		showAddress(s);
		s.setAddress(askString("New address?"));
		showAddress(s);

		/* register for courses.  For each course selected, let student see list of sections,
		   so they can choose one.  Add the student to the section, and the section to the
		   student */
	    } else if (choice == 4){ 
		Student s = StudentList.getStudent(askInteger("Student Number?"));
		String cprompt = "Input course codes.  Hit return to end." ;
		String cs = askString(cprompt);
		while(!cs.equals("")){
		    Course c = CourseList.getCourse(cs);
		    if (c!=null){
			c.toString();
			showInfo(c.getSections());
			int secnum = askInteger("Choose a section");
			(c.getSection(secnum)).addStudent(s);
			s.addCourse(c, secnum);
		    }else{
			error("Course doesn't exist");
		    }
		    cs =  askString(cprompt);
		}
	    } else if (choice == 5){
		finished = true;
	    }
	}
    }
    
    public static void adminMenu(){  // Administrative sub menu
	write("0 Display all student names and numbers ");
	write("1 Display course names and enrollment");
	write("2 Display instructor names");
	write("3 Display total/average owed university");
	write("4 Back to Main Menu");
   }

    public static void adminLoop() throws IOException{  // Process administrative options
	boolean finished = false;
 	while (!finished){
	    adminMenu();
	    int choice = askInteger("Type an integer");
	    if (choice == 0){  // Display names and student numbers of all students
		for (int i = 0; i < StudentList.getNumStudents(); i++){
		    Student s = StudentList.getStudent(i);
		    write(s.getName() + " " + s.getStudentNumber());
		}
	    } else if (choice == 1){  // Displsy name an enrollment of all courses
		Course [] a = CourseList.getList();
		for (int i = 0; i < CourseList.getNumCourses(); i++){
		    write(a[i].getName() + " " +  a[i].getEnrollment());
		}

	    }else if (choice == 2){ // Display name of instructor for each section
		for (int i = 0; i < CourseList.getNumCourses(); i++){
		    Course c = CourseList.getCourse(i);
		    Section [] s = c.getSections();
		    for(int j = 0; j < s.length; j++){
			write(s[j].getInstructor());
		    }
		}
	    } else if (choice == 3){  // Show how much money is owed university
		double totalOwed = 0.;
		int nStudents = StudentList.getNumStudents();
		for (int i = 0; i < nStudents; i++){
		    Student s = StudentList.getStudent(i);
		    totalOwed += s.getFeesOwing();
		}
		write("Total: "+totalOwed+" Average: "+totalOwed/nStudents);
	    } else if (choice == 4){
		finished = true;
	    }
	}
    }
    
    private static void showName(Student s){ write("Name: " + s.getName()); }
    private static void showSnum(Student s){ write("Student Number " + s.getStudentNumber()); }
    private static void showDob(Student s){ write("Date of Birth " + s.getDob()); }
    private static void showAddress(Student s){ write("Address " + s.getAddress()); }
    private static void showFeesOwing(Student s){ write("Fees Owing: " + s.getFeesOwing()); }
    private static void showCourses(Student s){ write("Courses: " + s.getCourses()); }

    public static void showInfo(Student s){  // Display student information
	if (s==null){ 
	    error("no such student.");
	}else{
	    showName(s);
	    showSnum(s);
	    showDob(s);
	    showAddress(s);
	    showFeesOwing(s);
	    showCourses(s);
	}
    }

    public static void showInfo(Section [] sec){  // Display section information
	for (int i = 0; i < sec.length; i++){
	    write("Section " + i + ":");
	    write("Instructor " + sec[i].getInstructor());
	    write("Lecture Room " + sec[i].getRoom());
	    write("Lecture Times " + sec[i].getTimes());
	}	    
    }

}













































class Section{
    public static final int MAX_STUDENTS_IN_SECTION = 100;

    private Student [] studentArray = new Student[MAX_STUDENTS_IN_SECTION];
    private int numStudents = 0;
    private int capacity = 20;
    private String instructor = "";
    private String lectureRoom = "";
    private String lectureTimes = "";

    public Section(String n, String r, String t){
	instructor = n;
	lectureRoom = r;
	lectureTimes = t;
    }

    public int getNumStudents() { return numStudents; }
    public String getInstructor() { return instructor; }
    public String getRoom() { return lectureRoom; }
    public String getTimes() { return lectureTimes; }
    public int  getAvailableSpots() { return capacity - numStudents; }

    public void addStudent(Student s){
	studentArray[numStudents] = s;
	numStudents++;
    }

    public String toString(){
	String s = instructor + lectureRoom + lectureTimes;
	for (int i = 0; i < numStudents; i++){
	    s += studentArray[i];
	}
	return s;
    }
}








































class Student{

    public static final int MAX_COURSES = 7;

    private String name;
    private int snum;
    private String dob;
    private String address;
    private double feesOwing = 5000.00;

    private Course [] course = new Course[MAX_COURSES];
    private Section [] section = new Section[MAX_COURSES];
    private int numCourses = 0;

    public Student(String n, int sn, String bd, String a){
	name = n;
	snum = sn;
	dob = bd;
	address = a;
    }

    public double getFeesOwing() { return feesOwing; }
    public void payFees(double someamount) { feesOwing -= someamount; }
    public int getNumCourses() { return numCourses; }
    public void setName(String somename) { name = somename; }
    public String getName() { return name; }
    public void setDob(String bd) { dob = bd; }
    public String getDob() { return dob; }
    public int getStudentNumber(){ return snum; }
    public boolean equals(int n){ return n == snum;}
    public String getAddress() {return address;}
    public void setAddress(String  v) {address = v;}
    public String toString() { return name + snum + dob + address + feesOwing; }

    public void  addCourse(Course c, int secnum){
	course[numCourses] = c;
	section[numCourses] = c.getSection(secnum);
	numCourses++;
    }

    public String getCourses(){
	String cinfo = "";
	for(int i = 0; i < numCourses; i++){
	    cinfo += course[i].getName() + " "+ section[i].getInstructor()  
		+ " " + section[i].getTimes() + "      ";
	}
	return cinfo;
    }
}









class StudentList{
    public static final int MAX_STUDENTS = 1000;

    private static int numStudents = 0;
    private static Student [] studentArray = new Student[MAX_STUDENTS];

    public static int getNumStudents() { return numStudents; }
    
    public static Student getStudent(int snum){  // Find Student object using student number, return reference
	for(int i = 0; i < numStudents; i++){
	    if(studentArray[i].equals(snum)) return studentArray[i];
	}
	return null;
    }

    public static int addStudent(String name, String dob, String address){
	if (numStudents >= MAX_STUDENTS) {
	    Io.error("Too many students: " + numStudents + " out of maximum " 
		     + MAX_STUDENTS + "enrolled.");	    
	    return -1;
	}
	studentArray[numStudents] = new Student(name, numStudents, dob, address);
	numStudents++;
	return numStudents-1;
    }
}



Chris Trendall
Copyright ©Chris Trendall, 2001. All rights reserved.

2001-12-09