Simple implementation of java lottery system
- 2020-12-22 17:38:54
- OfStack
This article shares the specific code of java lucky draw system for your reference, the specific content is as follows
User information class
/*
* User information class
* 1. account
* 2. password
* 3. The card number
* 4. Whether the login
*/
public class User {
public static String userName = "";
public static String passWord = "";
public static int cardNumber = 0;
// Whether the login
public static boolean isLogin = false;
// Whether or not registered
public static boolean isRegister = false;
}
registered
/*
* registered
* 1. Enter account password
* 2. Generate random numbers [1000,2000]
* 3. Save user registration information
* 4. Prompt successful registration
*/
public class Register {
// Registration method
public static void userRegister() {
System.out.println(" Please enter the user name :");
Scanner scanner = new Scanner(System.in);
// Receiving user information
String userName = scanner.nextLine();
System.out.println(" Please enter your password. :");
String passWord = scanner.nextLine();
int num = (int)(Math.random() * 1001 + 1000);
// Save to the user class
User.userName = userName;
User.passWord = passWord;
User.cardNumber = num;
// Print registration information
System.out.println();
System.out.println(" Registered successfully , Please remember your membership card number ");
System.out.println(" The user name :" + userName);
System.out.println(" password :" + passWord);
System.out.println(" The card number :" + num);
// Save registration status
User.isRegister = true;
}
}
Log in class
/*
* Log in class
* 1. Enter your login id and password
* 2. And user information matching
* There are 3 A reinput opportunity
* 3. Login successful
*/
public class Login {
// Save the number of login failures
static int num = 0;
// Login method
// Inside the static method You need to use static member variables
public static boolean userLogin() {
// Decide whether to register or not
if (User.isRegister == false) {
System.out.println(" Please register first ");
Register.userRegister();
// Login failed
return false;
}
// The input information
System.out.println(" The user name :");
Scanner scanner = new Scanner(System.in);
// Receive information
String userName = scanner.nextLine();
System.out.println(" password :");
String passWord = scanner.nextLine();
// Determine the matching login information
if(userName.equals(User.userName) && passWord.equals(User.passWord)) {
// Login successful
System.out.println(" You are welcome " + userName);
// Return login results
return true;
} else {
// Login failed
num++;
// Prompt the user for a few more chances
System.out.println(" Incorrect user name or password , Please re-enter !" + " And then there were " + (3 - num) + " time ");
// Determine that you logged in incorrectly several times
if (num != 3) {
// Continue to login
userLogin();
} else {
// Login failed
System.out.println(" I'm sorry 3 Second chances are used up , Please come again tomorrow !");
// Reset the variable that records the number of logins
num = 0;
}
// If the code goes here 1 Must have failed to log in
return false;
}
}
}
Lottery class
/*
* Lottery class
* 1. Determine whether or not to log in
* 2. Enter the lucky draw card number
* There are 3 A second chance to enter the card number
* 3. Decide if you win the lottery
*/
public class CJ {
// The number of times you saved the input card number
static int cardNumber = 0;
// Draw method
public static void userCJ() {
// Determine login status
if (!User.isLogin) {
// Not logged in Direct end method
System.out.println(" Please login first ");
return;
}
// Determine if the input is correct
if(!isCarNum()) {
System.out.println(" You typed it incorrectly ");
return;
}
// Match the account
// Save the variable whether or not you won the lottery
boolean isCJ = false;
// random 5 Number and concatenate into a string to print commas apart
String string = " Your lucky number of the day :";
for (int i = 0; i < 5; i++) {
// random
int num = (int)(Math.random() * 1001 + 1000);
// Joining together
if (i < 4) {
string = string + num + ",";
} else {
string = string + num;
}
// Check to see if you've won
if (num == User.cardNumber) {
isCJ = true;
}
}
// Print the winning number
System.out.println(string);
// Decide if you win the lottery
if(isCJ) {
System.out.println(" The winning ");
} else {
System.out.println(" Not winning ");
}
}
// Enter the card number method
public static boolean isCarNum() {
System.out.println(" Please enter the card number :");
Scanner scanner = new Scanner(System.in);
// receive
String carNum = scanner.nextLine();
// turn int
int num = Integer.parseInt(carNum);
// compare
if (User.cardNumber == num) {
// Match the correct
System.out.println(" The card number is entered correctly ");
return true;
} else {
// Incorrect match
cardNumber++;
System.out.println(" Enter remaining " + (3 - cardNumber) + " time ");
if (cardNumber != 3) {
isCarNum();
} else {
System.out.println("3 Second chances are used up ");
// Enter card number reset
cardNumber = 0;
}
return false;
}
}
}
Sweepstakes (Assembly Sweepstakes logic)
/*
* Lottery Start class ( Assembly lottery logic )
*
*/
public class CJStart {
// Lottery began
public void cjStart() {
// Whether to continue with the main menu
String isGoOn = "";
// Loop main menu
do {
System.out.println("***** Welcome to the lottery system *****");
System.out.println(" 1. registered ");
System.out.println(" 2. The login ");
System.out.println(" 3. Lucky draw ");
System.out.println("************************");
Scanner scanner = new Scanner(System.in);
// Receive menu options
String menuNum = scanner.nextLine();
// Determine options
chooseMenuNum(menuNum);
// Whether the reception continues
System.out.println(" Whether to continue to choose ? y/n");
isGoOn = scanner.nextLine();
} while (isGoOn.equals("y"));
}
// Determine menu options
public void chooseMenuNum(String menuNum) {
switch (menuNum) {
case "1":
System.out.println("[ Lanou lottery system > registered ]");
System.out.println(" Please fill in your personal registration information :");
// Calling the registration method
Register.userRegister();
break;
case "2":
System.out.println("[ Lanou lottery system > The login ]");
// Calling the login method Save login status
boolean userLogin = Login.userLogin();
User.isLogin = userLogin;
break;
case "3":
System.out.println("[ Lanou lottery system > Lucky draw ]");
// Call the lucky draw method
CJ.userCJ();
break;
default:
System.out.println(" Input error , Please reselect ");
break;
}
}
}
The test class
/*
* The test class
*/
public class CJTest {
public static void main(String[] args) {
CJStart cjStart = new CJStart();
cjStart.cjStart();
}
}