Expand|Select|Wrap|Line Numbers
- import java.util.Scanner;
- public class StudentPassword {
- static Scanner reader = new Scanner (System.in);
- private int option;//the user option
- private int count = 0;//the count
- private String names[] = new String [50];
- private int passwords[] = new int [50];
- public StudentPassword(){
- while(true){
- setOption();
- selection();
- }//close whlie
- }//close main
- public int menu()
- {
- while(true){
- String menu = "----------\n1)Enter name and password\n2)Search"
- + "\n3)Print\n4)Quit\nMake a selection: ";
- try{
- System.out.print(menu);
- option = reader.nextInt();
- if(option >= 1 && option <= 4){
- reader.nextLine();//CONSUME EXTRA CHARACTER
- break;
- }else{
- System.out.print("\nEnter a number answer");
- reader.nextLine();
- }//close if
- }catch(Exception e){
- System.out.print("\nEnter a valid answer");
- reader.nextLine();
- }//exception
- }//close whlie
- return option;
- }//close menu
- public void setOption()
- {
- option = menu();
- }//close setOption
- public int getOption(){
- return option;
- }//close getOption
- public void selection()
- {
- if(getOption() == 4){
- quit();
- }else if (getOption() == 3){
- print();
- }else if (option == 2){
- search();
- }else{
- getNames();
- }//close if
- }//close selection
- public void setName(String [] names2)
- {
- names = names2;
- }//close setRun
- public String [] getNames()
- {
- String nm;//the user local name
- for(int i = count; count < names.length; i++){
- System.out.print("Enter Your name or enter 'bye' to quit: ");
- nm = reader.nextLine();
- if (nm.equalsIgnoreCase("bye")){
- break;
- }else{
- names[count] = nm;
- getPass();
- }//close if
- }//close for
- return names;
- }//close getRun
- public void setPass(int [] pass2)
- {
- passwords = pass2;
- }//close setRun
- public int [] getPass()
- {
- int pass;//the user local password
- for(int i = count; count < passwords.length; i++){
- while(true){
- try{
- System.out.print("Enter Your password: ");
- pass = reader.nextInt();
- passwords[count] = pass;
- count++;
- reader.nextLine();
- break;
- }catch(Exception e){
- System.out.print("\nEnter a number answer: ");
- reader.nextLine();
- }//close exception
- }//close while
- break;
- }//close for
- return passwords;
- }//close run
- public void quit()
- {
- System.exit(99);
- }//close quit
- public void search()
- {
- int coresspondingPassword;
- for(int i = count; count < names.length; i++){
- String searchName;
- System.out.print("Enter Name: " );
- searchName = reader.nextLine();
- if(searchName.equals(names[count])){
- coresspondingPassword = passwords[count];
- System.out.print("The password is " + coresspondingPassword);
- break;
- }else{
- System.out.print(searchName + " not found");
- }//close if
- count++;
- }//close for
- }//close search
- public void print()
- {
- for(int i = count; count < names.length; i++){
- System.out.print(names[i]);
- }//close for
- }//close getPrint
- }//StudentPassword