Advertisement
Guest User

Untitled

a guest
Apr 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.77 KB | None | 0 0
  1. /**
  2. * @(#)StudentTest.java
  3. *
  4. *
  5. * @author
  6. * @version 1.00 2016/4/27
  7. */
  8.  
  9. import java.util.Scanner;
  10. import java.util.Date;
  11. import java.io.*;
  12. public class StudentTest {
  13.  
  14. static Scanner input = new Scanner(System.in);
  15. static int sub = 0;
  16. public static void main(String args[]) {
  17.  
  18. //declare variables
  19. int ID,Course_Number,userID,choice = 0,numrecords,menu = 0,mchoice = 0,changeap = 0,NewCourseID,changest = 0,NewStudentID;
  20. String Name,Course_Name,Address,mstudent,Date_Of_Birth="",NewCourseName = "",NewStudentName = "",strID,strCourse_Number,struserID,strchoice,strnumrecords,strmenu,strmchoice,strchangeap,strnewcourseID,strchangest,strnewstudentID;
  21.  
  22. char check1,check2;
  23. boolean MatureFlag = false;
  24.  
  25. //Welcome the user to the program
  26. System.out.println("\t\t\t Welcome to the college application app.");
  27. //Prompt the user to enter the number of students, he wishes to add to the array
  28. System.out.print("\t Enter the number of students:");
  29. strnumrecords = input.next();
  30. //validate the entered data.
  31. numrecords = validate(strnumrecords,4);
  32. //Create new student and application.
  33. Student s1[] = new Student[numrecords];
  34. Application a1[] = new Application[numrecords];
  35.  
  36. //For loop, To make the user input data the X amount of times, X being the amount of students he entered.
  37. for(int i = 0;i<numrecords;i++){
  38.  
  39. //Prompt the user for the information
  40. input.nextLine();
  41. System.out.print("\t Enter the Applicants Name : ");
  42. Name = input.nextLine();
  43. System.out.print("\t Enter the Applicants ID : ");
  44. strID = input.nextLine();
  45. ID = validate(strID,2);
  46. System.out.print("\t Enter the Course Name: ");
  47. Course_Name = input.nextLine();
  48. System.out.print("\t Enter the Course Number : ");
  49. strCourse_Number = input.nextLine();
  50. Course_Number = validate(strCourse_Number,4);
  51. System.out.print("\t Enter the Student Address : ");
  52. Address = input.nextLine();
  53.  
  54. //create application
  55. a1[i] = new Application(Course_Number,Course_Name);
  56. System.out.print("\t Is this a Mature Student ? Y/N :");
  57. mstudent = input.next();
  58. mstudent = mstudent.toUpperCase();
  59. System.out.println("\t Option Chosen = " + mstudent);
  60.  
  61. //While loop, validation for mature student Y/N
  62. while(!mstudent.matches("Y") && (!mstudent.matches("N"))){
  63. System.out.println("\t\tError, Enter Y or N Please");
  64. System.out.print("\tEnter Y/N For Mature Student : ");
  65. mstudent = input.next();
  66. mstudent = mstudent.toUpperCase();
  67. }//End while Loop
  68.  
  69. //reset MatureFlag
  70. MatureFlag = false;
  71.  
  72. //If the matrueFlag is Yes, Then Make the user enter Date_Of_Birth
  73. if(mstudent.matches("Y")){
  74. while(MatureFlag == false){
  75. System.out.print("\tEnter date of birth dd/mm/yyyy : ");
  76. Date_Of_Birth = input.next();
  77. //if character input > 10, Then make him re-enter it.
  78. if (Date_Of_Birth.length()!=10){
  79. System.out.println("\tError, Incorrect Input\n\nPlease enter dd/mm/yyyy");
  80. }
  81. // If the character input = 10, Then make sure the 2'nd and 5'th characters are "/"
  82. else{
  83. check1 = Date_Of_Birth.charAt(2);
  84. check2 = Date_Of_Birth.charAt(5);
  85. //If the 2'nd and 5'th characters are "/" then, Change the MatureFlag to true and tell the user the information has been entered correctly
  86. if ((check1=='/') && (check2 =='/')){
  87. System.out.println("\t Date of Birth has been entered Correctly");
  88. MatureFlag = true;
  89. }
  90. //If the 2'nd and 5'th characters are not "/", but it is 10 characters, make the user re-input the data.
  91. else{
  92. System.out.println("\tError, Incorrect Input\n\nPlease enter dd/mm/yyyy");
  93. }
  94. }//end else
  95. }//end while
  96. //Create Mature Student
  97. s1[i] = new MatureStudent(Name,ID,Address,a1[i],Date_Of_Birth);
  98. }//end if
  99. else{
  100. //Create Student
  101. s1[i] = new Student(Name,ID,Address,a1[i]);
  102. }//end if
  103. }//end forloop of inputting data
  104.  
  105. //Reset Choice
  106. choice =0;
  107.  
  108. //While loop, keeps going until the user chooses to quit.
  109. while (choice !=3){
  110. //Prompt the user for his choice
  111. System.out.println("");
  112. System.out.println("\t\t\t Please, Select an option: ");
  113. System.out.println("\t 1]View Course Details");
  114. System.out.println("\t 2]Edit Course/Personal Details");
  115. System.out.println("\t 3]Quit");
  116. //Validate the next Input
  117. strchoice = input.next();
  118. choice = validate(strchoice,1);
  119.  
  120. //User has chosen to View Course Info, Prompt him for ID
  121. if(choice == 1){
  122. System.out.print("Which Applicant's Course Info, Do you wish to see?");
  123. System.out.print("Enter that student's ID");
  124. struserID = input.next();
  125. userID = validate(struserID,2);
  126. for(int i = 0;i<s1.length;i++){
  127. //If user prompt matches ID
  128. if(userID == s1[i].getID()){
  129. //Change matureFlag
  130. MatureFlag = true;
  131. System.out.println("ID Found");
  132. //Hold onto the position the array is on.
  133. sub = i;
  134. break;
  135. }//end if
  136. }//end for
  137. //If flag = False, then ID not found
  138. if (MatureFlag == false){
  139. System.out.println("\t ID entered, Does not match ID.");
  140. System.out.println("\t The program will now shut down.");
  141. System.exit(0);
  142. }//end if
  143. //If mature flag = true, then ID does match, and you can print the details.
  144. else{
  145. System.out.print(s1[sub].toString());
  146. }//end else
  147. }//end if choice 1
  148.  
  149. //If user chose to Edit Personal/
  150. else if(choice==2){
  151. //promt the user to enter ID
  152. System.out.print("Enter Applicants ID to Display : ");
  153. struserID = input.nextLine();
  154. userID = validate(struserID,2);
  155.  
  156. //if useID and ID- match print users details outside the loop
  157. for(int i = 0;i<s1.length;i++){
  158. if(userID == s1[i].getID()){
  159. //change the value of the MatureFlag
  160. MatureFlag = true;
  161. System.out.println("ID Found");
  162. //hold onto i, this is where the users details are stored in the array
  163. sub = i;
  164. //break out of the loop
  165. break;
  166.  
  167. }//end if userID == ID[i]
  168. }//end for
  169. //If id's do not match, then exit the program
  170. if (MatureFlag == false){
  171. System.out.println("\n---ID Number Doesnt Match, Goodbye---\n");
  172. System.exit(0);
  173. }//end if
  174.  
  175. else{
  176. while(menu != 5){
  177. //Show the menu, and Prompt the user for input
  178. System.out.println("");
  179. System.out.println("\t\t\t Please, Select an option: ");
  180. System.out.println("\t 1]View Application Details");
  181. System.out.println("\t 2]View Personal Details");
  182. System.out.println("\t 3]Change Application Details");
  183. System.out.println("\t 4]Change Personal Details");
  184. System.out.println("\t 5]Quit");
  185. strmchoice = input.nextLine();
  186.  
  187. mchoice = validate(strmchoice,3);
  188.  
  189. if(mchoice == 1){
  190. System.out.print(a1[sub].toString());
  191. }//end choice = 1
  192. else if(mchoice == 2){
  193. System.out.print(s1[sub].getInfo());
  194. }//end choice = 2
  195. else if(mchoice == 3){
  196. System.out.print("Enter 1 to Change Course Or 2 to Change Course ID : ");
  197. strchangeap = input.nextLine();
  198. changeap = validate(strchangeap,5);
  199. if(changeap == 1){
  200. System.out.print("Enter Course Name :");
  201. NewCourseName = input.nextLine();
  202. a1[sub].setCourse_Name(NewCourseName);
  203. }//end if
  204. if(changeap == 2){
  205. System.out.print("Enter Course ID : ");
  206. strnewcourseID = input.nextLine();
  207. NewCourseID = validate(strnewcourseID,4);
  208. a1[sub].setCourse_Number(NewCourseID);
  209. }//end if
  210. }//end choice = 3
  211.  
  212. else if(mchoice == 4){
  213. System.out.print("Enter 1 to Change Student Name Or 2 to Change Student ID : ");
  214. strchangest = input.nextLine();
  215. changest = validate(strchangest,5);
  216. if(changest == 1){
  217. System.out.print("Enter Course Name :");
  218. NewStudentName = input.nextLine();
  219. s1[sub].setName(NewStudentName);
  220. }//end if
  221. if(changest == 2){
  222. System.out.print("Enter Student ID : ");
  223. strnewstudentID = input.nextLine();
  224. NewStudentID = validate(strnewstudentID,2);
  225. s1[sub].setID(NewStudentID);
  226. }//end if
  227. }//end choice = 4
  228. else{
  229. break;
  230. }//end if
  231. }//end while
  232. }//end else
  233. }//end choice
  234.  
  235. else{
  236. //Write all info to a file
  237. BufferedWriter Information = null;
  238. String loc1 = "Info.doc";
  239.  
  240. try{
  241. Information = new BufferedWriter (new FileWriter(loc1,true));
  242. }catch(Exception e){
  243. System.out.println("Cannot save to file");
  244. }
  245.  
  246. //not Names.length, it will be collegeStudents (or the Name of your student array)
  247. for (int i = 0;i<s1.length;i++){
  248. try{
  249. Information.newLine();
  250. Information.write(s1[i].toString());
  251. //not Names.length, it will be collegeStudents (or the Name of your student array)
  252. if(i==s1.length-1){
  253. Information.newLine();
  254. Information.close();
  255. Date d = new Date();
  256. System.out.println("\nFile Updated on " + d.toString() + "\n");
  257. }//end if
  258.  
  259. }//end try
  260. catch(Exception ex){
  261. System.out.println("Could not access file");
  262. }//end catch
  263. }//end for
  264. //End of writing to a file
  265. //System.out.println("\nFile updated\n");
  266. System.out.println("Thank you for using the program\n\n");
  267. System.exit(0);
  268. }//end if
  269. }//end while
  270.  
  271. }//end main method
  272.  
  273. public static int validate(String x,int y){
  274. if(y==1){
  275. while(!x.matches("[1-3]")){
  276. java.awt.Toolkit.getDefaultToolkit().beep();
  277. System.out.println("Error,Digits Between 1 to 3 Only");
  278. System.out.print("\nSelect An Option\n1) View Course/Personal Details\n2) Edit Course/Personal Details\n3)Exit\n>");
  279. x = input.nextLine();
  280. }//end while for validate loginmenu
  281. return Integer.parseInt(x);
  282. }//end if y==1
  283.  
  284. else if(y==2){
  285. while(!x.matches("\\d{3}")){
  286. java.awt.Toolkit.getDefaultToolkit().beep();
  287. System.out.print("Error, 3 digits only\n\nEnter ID : ");
  288. x = input.nextLine();
  289. }//end while
  290. return Integer.parseInt(x);
  291. }//end if y==2
  292.  
  293. else if(y==3){
  294. while(!x.matches("[1-5]")){
  295. java.awt.Toolkit.getDefaultToolkit().beep();
  296. System.out.println("\n_-_-_Error, Numbers 1-5 Only_-_-_\n");
  297. System.out.print("\nSelect An Option\n1) View Application Details\n2) View Personal Details\n3) Change Application Details\n4) Change Personal Details\n5) Exit\n>");
  298. x = input.nextLine();
  299. }//end while
  300. return Integer.parseInt(x);
  301. }//end if y==3
  302.  
  303. else if(y==4){
  304. while(!x.matches("\\d+")){
  305. java.awt.Toolkit.getDefaultToolkit().beep();
  306. System.out.println("Error,Digits Only");
  307. System.out.print("\n*******Enter Number of Students*******\n>");
  308. x = input.nextLine();
  309. }//end while
  310. return Integer.parseInt(x);
  311. }//end if==4
  312.  
  313. else if(y==5){
  314. while(!x.matches("[1-2]")){
  315. java.awt.Toolkit.getDefaultToolkit().beep();
  316. System.out.println("Error,Digits Between 1 to 2 Only");
  317. System.out.print("Enter 1 to Change Course Or 2 to Change Course ID : ");
  318. x = input.nextLine();
  319. }//end while for validate loginmenu
  320. return Integer.parseInt(x);
  321. }
  322. return Integer.parseInt(x);
  323.  
  324. }//end validate method
  325. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement