Write a system (login, register, verify username, password and verification code, overwrite storage user)

Write a system

Start method presents the Start menu
login
Register
exit

Write account class (store correct username and password)
username
password


Requirement:
Enter the username and password after login and verify (write verification method), and enter the main menu interface after successful verification

Main menu interface:
Lucky Draw (need to complete the function)
shopping settlement
merchandise display
true feelings feedback
cancellation user

The verification error or the menu selection error can give a prompt and allow the operation to continue (for example, the user name is entered incorrectly, re-entered, the menu input error can also be re-entered, the main menu input error should return to the main menu interface)

Registration requirements: Enter the registered user name and password and verification code (4-digit verification code). If the account exists, it cannot be registered. If it does not exist, write the registered information into the account class (the current implementation will result in the original user name and password overwritten ), The registration is successfully returned to the start menu, you can use the registered account to log in

Log off user: return to the start menu, you can continue to log in or register

Account type: used to store passwords, not stored in collections, so each registration can only be overwritten

/ ** 
 * Account class 
 * User name and password for storage system 
 * / 
public  class Account {
     // User name and password 
    String userName = "admin" ; 
    String password = "000" ; 
}

Shopping system: encapsulates all business logic of the system

/ ** 
 * shopping system 
 * encapsulate all business logic of the system 
 * / 
public  class ShoppingSystem {
     // keypad scanner 
    Scanner SC;
     // account object 
    the Account Account;
     // random number generating objects 
    the Random Random;
     // whether the winning identification 
    boolean flag; 

     / ** 
     * Initialize the system 
     * Initialization is to initialize some necessary data before the system runs 
     * / 
    public  void init () { 
        sc = new Scanner (System.in);
         // Initialize 
        account = new Account ();
         // Initialize random number generation object
        random = new Random (); 
    } 

     / ** 
     * Turn on the system 
     * / 
    public  void start () {
         // Show the start menu 
        System.out.println ("1. Login" ); 
        System.out.println ( "2. Register " ); 
        System.out.println ( " 3. Exit " ); 
        System.out.println ( " Please choose: " );
         int chooice = sc.nextInt ();
         // Call the corresponding function 
        switch (chooice) according to the options {
             // 1. Call login method 
            case 1 : 
                login (); 
                break;
             // 2. Call the registration method 
            case 2 : 
                regist (); 
                break ;
             case 3 : 
                System.out.println ( "Exited the system" );
                 // Exit the running program
                 // System.exit (0); 
                break ; 
        } 

    } 

     / ** 
     * Login method 
     * / 
    public  void login () {
         // Enter username and password 
        System.out.println ("Please enter username:" ); 
        String userName = sc.next (); 
        System.out .println ("Please enter password:" ); 
        String password = sc.next ();
         // Verify account: incoming username and password 
        if (validate (userName, password)) { 
       // If correct, enter the main menu mainMenu (); }
else { System.out.println ( "The user name or password is wrong!" ); login (); } / ** * The method to verify the user name (the user name is also used in the registration, so separate it into one Method) * @param userName * @return * / public boolean validateName (String userName) { if (userName.equals (account.userName)) { return true ; } return false ; } / ** * Verify account * @param userName * @param password * @return * / public boolean validate (String userName, String password) {
     // First call the method of verifying the user name
if (validateName ( userName)) {
       // If the user name is correct, further verify whether the password is correct
if (password.equals (account.password)) { return true ; } } return false ; } / ** * Registration method * / public void regist () { // Enter user name and password System.out.println ("Please enter user name:" ); String userName = sc.next (); System.out.println ( "Please enter Password: " ); String password = sc.next (); // Generate verification code String code = createValidateCode (); System.out.println ( " Please enter verification code: ["+ code +"] " ); String inputCode = sc.next (); // Judgment verification code if (! inputCode.equals (code)) { System.out.println ( "Verification code input error!"); // Recursive registration regist (); return ; } // Verify username and password if (validateName (userName)) { // If verification fails, re-enter, recursive registration method System.out.println ("user name Already registered! " ); Regist (); } else { // If the verification is successful, write the registered user name and password into the account object account.userName = userName; account.password = password; // Enter the start menu start ( ); } / ** * Generate verification code *@return * / public String createValidateCode () { String code = "" ;
     // Cyclically generate 4-digit verification code
for ( int i = 1; i <= 4; i ++ ) { int n = random.nextInt (10 ); code + = n; } return code; } / ** * Main menu display method * / public void mainMenu () { // Display main menu options System.out.println ("1. Lucky Draw" ); System.out.println ( "2. Shopping settlement"); System.out.println ( :"3. Product display" ); System.out.println ( "4. True feedback" ); System.out.println ( "5. Cancel account" ); System.out.println ( "Please select:" ); int chooice = sc.nextInt (); // User input options // Judge the options and call the corresponding method switch (chooice) { // 1. Call the lucky draw method case 1 : luckyGuess (); break ; case 2 : case 3 : case 4 System.out.println ( "Function is being improved ..." ); break ; // 5. Call logout method case 5 : loginOff (); } } / ** * Draw method * / public void luckyGuess () {
     // Prevent multiple draw
if (flag) { System.out.println ("You have already drawn a lot! The system will jump directly back to the main menu"); mainMenu (); return; } // Modify the flag of the draw flag = true ; // Generate the number of winning int target = random.nextInt (10 ); System.out.println ( "Press any key for lottery" ); sc.next (); int luckNumber = random.nextInt (10 ); // Determine whether to win if (luckNumber == target) { System.out.println ( "Congratulations on winning the grand prize! Reward a GeekHome course!" ); } Else { System.out.println ( "Unfortunately, there is no winning, please make persistent efforts!" ); } // Whether winning or not should return to the main menu mainMenu (); } / ** * Logout user method * / public void loginOff () { // Return to start menu start (); } }

Launch the shopping system app

public  class SystemApp {
     public  static  void main (String [] args) {
         // Create system 
        ShoppingSystem sys = new ShoppingSystem ();
         // Initialize the system 
        sys.init ();
         // Start the system 
        sys.start (); 
    } 
}

 

Guess you like

Origin www.cnblogs.com/gfl-1112/p/12688680.html