import java.util.Scanner;
public class Registration{
/**
* @param args
*/
public static void main(String[] args)
{
String[] userId = {"S1234", "A1234", "S456", "S789"};
String[] password = {"apple", "peach", "banana", "apricot"};
String[] firstName = {"Jack", "Belinda", "John", "Mary"};
String[] lastName = {"Black", "Blake", "Smith", "Murphy"};
int[] userType = {1, 2, 1, 1};
String[] choice1 = {"SOFT6017", null, "SOFT6018", "SOFT6017"};
String[] choice2 = {"SOFT6018", null, "SOFT6017", "SOFT6018"};
welcome();
int loggedInIndex = login(userId, password);
if(loggedInIndex!=-1)
{
System.out.println("Welcome " + firstName[loggedInIndex] +" " + lastName[loggedInIndex]);
}
else
{
System.out.println("Invailed Details Entered");
}
}
private static int login(String[] userId, String[] password)
{
Scanner keyboard = new Scanner(System.in);
String userIdEntered = readString("Please Enter ID Number: ");
int index = find(userId, userIdEntered );
String passwordEntered = readString("Enter Password: ");
if(passwordEntered.equals(password[index]))
{
return index;
}
else
{
return -1;
}
}
private static void welcome()
{
System.out.println(" CIT MODULE REGISTRATION - Login");
System.out.println("================================");
}
private static String readString(String prompt)
{
// Create a scanner object for input from the console window
Scanner keyboard = new Scanner(System.in);
String value;
do
{
System.out.println(prompt);
value = keyboard.nextLine();
if(value.length()<1)
{
//error
System.out.println("Error");
}
}while(value.length()<1);
return value;
}
/**Finds the index of a specified item in an array
* @param x The array to be searched
* @param value The value to be found
* @return The index in the array at which the item is found */
public static int find(String[] x, String value) {
int index = 0;
boolean found = false;
while(index < x.length && !found)
{
if (x[index].equals( value))
found = true;
else
index++;
}
if (found)
return index;
else
return -1;
}
/** The readInteger method reads an integer value from
* the console window and will display an appropriate
* error message if a non-integer value is entered.
* @param prompt A prompt to request the user to enter a value
* @return The integer value entered.
*/
public static int readInteger(String prompt)
{
// Create a scanner object for input from the console window
Scanner keyboard = new Scanner(System.in);
// boolean flag which is used to control the
// data validation loop
boolean numGood = false; // Assume the worst
do
{
System.out.print(prompt); // ask for the value
if(!keyboard.hasNextInt()) // check if what's in the keyboard buffer is not an integer
{
System.out.println("You must enter an integer value!"); // display an error message
keyboard.nextLine(); // consume the bad value entered
}
else
numGood = true; // value entered is good
} while(!numGood);
// at this point we know the value in the
// keyboard buffer is numeric so we can go ahead and
// return it.
return keyboard.nextInt();
}
/**
* The readPositiveInteger method reads a positive integer value
* from the console window and will display an appropriate
* error message if a non-positive integer value is entered.
* @param prompt A prompt to request the user to enter a value
* @return The positive integer value entered.
*/
public static int readPositiveInteger(String prompt)
{
int value;
do
{
value = readInteger(prompt); // ask for and read an integer value
if (value < 0) // check if the value entered is less than 0
// display an error message
System.out.println("Error - you must enter a positive integer value!");
} while (value <0);
// at this point we know the value entered is positive
// so return it
return value;
}
}