Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.InputMismatchException;
  3. import java.util.Scanner;
  4.  
  5. enum ClassStanding{FRESHMAN,SOPHOMORE,JUNIOR,SENIOR,UNKNOWN,MASTERS_STUDIES,PHD_STUDIES};
  6. enum Major{CS,CEG,EE,ISE,BME,ME,MET,UNKNOWN};
  7. enum StudentType{UNDERGRADUATE,GRADUATE,UNDECLARED};
  8.  
  9. public class Main {
  10.  
  11. /**
  12. * @param args the command line arguments
  13. */
  14. public static void main(String[] args) {
  15. Scanner stdin = new Scanner(System.in);
  16. ArrayList<Student> studentList = new ArrayList<>();
  17. int counter;
  18. boolean continueInput;
  19. int contCounter;
  20. do {
  21. do {
  22. System.out.print("Please enter what you want to do-ADD, REMOVE, LIST, or SAVE: ");
  23. switch (stdin.next().toLowerCase()) {
  24. case "add": add(stdin, studentList); counter = 0; break;
  25. case "remove": counter = 0; break;
  26. case "list": counter = 0; break;
  27. case "save": counter = 0; break;
  28. default: System.out.println("Improper input, please enter only ADD, REMOVE, LIST, or SAVE."); counter = 1;
  29. }
  30. } while (counter == 1);
  31. do {
  32. System.out.print("nDo you want to continue? Yes or no: ");
  33. switch (stdin.next().toLowerCase()) {
  34. case "yes": contCounter = 0; continueInput = true; break;
  35. case "no": contCounter = 0; continueInput = false; break;
  36. default: contCounter = 1; continueInput = false; System.out.print("nPlease only enter 'yes' or 'no'.");
  37. }
  38. } while (contCounter == 1);
  39. } while (continueInput);
  40. } // end main method
  41.  
  42. public static void add(Scanner stdin, ArrayList<Student> studentList) { // this is the one
  43. String firstName;
  44. String lastName;
  45. String uid;
  46. StudentType studentType;
  47. ClassStanding studentClassStanding;
  48. Major major;
  49. double overallGPA;
  50. double majorGPA;
  51. String majorProfessor;
  52. boolean thesisOption;
  53. System.out.print("Please enter the student's first name: ");
  54. String tempName = stdin.next();
  55. firstName = checkName(tempName);
  56. System.out.print("Please enter the student's last name: ");
  57. tempName = stdin.next();
  58. lastName = checkName(tempName);
  59. System.out.println("Please enter the student's UID in the format 'U####' or 'U#####': ");
  60. String tempUID = stdin.next();
  61. uid = checkUID(tempUID).toUpperCase();
  62. int count;
  63. do {
  64. System.out.print("Please enter the student's status as UNDECLARED, UNDERGRADUATE, or GRADUATE: ");
  65. switch (stdin.next().toUpperCase()) {
  66. case "UNDECLARED":
  67. studentType = StudentType.UNDECLARED;
  68. studentClassStanding = setStudentClassStanding(studentType);
  69. count = 0;
  70. Student student = new Student(firstName, lastName,
  71. uid, studentType, studentClassStanding);
  72. studentList.add(student);
  73. break;
  74. case "UNDERGRADUATE":
  75. studentType = StudentType.UNDERGRADUATE;
  76. major = setMajor();
  77. studentClassStanding = setStudentClassStanding(studentType);
  78. System.out.println("Enter the student's overall GPA below.");
  79. overallGPA = setGPA();
  80. System.out.println("Enter the student's major GPA below.");
  81. majorGPA = setGPA();
  82. count = 0;
  83. UnderGraduate underGraduate = new UnderGraduate(firstName, lastName, uid, studentType,
  84. studentClassStanding, major, overallGPA, majorGPA);
  85. studentList.add(underGraduate);
  86. break;
  87. case "GRADUATE":
  88. studentType = StudentType.GRADUATE;
  89. studentClassStanding = setStudentClassStanding(studentType);
  90. majorProfessor = setMajorProfessor();
  91. thesisOption = setThesisOption();
  92. count = 0;
  93. Graduate graduate = new Graduate(firstName, lastName, uid, studentType,
  94. studentClassStanding, majorProfessor, thesisOption);
  95. studentList.add(graduate);
  96. break;
  97. default:
  98. System.out.println("Please enter either Undeclared, Undergraduate, or Graduate only.");
  99. count = 1;
  100. }
  101. } while (count == 1);
  102. }
  103.  
  104. public static String checkName(String tempName) {
  105. int a = 1;
  106. String name1;
  107. Scanner scanner = new Scanner(System.in);
  108. do {
  109. name1 = tempName; // hold the value of firstName in name1
  110. for (int i = 0; i < tempName.length(); i++) { // loop to check input consists of letters (is a name)
  111. if (!Character.isLetter(tempName.charAt(i))) { // if non-letters detected, ensure this was intentional
  112. System.out.println("Please ensure you've entered the correct name. Re-enter the name or enter 'continue' to proceed: ");
  113. tempName = scanner.nextLine();
  114. if (tempName.equalsIgnoreCase("continue")) { // if user enters "continue", use original input
  115. a = 0;
  116. tempName = name1; // pass name1 value to firstName
  117. break;
  118. } else {
  119. a = 1; // continue prompting for firstName
  120. }
  121. } else { // accept input
  122. a = 0;
  123. }
  124. }
  125. } while (a == 1); // loop to ensure proper input
  126. return tempName;
  127. } // end checkName method
  128.  
  129. public static String checkUID(String tempUID) {
  130. Scanner scan = new Scanner(System.in);
  131. int a;
  132. do {
  133. if (tempUID.charAt(0) == 'U' || tempUID.charAt(0) == 'u') {
  134. if (tempUID.length() == 6 || tempUID.length() == 5) {
  135. a = 0;
  136. } else {
  137. a = 1;
  138. System.out.print("Please ensure input is in the form of U#### or U#####. Please re-enter the UID: ");
  139. tempUID = scan.next();
  140. }
  141. } else {
  142. a = 1;
  143. System.out.print("Please ensure input is in the form of U#### or U#####. Please re-enter the UID: ");
  144. tempUID = scan.next();
  145. }
  146. } while (a == 1);
  147. return tempUID;
  148. } // end checkUID method
  149.  
  150. public static ClassStanding setStudentClassStanding(StudentType studentType) {
  151. Scanner scan = new Scanner(System.in);
  152. int count;
  153. ClassStanding studentTempClassStanding = null;
  154. do {
  155. if (studentType == StudentType.UNDECLARED || studentType == StudentType.UNDERGRADUATE) {
  156. System.out.print("Please enter the student's class standing as either Freshman, Sophomore, Junior, Senior, or Unknown: ");
  157. switch (scan.next().toUpperCase()) {
  158. case "FRESHMAN":
  159. studentTempClassStanding = ClassStanding.FRESHMAN;
  160. count = 0;
  161. break;
  162. case "SOPHOMORE":
  163. studentTempClassStanding = ClassStanding.SOPHOMORE;
  164. count = 0;
  165. break;
  166. case "JUNIOR":
  167. studentTempClassStanding = ClassStanding.JUNIOR;
  168. count = 0;
  169. break;
  170. case "SENIOR":
  171. studentTempClassStanding = ClassStanding.SENIOR;
  172. count = 0;
  173. break;
  174. case "UNKNOWN":
  175. studentTempClassStanding = ClassStanding.UNKNOWN;
  176. count = 0;
  177. break;
  178. default:
  179. System.out.println("Please enter only Freshman, Sophomore, Junior, Senior, or Unknown.");
  180. count = 1;
  181. }
  182. } else {
  183. System.out.print("Please enter the student's class standing as either 'Masters' for Masters Studies or 'PhD' for PhD Studies: ");
  184. switch (scan.next().toUpperCase()) {
  185. case "MASTERS": studentTempClassStanding = ClassStanding.MASTERS_STUDIES; count = 0; break;
  186. case "PHD": studentTempClassStanding = ClassStanding.PHD_STUDIES; count = 0; break;
  187. default: System.out.println("Please enter only 'Masters' or 'PhD'.");
  188. count = 1;
  189. }
  190. }
  191. } while (count == 1);
  192. return studentTempClassStanding;
  193. } // end setStudentClassStanding method
  194.  
  195. public static Major setMajor() {
  196. Major tempMaj = null;
  197. Scanner s = new Scanner(System.in);
  198. int c;
  199. do {
  200. System.out.print("Please enter the student's major as either CS, CEG, EE, ISE, BME, ME, MET, or Unknown: ");
  201. switch (s.next().toUpperCase()) {
  202. case "CS":
  203. tempMaj = Major.CS;
  204. c = 0;
  205. break;
  206. case "CEG":
  207. tempMaj = Major.CEG;
  208. c = 0;
  209. break;
  210. case "EE":
  211. tempMaj = Major.EE;
  212. c = 0;
  213. break;
  214. case "ISE":
  215. tempMaj = Major.ISE;
  216. c = 0;
  217. break;
  218. case "BME":
  219. tempMaj = Major.BME;
  220. c = 0;
  221. break;
  222. case "ME":
  223. tempMaj = Major.ME;
  224. c = 0;
  225. break;
  226. case "MET":
  227. tempMaj = Major.MET;
  228. c = 0;
  229. break;
  230. case "UNKOWN":
  231. tempMaj = Major.UNKNOWN;
  232. c = 0;
  233. break;
  234. default:
  235. System.out.println("Please enter only the specified values. ");
  236. c = 1;
  237. }
  238. } while (c == 1);
  239. return tempMaj;
  240. } // end setMajor method
  241.  
  242. public static double setGPA() {
  243. Scanner s = new Scanner(System.in);
  244. double gpa;
  245. int a;
  246. do {
  247. try {
  248. System.out.print("Please enter the student's GPA: ");
  249. gpa = s.nextDouble();// read in the gpa
  250. if (gpa < 0.0 || gpa > 4.0) { // ensure the gpa is in the correct range
  251. System.out.println("Invalid input, please enter a positive value between 0.0 and 4.0.");
  252. a = 1;
  253. } else {
  254. a = 0;
  255. }
  256. } catch (InputMismatchException ex) { //catch any exceptions, prompt for correct input
  257. a = 1;
  258. gpa = 0.0;
  259. System.out.println("Sorry, please enter a double value.");
  260. s.nextLine(); // skip the last input
  261. }
  262. } while (a == 1 || gpa < 0.0 || gpa > 4.0); //loop while gpa is negative or incorrect input is received
  263. return gpa;
  264. } // end setGPA method
  265.  
  266. private static String setMajorProfessor() {
  267. Scanner s = new Scanner(System.in);
  268. String prof;
  269. System.out.print("Please enter the name of the major professor: ");
  270. String tempName = s.nextLine();
  271. prof = checkName(tempName);
  272. return prof;
  273. } // end setMajorProfessor method
  274.  
  275. private static boolean setThesisOption() {
  276. Scanner s = new Scanner(System.in);
  277. boolean thesis = false;
  278. int a;
  279. do {
  280. System.out.print("Please enter 'yes' if a thesis will be written, otherwise enter 'no': ");
  281. switch (s.next().toUpperCase()) {
  282. case "YES": thesis = true; a = 0; break;
  283. case "NO": thesis = false; a = 0; break;
  284. default: System.out.println("Please enter only 'yes' or 'no'."); a = 1;
  285. }
  286. } while (a == 1);
  287. return thesis;
  288. } // end setThesisOption method
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement