Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @(#)StudentTest.java
- *
- *
- * @author
- * @version 1.00 2016/4/27
- */
- import java.util.Scanner;
- import java.util.Date;
- import java.io.*;
- public class StudentTest {
- static Scanner input = new Scanner(System.in);
- static int sub = 0;
- public static void main(String args[]) {
- //declare variables
- int ID,Course_Number,userID,choice = 0,numrecords,menu = 0,mchoice = 0,changeap = 0,NewCourseID,changest = 0,NewStudentID;
- String Name,Course_Name,Address,mstudent,Date_Of_Birth="",NewCourseName = "",NewStudentName = "",strID,strCourse_Number,struserID,strchoice,strnumrecords,strmenu,strmchoice,strchangeap,strnewcourseID,strchangest,strnewstudentID;
- char check1,check2;
- boolean MatureFlag = false;
- //Welcome the user to the program
- System.out.println("\t\t\t Welcome to the college application app.");
- //Prompt the user to enter the number of students, he wishes to add to the array
- System.out.print("\t Enter the number of students:");
- strnumrecords = input.next();
- //validate the entered data.
- numrecords = validate(strnumrecords,4);
- //Create new student and application.
- Student s1[] = new Student[numrecords];
- Application a1[] = new Application[numrecords];
- //For loop, To make the user input data the X amount of times, X being the amount of students he entered.
- for(int i = 0;i<numrecords;i++){
- //Prompt the user for the information
- input.nextLine();
- System.out.print("\t Enter the Applicants Name : ");
- Name = input.nextLine();
- System.out.print("\t Enter the Applicants ID : ");
- strID = input.nextLine();
- ID = validate(strID,2);
- System.out.print("\t Enter the Course Name: ");
- Course_Name = input.nextLine();
- System.out.print("\t Enter the Course Number : ");
- strCourse_Number = input.nextLine();
- Course_Number = validate(strCourse_Number,4);
- System.out.print("\t Enter the Student Address : ");
- Address = input.nextLine();
- //create application
- a1[i] = new Application(Course_Number,Course_Name);
- System.out.print("\t Is this a Mature Student ? Y/N :");
- mstudent = input.next();
- mstudent = mstudent.toUpperCase();
- System.out.println("\t Option Chosen = " + mstudent);
- //While loop, validation for mature student Y/N
- while(!mstudent.matches("Y") && (!mstudent.matches("N"))){
- System.out.println("\t\tError, Enter Y or N Please");
- System.out.print("\tEnter Y/N For Mature Student : ");
- mstudent = input.next();
- mstudent = mstudent.toUpperCase();
- }//End while Loop
- //reset MatureFlag
- MatureFlag = false;
- //If the matrueFlag is Yes, Then Make the user enter Date_Of_Birth
- if(mstudent.matches("Y")){
- while(MatureFlag == false){
- System.out.print("\tEnter date of birth dd/mm/yyyy : ");
- Date_Of_Birth = input.next();
- //if character input > 10, Then make him re-enter it.
- if (Date_Of_Birth.length()!=10){
- System.out.println("\tError, Incorrect Input\n\nPlease enter dd/mm/yyyy");
- }
- // If the character input = 10, Then make sure the 2'nd and 5'th characters are "/"
- else{
- check1 = Date_Of_Birth.charAt(2);
- check2 = Date_Of_Birth.charAt(5);
- //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
- if ((check1=='/') && (check2 =='/')){
- System.out.println("\t Date of Birth has been entered Correctly");
- MatureFlag = true;
- }
- //If the 2'nd and 5'th characters are not "/", but it is 10 characters, make the user re-input the data.
- else{
- System.out.println("\tError, Incorrect Input\n\nPlease enter dd/mm/yyyy");
- }
- }//end else
- }//end while
- //Create Mature Student
- s1[i] = new MatureStudent(Name,ID,Address,a1[i],Date_Of_Birth);
- }//end if
- else{
- //Create Student
- s1[i] = new Student(Name,ID,Address,a1[i]);
- }//end if
- }//end forloop of inputting data
- //Reset Choice
- choice =0;
- //While loop, keeps going until the user chooses to quit.
- while (choice !=3){
- //Prompt the user for his choice
- System.out.println("");
- System.out.println("\t\t\t Please, Select an option: ");
- System.out.println("\t 1]View Course Details");
- System.out.println("\t 2]Edit Course/Personal Details");
- System.out.println("\t 3]Quit");
- //Validate the next Input
- strchoice = input.next();
- choice = validate(strchoice,1);
- //User has chosen to View Course Info, Prompt him for ID
- if(choice == 1){
- System.out.print("Which Applicant's Course Info, Do you wish to see?");
- System.out.print("Enter that student's ID");
- struserID = input.next();
- userID = validate(struserID,2);
- for(int i = 0;i<s1.length;i++){
- //If user prompt matches ID
- if(userID == s1[i].getID()){
- //Change matureFlag
- MatureFlag = true;
- System.out.println("ID Found");
- //Hold onto the position the array is on.
- sub = i;
- break;
- }//end if
- }//end for
- //If flag = False, then ID not found
- if (MatureFlag == false){
- System.out.println("\t ID entered, Does not match ID.");
- System.out.println("\t The program will now shut down.");
- System.exit(0);
- }//end if
- //If mature flag = true, then ID does match, and you can print the details.
- else{
- System.out.print(s1[sub].toString());
- }//end else
- }//end if choice 1
- //If user chose to Edit Personal/
- else if(choice==2){
- //promt the user to enter ID
- System.out.print("Enter Applicants ID to Display : ");
- struserID = input.nextLine();
- userID = validate(struserID,2);
- //if useID and ID- match print users details outside the loop
- for(int i = 0;i<s1.length;i++){
- if(userID == s1[i].getID()){
- //change the value of the MatureFlag
- MatureFlag = true;
- System.out.println("ID Found");
- //hold onto i, this is where the users details are stored in the array
- sub = i;
- //break out of the loop
- break;
- }//end if userID == ID[i]
- }//end for
- //If id's do not match, then exit the program
- if (MatureFlag == false){
- System.out.println("\n---ID Number Doesnt Match, Goodbye---\n");
- System.exit(0);
- }//end if
- else{
- while(menu != 5){
- //Show the menu, and Prompt the user for input
- System.out.println("");
- System.out.println("\t\t\t Please, Select an option: ");
- System.out.println("\t 1]View Application Details");
- System.out.println("\t 2]View Personal Details");
- System.out.println("\t 3]Change Application Details");
- System.out.println("\t 4]Change Personal Details");
- System.out.println("\t 5]Quit");
- strmchoice = input.nextLine();
- mchoice = validate(strmchoice,3);
- if(mchoice == 1){
- System.out.print(a1[sub].toString());
- }//end choice = 1
- else if(mchoice == 2){
- System.out.print(s1[sub].getInfo());
- }//end choice = 2
- else if(mchoice == 3){
- System.out.print("Enter 1 to Change Course Or 2 to Change Course ID : ");
- strchangeap = input.nextLine();
- changeap = validate(strchangeap,5);
- if(changeap == 1){
- System.out.print("Enter Course Name :");
- NewCourseName = input.nextLine();
- a1[sub].setCourse_Name(NewCourseName);
- }//end if
- if(changeap == 2){
- System.out.print("Enter Course ID : ");
- strnewcourseID = input.nextLine();
- NewCourseID = validate(strnewcourseID,4);
- a1[sub].setCourse_Number(NewCourseID);
- }//end if
- }//end choice = 3
- else if(mchoice == 4){
- System.out.print("Enter 1 to Change Student Name Or 2 to Change Student ID : ");
- strchangest = input.nextLine();
- changest = validate(strchangest,5);
- if(changest == 1){
- System.out.print("Enter Course Name :");
- NewStudentName = input.nextLine();
- s1[sub].setName(NewStudentName);
- }//end if
- if(changest == 2){
- System.out.print("Enter Student ID : ");
- strnewstudentID = input.nextLine();
- NewStudentID = validate(strnewstudentID,2);
- s1[sub].setID(NewStudentID);
- }//end if
- }//end choice = 4
- else{
- break;
- }//end if
- }//end while
- }//end else
- }//end choice
- else{
- //Write all info to a file
- BufferedWriter Information = null;
- String loc1 = "Info.doc";
- try{
- Information = new BufferedWriter (new FileWriter(loc1,true));
- }catch(Exception e){
- System.out.println("Cannot save to file");
- }
- //not Names.length, it will be collegeStudents (or the Name of your student array)
- for (int i = 0;i<s1.length;i++){
- try{
- Information.newLine();
- Information.write(s1[i].toString());
- //not Names.length, it will be collegeStudents (or the Name of your student array)
- if(i==s1.length-1){
- Information.newLine();
- Information.close();
- Date d = new Date();
- System.out.println("\nFile Updated on " + d.toString() + "\n");
- }//end if
- }//end try
- catch(Exception ex){
- System.out.println("Could not access file");
- }//end catch
- }//end for
- //End of writing to a file
- //System.out.println("\nFile updated\n");
- System.out.println("Thank you for using the program\n\n");
- System.exit(0);
- }//end if
- }//end while
- }//end main method
- public static int validate(String x,int y){
- if(y==1){
- while(!x.matches("[1-3]")){
- java.awt.Toolkit.getDefaultToolkit().beep();
- System.out.println("Error,Digits Between 1 to 3 Only");
- System.out.print("\nSelect An Option\n1) View Course/Personal Details\n2) Edit Course/Personal Details\n3)Exit\n>");
- x = input.nextLine();
- }//end while for validate loginmenu
- return Integer.parseInt(x);
- }//end if y==1
- else if(y==2){
- while(!x.matches("\\d{3}")){
- java.awt.Toolkit.getDefaultToolkit().beep();
- System.out.print("Error, 3 digits only\n\nEnter ID : ");
- x = input.nextLine();
- }//end while
- return Integer.parseInt(x);
- }//end if y==2
- else if(y==3){
- while(!x.matches("[1-5]")){
- java.awt.Toolkit.getDefaultToolkit().beep();
- System.out.println("\n_-_-_Error, Numbers 1-5 Only_-_-_\n");
- 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>");
- x = input.nextLine();
- }//end while
- return Integer.parseInt(x);
- }//end if y==3
- else if(y==4){
- while(!x.matches("\\d+")){
- java.awt.Toolkit.getDefaultToolkit().beep();
- System.out.println("Error,Digits Only");
- System.out.print("\n*******Enter Number of Students*******\n>");
- x = input.nextLine();
- }//end while
- return Integer.parseInt(x);
- }//end if==4
- else if(y==5){
- while(!x.matches("[1-2]")){
- java.awt.Toolkit.getDefaultToolkit().beep();
- System.out.println("Error,Digits Between 1 to 2 Only");
- System.out.print("Enter 1 to Change Course Or 2 to Change Course ID : ");
- x = input.nextLine();
- }//end while for validate loginmenu
- return Integer.parseInt(x);
- }
- return Integer.parseInt(x);
- }//end validate method
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement