Advertisement
Ramaraunt1

Untitled

Dec 3rd, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.79 KB | None | 0 0
  1. //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
  2.  
  3. //First step, import the custom exceptions.
  4. import CustomExceptions.CourseAlreadyExistsException;
  5. import CustomExceptions.SectionAlreadyExistsException;
  6. import CustomExceptions.SectionInvalidException;
  7. import CustomExceptions.MaxSectionsException;
  8.  
  9. //now import the stuff built into the JDK that I need
  10. import java.io.IOException;
  11. import java.io.PrintWriter;
  12. import java.util.GregorianCalendar;
  13. import java.util.InputMismatchException;
  14. import java.util.Scanner;
  15.  
  16. //create the Test class
  17. public class Test {
  18.  
  19. //Define the method, and use throws to prepare for the exceptions
  20. public static void main(String[] args) throws IOException,InputMismatchException, CourseAlreadyExistsException, SectionAlreadyExistsException, MaxSectionsException {
  21.  
  22. //The following is not used anymore! Please ignore.
  23. //DataStore dataStore = DataStore.getInstance();
  24. //Section[] sections = dataStore.getSections();
  25.  
  26. //Get an instance of the singleton FileReader class.
  27. FileReader fileReader = FileReader.getInstance();
  28.  
  29. //Get the sections from the fileReader.
  30. Section[] sections = fileReader.getSections();
  31.  
  32. //If the output sections are not null
  33. if (sections[0] != null) {
  34.  
  35. //use a boolean for the main while loop for the program.
  36. boolean continueProgram = true;
  37.  
  38. //while the boolean is true...
  39. while (continueProgram) {
  40.  
  41. //begin the outermost try block, to catch InvalidInputExceptions when the user is typing in information.
  42. try {
  43.  
  44. //initialize variables
  45. String firstName;
  46. String lastName;
  47. String socialSecurityNumber;
  48. String street;
  49. String city;
  50. String state;
  51. String zipCode;
  52. GregorianCalendar dob;
  53. Section[] sectionList = new Section[0];
  54. String studentID;
  55. int month;
  56. int day;
  57. int year;
  58. Scanner scanner1 = new Scanner(System.in);
  59.  
  60. //Title text in the console
  61. System.out.println("Welcome to the Course Chooser Program!!!");
  62.  
  63. //Ask for student info, and store it in variables
  64. System.out.println("Please enter your student ID.");
  65. System.out.println("");
  66. studentID = scanner1.nextLine();
  67. System.out.println("Please enter your first name.");
  68. System.out.println("");
  69. firstName = scanner1.nextLine();
  70. System.out.println("Please enter your last name.");
  71. System.out.println("");
  72. lastName = scanner1.nextLine();
  73. System.out.println("Please enter your social security number.");
  74. System.out.println("");
  75. socialSecurityNumber = scanner1.nextLine();
  76. System.out.println("Please enter your street.");
  77. System.out.println("");
  78. street = scanner1.nextLine();
  79. System.out.println("Please enter your city.");
  80. System.out.println("");
  81. city = scanner1.nextLine();
  82. System.out.println("Please enter your state acronym.");
  83. System.out.println("");
  84. state = scanner1.nextLine();
  85. System.out.println("Please enter your 5-digit zip code.");
  86. System.out.println("");
  87. zipCode = scanner1.nextLine();
  88. System.out.println("Please enter your birth month (as a number).");
  89. System.out.println("");
  90. month = scanner1.nextInt();
  91. System.out.println("Please enter your birthday.");
  92. System.out.println("");
  93. day = scanner1.nextInt();
  94. System.out.println("Please enter your birth year.");
  95. System.out.println("");
  96. year = scanner1.nextInt();
  97.  
  98. //Create a GregorianCalendar date for the date of birth.
  99. dob = new GregorianCalendar(year, month, day);
  100.  
  101. //Add all of the information to a new student in constructor.
  102. Student newStudent = new Student(firstName, lastName, socialSecurityNumber, street, city, state, zipCode, sectionList, studentID, dob);
  103.  
  104. //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.
  105. boolean continueWhile = true;
  106.  
  107. //initialize some variables used for calculations in the while loop.
  108. int action;
  109. int test;
  110.  
  111. //while the boolean says to continue...
  112. while (continueWhile) {
  113.  
  114. //Print the courses to console
  115. System.out.println("Here is a list of available courses:");
  116. System.out.println("");
  117.  
  118. for (int i = 0; i < sections.length; i++) {
  119. System.out.print((i + 1) + ". " + sections[i].getCourseID() + " " + sections[i].getSectionNumber() + " " + sections[i].getCourseName() + " " + sections[i].getTime().toString() + " " + sections[i].getDaysOfTheWeek() + "\n");
  120. }
  121.  
  122. //Ask the user what courses they would like to add.
  123. System.out.println("");
  124. System.out.println("Which course would you like to add? Type in the course number (the number in front), or tpe 0 to quit.");
  125. System.out.println("");
  126.  
  127. //Now for the grand try statement, where numerous things are caught. See Student class to see how the numerous exceptions are thrown.
  128. try {
  129. //take in the number the user enters.
  130. action = scanner1.nextInt();
  131. //if the entered number is 0
  132. if (action == 0)
  133. {
  134. //end the while loop and don't add more sections, terminate the program.
  135. continueWhile = false;
  136. }
  137. //if anything but 0 is entered
  138. else
  139. {
  140. //add the section to the student's sections list.
  141. test = newStudent.addSection(action-1,sections);
  142. }
  143. }
  144. //This one is caught if the user types in an invalid datatype.
  145. catch(InputMismatchException a)
  146. {
  147. System.out.println("That is not a number!");
  148. }
  149. //This one is caught if the user types in a number that is not associated with a section.
  150. catch(SectionInvalidException f)
  151. {
  152. System.out.println("This section could not be added, because there is no section assigned to this number!");
  153. }
  154. //This one is caught if the student already has the section.
  155. catch(SectionAlreadyExistsException b)
  156. {
  157. System.out.println("This section couldn't be added, because you already have this section!");
  158. }
  159. //This one is caught if the student already has a section in the same course.
  160. catch(CourseAlreadyExistsException c)
  161. {
  162. System.out.println("This section couldn't be added, because you already have another section in this same course!");
  163. }
  164. //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.
  165. catch(MaxSectionsException d)
  166. {
  167. System.out.println("This section couldn't be added, because you already have the maximum of five sections!");
  168. System.out.println("This should never be seen, so you have a bug!");
  169. }
  170. //This is where it checks if there are 5 sections, and if so, ends the loop.
  171. if (newStudent.getSectionList().length == 5) {
  172. continueWhile = false;
  173. System.out.println("You hit the maximum of 5 sections, so you cannot add any more!");
  174. }
  175.  
  176. }
  177. //Now for the results stage.
  178. //Print out an explanation.
  179. System.out.println("");
  180. System.out.println("Here are your results:");
  181.  
  182. //Print out the student's toString.
  183. System.out.println("************************************************************************************");
  184. System.out.println(newStudent.toString());
  185. System.out.println("************************************************************************************");
  186.  
  187. //Thank the user for using program.
  188. System.out.println("Thanks for using this program!");
  189.  
  190. //Tell the user it is attempting to write results to a file.
  191. System.out.println("Attempting to write results to file...");
  192.  
  193. //attempt to write the results to a file.
  194. try{
  195. PrintWriter writer = new PrintWriter("results.txt", "UTF-8");
  196. writer.println(newStudent.toString());
  197. writer.println("");
  198. writer.println("This is a text file output from a program made by Daniel Valcour");
  199. writer.close();
  200. System.out.println("File write successful! You can find your results in results.txt.");
  201.  
  202. //If file error occurs, tell the user it failed.
  203. } catch (IOException e) {
  204. System.out.println("File write Failed!");
  205. }
  206. //End the grand while loop by setting its boolean to false.
  207. continueProgram = false;
  208. }
  209. //This little bit down here, is for handling InputMismatchException errors from when the user is typing in their info at the very top.
  210. catch(InputMismatchException a)
  211. {
  212. System.out.println("You entered an invalid input for this field!");
  213. System.out.println("Restarting program.");
  214. }
  215.  
  216. }
  217. }
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement