Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program 1: write a program in java to Make Login program to manage members inclu

ID: 662696 • Letter: P

Question

Program 1: write a program in java to Make Login program to manage members including sign up, log in, search ID/Pass. (search ID/PASS will be implemented in HW4, so please ignore it in HW3) This program is a combiation of lab5-2 and 5-3. Your program should include at least two classes, Menu class and Member class. First, your program starts with login menu. If an user finished sign up, the user can log in with correct ID and Pass. Once the user log in, then the user can see the main menu and sub-menu, which is same as lab5-2. When the user exits(log out) the main menu, the program shows login menu again. Member class should have at least three methods, login, signup, and change_pass. You can define any arguments and return value in mehtod definitions. There is no specific requirement of the methods. However, once user inputs ID and passwrod in login, the login mehtod should be used to check whether it is correct ID and pass comparing ID.txt and Pass.txt files. Simirally, if user does signup, new ID and Pass should be stored in these files through signup method. In addition, password can be changed by using change_pass method only when user have been loged in already. You can use any additinoal classes and methods if you want (of course, you can submit more than two java files for your additional classes). Also you may want to use java String class to compare two strings. In addition, you do not allow user to use white space in ID and PASS. The example of input and output are below. Example of String comparison (compare string1 to string2)

String string1 = "abc";

String string2 = "def";

if (string1.equals(string2))

{ // this line will not print because the // java string equals method returns false: System.out.println("The two strings are the same.");

}

Example of program input and output

Program start

1. Log in

2. Sign Up

3. Search ID/PASS

4. Exit

Please input:7

I am sorry. It

Explanation / Answer

/*The java program that creates methods for logIn,signUp ,search mainMenu and showMenu methods . The java program that shows a menu that allows user to enter choice to login, singup,search user id,password and logout options.
* */

//Menu.java
import java.util.Scanner;
public class Menu
{

   //create a Scanner class object
   private static Scanner scanner=new Scanner(System.in);
   //Create an instance of Member class as private member of Menu class
   private static Member member=new Member();

   public static void main(String[] args)
   {

       //calls the method showMenu that shows the menu of options
       int choice=showMenu();

       while(true)
       {
           //switch cast that selects the appropritae method
           switch (choice)
           {
          
           //calling Login method
           case 1:
               if(logIn())
               {
                   int ch=mainMenu();

                   if(ch==5)
                       break;
               }
               break;
              
               //calling signup method
           case 2:
               singUp();
               choice=showMenu();
               break;
               //calling search method
           case 3:
               search();
               break;
               //calling Logout method
           case 4:
               Exit();
           }
           //show menu choices to select
           choice=showMenu();
       }

   }

   //Terminate program
   private static void Exit()
   {
       System.out.println("Thank you.Bye!");
       System.exit(0);
   }

   /*The method search prompts user to select search by id and password*/
   private static void search()
   {
       System.out.println("Search By ");
       System.out.println("1.ID");
       System.out.println("2.Password");

       System.out.println("Enter choice");
       int ch=scanner.nextInt();
       scanner.nextLine();

       if(ch==1)
       {
           System.out.print("ID");
           String userId=scanner.nextLine();

           try
           {
               if(member.searchByID(userId))
                   System.out.println("ID found.");
               else
                   System.out.println("ID not found.");
           }
           catch (Exception e)
           {
               System.out.println(e);
           }
       }
       else if(ch==2)
       {
           System.out.print("Pass");
           int userPassword=scanner.nextInt();

           try
           {
               if(member.searchByPassword(userPassword))
                   System.out.println("password found.");
               else
                   System.out.println("password not found.");
           }
           catch (Exception e)
           {
               System.out.println(e);
           }
       }
      
   }

  
   /*The method mainMenu that shows the listof options after login
   * successful
   * */
   private static int mainMenu()
   {

       int userChoice;
       do
       {
           System.out.println("Main Menu");
           System.out.println("1. Utility");
           System.out.println("2. Game");
           System.out.println("3. Multimedia");
           System.out.println("4. Change Password");
           System.out.println("5. Logout");

           userChoice=scanner.nextInt();

           if(userChoice<0 || userChoice>5)
               System.out.println("Invalid choice");

       }while(userChoice<0 || userChoice>5);

       return userChoice;
   }

   /*The metod signUp that calls the openAccount with id and password
   * and opens a new accoutn by writing the id and password to text file
   * ID.txt and PASSWORD.txt files
   * */
   private static void singUp()
   {
       System.out.print("ID");
       String userId=scanner.nextLine();

       System.out.print("Pass");
       int userPassword=scanner.nextInt();

       try
       {
           //calling openAccount
           member.openAccount(userId, userPassword);
           System.out.println("Thank you!");
       }
       catch (Exception e)
       {
           System.out.println(e);
       }


   }


   /*The method logIn that prompts the user to input id and password
   * and calls the verify method that checks whether user id and password
   * are valid.
   * */
   private static boolean logIn()
   {

       System.out.print("ID");
       String userId=scanner.nextLine();

       System.out.print("Password");
       int userPassword=scanner.nextInt();

       boolean valid=member.verify(userId, userPassword);

       if(!valid)
           System.out.println("I am sorry.It's invalid ID or Password");


       return valid;
   }


/*The method showMenu that displays the list of options to user to select from
* */
   private static int showMenu()
   {
       int userInput;

       do
       {
           System.out.println("1.Log in");
           System.out.println("2.Sign Up");
           System.out.println("3.Search ID/PASS");
           System.out.println("4.Exit");
           System.out.println("Please input: ");
           userInput=scanner.nextInt();
           scanner.nextLine();

           if(userInput<0 || userInput>4)
               System.out.println("I am sorry.It's incorrect input.");

       }while(userInput<0 || userInput>4);

       return userInput;
   }
}


-------------------------------------------------------------------------------------------------------------------------------------

/**The java class Member has metods to create an account ,
* verify the account and search the member by id and password.
* */
//Member.java
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Member
{

   //declare two variable of class Scanner and FileWriter
   private Scanner fileReader;
   private FileWriter writer;

//constructor of the class
   public Member()
   {
       fileReader=null;
       writer=null;
   }


   //Opens an account with userid and password that write to file
   //ID.txt and PASS.txt files
   public void openAccount(String userId, int userPassword)
   {
       File file=null;
       try
       {
           file=new File("ID.txt");          
           if(file.exists())
               writer= new FileWriter(file,true);
           else
               writer= new FileWriter(file);

          
           writer.write(userId+" ");
           writer.close();
          
       }
       catch(IOException io)
       {
           System.out.println(io);
       }


       file=new File("PASS.txt");          

       try
       {
           if(file.exists())
               writer= new FileWriter(file,true);
           else
               writer= new FileWriter(file);

          
           writer.write(userPassword+" ");
           writer.close();
          
       }
       catch(IOException io)
       {
           System.out.println(io);
       }

   }

   /*The method verify that takes input argumetns id and password
   * and returns true if the account is existed in the text file
   * ID.txt and PASS.txt
   * */
   public boolean verify(String userId, int userPassword)
   {

       boolean isValidId=false;
       boolean isValidPassword=false;
       String idFile="ID.txt";
       String passwordFile="PASS.txt";

       try
       {
           File file=new File(idFile);
           //open id file
           fileReader=new Scanner(file);  

           while(fileReader.hasNext() && !isValidId)
           {
               String id=fileReader.next();
               if(id.equals(userId))
                   isValidId=true;              
           }

           //close id file
           fileReader.close();

          
           file=new File(passwordFile);
           //open password file
           fileReader=new Scanner(file);  

           while(fileReader.hasNext() && !isValidPassword)
           {
               int password=fileReader.nextInt();
               if(password==userPassword)
                   isValidPassword=true;              
           }

           //close password file
           fileReader.close();

       }
       catch (Exception e)
       {
           System.out.println(e);
       }      
       return isValidPassword && isValidId;

   }


   /*The method searchByID method takes input string ID as input
   * argument and returns true if id is found inthe text file
   * "ID.txt"
   * */
   public boolean searchByID(String ID)
   {
       boolean isValidId=false;      
       String idFile="ID.txt";
  
       try
       {

           File file=new File(idFile);
           //open id file
           fileReader=new Scanner(file);  

           while(fileReader.hasNext() && !isValidId)
           {
               String userId=fileReader.next();
               if(ID.equals(userId))
                   isValidId=true;              
           }

           //close id file
           fileReader.close();

       }
       catch (Exception e)
       {
           System.out.println(e);
       }  
      
       return isValidId;

   }

   /*The method searchByPassword method takes input integer password as input
   * argument and returns true if password is found in the text file
   * "PASS.txt"
   * */
   public boolean searchByPassword(int userPassword)
   {      
       boolean isValidPassword=false;      
       String passwordFile="PASS.txt";

       try
       {
           File file=new File(passwordFile);
           //open id file
           fileReader=new Scanner(file);  

           while(fileReader.hasNext() && !isValidPassword)
           {
               int password=fileReader.nextInt();
               if(password==userPassword)
                   isValidPassword=true;              
           }

           //close id file
           fileReader.close();
       }
       catch (Exception e)
       {
           System.out.println(e);
       }      
       return isValidPassword ;
   }
}

-----------------------------------------------------------------------------------------------------------------------------

Create two text files

"ID.txt" and write some input before starting the program

ID.txt

apple12

"PASS.txt"

1234

----------------------------------------

sample output:

1.Log in
2.Sign Up
3.Search ID/PASS
4.Exit
Please input:
1
IDapple12
Password1234
Main Menu
1. Utility
2. Game
3. Multimedia
4. Change Password
5. Logout
5
1.Log in
2.Sign Up
3.Search ID/PASS
4.Exit
Please input:
2
IDmango
Pass2345
Thank you!
1.Log in
2.Sign Up
3.Search ID/PASS
4.Exit
Please input:
1
1.Log in
2.Sign Up
3.Search ID/PASS
4.Exit
Please input:
1
IDmango
Password2345
Main Menu
1. Utility
2. Game
3. Multimedia
4. Change Password
5. Logout
5
1.Log in
2.Sign Up
3.Search ID/PASS
4.Exit
Please input:
4
Thank you.Bye!

Hope this helps you.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote