Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hello, I hope this program is what you wanted. I commented the code like you asked me to. Thank you for teaching me this semester, I've learned a lot! - Daniel Valcour
- //First step, import the custom exceptions.
- import CustomExceptions.CourseAlreadyExistsException;
- import CustomExceptions.SectionAlreadyExistsException;
- import CustomExceptions.SectionInvalidException;
- import CustomExceptions.MaxSectionsException;
- //now import the stuff built into the JDK that I need
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.GregorianCalendar;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- //create the Test class
- public class Test {
- //Define the method, and use throws to prepare for the exceptions
- public static void main(String[] args) throws IOException,InputMismatchException, CourseAlreadyExistsException, SectionAlreadyExistsException, MaxSectionsException {
- //The following is not used anymore! Please ignore.
- //DataStore dataStore = DataStore.getInstance();
- //Section[] sections = dataStore.getSections();
- //Get an instance of the singleton FileReader class.
- FileReader fileReader = FileReader.getInstance();
- //Get the sections from the fileReader.
- Section[] sections = fileReader.getSections();
- //If the output sections are not null
- if (sections[0] != null) {
- //use a boolean for the main while loop for the program.
- boolean continueProgram = true;
- //while the boolean is true...
- while (continueProgram) {
- //begin the outermost try block, to catch InvalidInputExceptions when the user is typing in information.
- try {
- //initialize variables
- String firstName;
- String lastName;
- String socialSecurityNumber;
- String street;
- String city;
- String state;
- String zipCode;
- GregorianCalendar dob;
- Section[] sectionList = new Section[0];
- String studentID;
- int month;
- int day;
- int year;
- Scanner scanner1 = new Scanner(System.in);
- //Title text in the console
- System.out.println("Welcome to the Course Chooser Program!!!");
- //Ask for student info, and store it in variables
- System.out.println("Please enter your student ID.");
- System.out.println("");
- studentID = scanner1.nextLine();
- System.out.println("Please enter your first name.");
- System.out.println("");
- firstName = scanner1.nextLine();
- System.out.println("Please enter your last name.");
- System.out.println("");
- lastName = scanner1.nextLine();
- System.out.println("Please enter your social security number.");
- System.out.println("");
- socialSecurityNumber = scanner1.nextLine();
- System.out.println("Please enter your street.");
- System.out.println("");
- street = scanner1.nextLine();
- System.out.println("Please enter your city.");
- System.out.println("");
- city = scanner1.nextLine();
- System.out.println("Please enter your state acronym.");
- System.out.println("");
- state = scanner1.nextLine();
- System.out.println("Please enter your 5-digit zip code.");
- System.out.println("");
- zipCode = scanner1.nextLine();
- System.out.println("Please enter your birth month (as a number).");
- System.out.println("");
- month = scanner1.nextInt();
- System.out.println("Please enter your birthday.");
- System.out.println("");
- day = scanner1.nextInt();
- System.out.println("Please enter your birth year.");
- System.out.println("");
- year = scanner1.nextInt();
- //Create a GregorianCalendar date for the date of birth.
- dob = new GregorianCalendar(year, month, day);
- //Add all of the information to a new student in constructor.
- Student newStudent = new Student(firstName, lastName, socialSecurityNumber, street, city, state, zipCode, sectionList, studentID, dob);
- //Create new boolean, for nested while loop. This loop will repeat for each course added until the user says to stop or 5 courses have been added.
- boolean continueWhile = true;
- //initialize some variables used for calculations in the while loop.
- int action;
- int test;
- //while the boolean says to continue...
- while (continueWhile) {
- //Print the courses to console
- System.out.println("Here is a list of available courses:");
- System.out.println("");
- for (int i = 0; i < sections.length; i++) {
- System.out.print((i + 1) + ". " + sections[i].getCourseID() + " " + sections[i].getSectionNumber() + " " + sections[i].getCourseName() + " " + sections[i].getTime().toString() + " " + sections[i].getDaysOfTheWeek() + "\n");
- }
- //Ask the user what courses they would like to add.
- System.out.println("");
- System.out.println("Which course would you like to add? Type in the course number (the number in front), or tpe 0 to quit.");
- System.out.println("");
- //Now for the grand try statement, where numerous things are caught. See Student class to see how the numerous exceptions are thrown.
- try {
- //take in the number the user enters.
- action = scanner1.nextInt();
- //if the entered number is 0
- if (action == 0)
- {
- //end the while loop and don't add more sections, terminate the program.
- continueWhile = false;
- }
- //if anything but 0 is entered
- else
- {
- //add the section to the student's sections list.
- test = newStudent.addSection(action-1,sections);
- }
- }
- //This one is caught if the user types in an invalid datatype.
- catch(InputMismatchException a)
- {
- System.out.println("That is not a number!");
- }
- //This one is caught if the user types in a number that is not associated with a section.
- catch(SectionInvalidException f)
- {
- System.out.println("This section could not be added, because there is no section assigned to this number!");
- }
- //This one is caught if the student already has the section.
- catch(SectionAlreadyExistsException b)
- {
- System.out.println("This section couldn't be added, because you already have this section!");
- }
- //This one is caught if the student already has a section in the same course.
- catch(CourseAlreadyExistsException c)
- {
- System.out.println("This section couldn't be added, because you already have another section in this same course!");
- }
- //This one is thrown if the maximum number of sections has already been hit. This should never occur, because once the max is hit it should automatically end the loop.
- catch(MaxSectionsException d)
- {
- System.out.println("This section couldn't be added, because you already have the maximum of five sections!");
- System.out.println("This should never be seen, so you have a bug!");
- }
- //This is where it checks if there are 5 sections, and if so, ends the loop.
- if (newStudent.getSectionList().length == 5) {
- continueWhile = false;
- System.out.println("You hit the maximum of 5 sections, so you cannot add any more!");
- }
- }
- //Now for the results stage.
- //Print out an explanation.
- System.out.println("");
- System.out.println("Here are your results:");
- //Print out the student's toString.
- System.out.println("************************************************************************************");
- System.out.println(newStudent.toString());
- System.out.println("************************************************************************************");
- //Thank the user for using program.
- System.out.println("Thanks for using this program!");
- //Tell the user it is attempting to write results to a file.
- System.out.println("Attempting to write results to file...");
- //attempt to write the results to a file.
- try{
- PrintWriter writer = new PrintWriter("results.txt", "UTF-8");
- writer.println(newStudent.toString());
- writer.println("");
- writer.println("This is a text file output from a program made by Daniel Valcour");
- writer.close();
- System.out.println("File write successful! You can find your results in results.txt.");
- //If file error occurs, tell the user it failed.
- } catch (IOException e) {
- System.out.println("File write Failed!");
- }
- //End the grand while loop by setting its boolean to false.
- continueProgram = false;
- }
- //This little bit down here, is for handling InputMismatchException errors from when the user is typing in their info at the very top.
- catch(InputMismatchException a)
- {
- System.out.println("You entered an invalid input for this field!");
- System.out.println("Restarting program.");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement