Implement lucky draw system based on Java

  • 2020-12-22 17:38:41
  • OfStack

This task requires to develop a set of lucky draw system for a shopping mall, the customer must first register as a member of the shopping mall, members login successfully, you can participate in the lucky draw activities.

registered

The user selects the "Registration" menu and enters the registration interface. After inputting the user name and password, the system prompts the successful registration and gives the member card number.

The login

After successful registration, the user selects the "Login" menu and enters the login interface. Enter the username and password with which you registered. Login successful, the system prompt welcome message. If the user and password are incorrectly typed, prompt the user to continue typing for up to 3 chances.

Lucky draw

After successful login, the user selects the "Lucky draw" menu and enters the lucky draw interface. Enter the membership card number, and the system will generate 5 random 4-digit numbers as lucky numbers. If your card number is 1 of them, you will be a lucky member for the day.

The source code


package cn.jbit.dlc1;

import java.util.Scanner;

public class LuckyNumber5 {

 /**
  *  Lucky draw 
  */
 public static void main(String[] args) {
  String answer = "y"; //  Whether the logo continues 
  String userName = ""; //  The user name 
  String password = ""; //  password 
  int cardNumber = 0; //  The card number 
  boolean isRegister = false; //  Whether the identity is registered 
  boolean isLogin = false; //  Whether the identity is logged in 
  int max = 9999;
  int min = 1000;
  Scanner input = new Scanner(System.in);

  do {
   System.out.println("***** Welcome to the Lottery system *****");
   System.out.println("\t1. registered ");
   System.out.println("\t2. The login ");
   System.out.println("\t3. Lucky draw ");
   System.out.println("***************************");
   System.out.print(" Please select the menu: ");
   int choice = input.nextInt();
   switch (choice) {
   case 1:
    System.out.println("[ The Jackpot system  >  registered ]");
    System.out.println(" Please fill in your personal registration information: ");
    System.out.print(" User name: ");
    userName = input.next();
    System.out.print(" Password: ");
    password = input.next();
    //  To obtain 4 A bit random number serves as the card number 
    cardNumber = (int)(Math.random()*(max-min))+min;
    System.out.println("\n After successful registration, please remember your membership card number ");
    System.out.println(" The user name \t password \t Membership card number ");
    System.out.println(userName + "\t" + password + "\t" + cardNumber);
    isRegister = true; //  The registration is successful and the flag bit is set to true
    break;
   case 2:
    System.out.println("[ The Jackpot system  >  The login ]");
    if (isRegister) { //  Determine whether or not to register 
     // 3 Secondary input opportunity 
     for (int i = 1; i <= 3; i++) {
      System.out.print(" Please enter user name: ");
      String inputName = input.next();
      System.out.print(" Please enter your password: ");
      String inputPassword = input.next();
      if (userName.equals(inputName) && password.equals(inputPassword)) {
       System.out.println("\n Welcome you to: " + userName);
       isLogin = true; //  Login successful, flag bit set to true
       break;
      } else if (i < 3) {
       System.out.println(" User name or password error, and " + (3 - i) + " Second chance! ");
      } else {
       System.out.println(" you 3 Input error! ");
      }
     }
    } else {
     System.out.println(" Please register first, then log in! ");
    }
    break;
   case 3:
    System.out.println("[ The Jackpot system  >  Lucky draw ]");
    if (!isLogin) { //  Determine whether or not to log in 
     System.out.println(" Please log in first and then draw! ");
     System.out.println(" To continue? ( y/n ) ");
     answer = input.next();
    } else {
     // generate 5 a 4 Bit random numbers, and stored in an array 
     int[] luckynums = new int[5];
     for(int i = 0; i < luckynums.length; i++){
      luckynums[i] = (int)(Math.random()*(max-min))+min;
     }
     System.out.print(" Please enter your card number: ");
     int yourcard = input.nextInt();
     int i;
     System.out.print("\n Today's lucky number is: ");
     for (i = 0; i < luckynums.length; i++) {
      System.out.print(luckynums[i] + " ");
     }
     for (i = 0; i < luckynums.length; i++) {
      if (luckynums[i] == yourcard) {
       System.out.println("\n A: congratulations! You are the lucky member of this day! ");
       break;
      }
     }
     if (i == luckynums.length) {
      System.out.println("\n I'm sorry! You are not a lucky member of this day! ");
     }
    }
    break;
   default:
    System.out.println("[ Your input is incorrect! ]");
    break;
   }
   System.out.print(" To continue? ( y/n ) :");
   answer = input.next();
   System.out.println("");
  } while ("y".equals(answer));
  if ("n".equals(answer)) {
   System.out.println(" System exit, thank you for using! ");
  }
 }
}

Just a basic small architecture, there are many areas that can be improved, I hope you have a definite help.

To learn more about the implementation of the lucky draw function, please refer to this topic: lucky draw function


Related articles: