Modular design of Java registered login lottery system

Posted by vivek on Sun, 14 Jun 2020 18:13:03 +0200


The overall system is divided into the following parts:

Test Class

Register Class

Login Class

Draw category

Classes that hold user information


1. Save user information in a class and define it as static to facilitate other class calls

2. When you encounter the required functions, create new methods as far as possible and minimize the amount of code in main methods.

3. Set variables to define the status of login or not, determine whether to register by the length of user name in lottery draw, and determine whether to login by recording the variables of login status

Determine if the card number is correct by matching it to the data in the user information

4. When judging the number of logins and errors, the recursive function is to use if else statement to judge the variables defined outside the statement and increase or decrease by itself.

When the if statement meets or does not satisfy the condition, continue executing the function until the condition is met and jump out of the judgment statement

Test class code:

public class CJTest {
	public static void main(String[] args) {
		//Whether the criteria of the loop continue
		String isGoon = "";
		do {
			System.out.println("************lucky 52***********");
			System.out.println("2.Sign in");
			System.out.println("3.Lucky Draw");
			System.out.println("Please select a menu");
			Scanner scanner = new Scanner(;
			String menuNum = scanner.nextLine();
			//Call menuChoose method
			System.out.println("Continue or not---input y/n");
			//Determine whether to continue typing
			isGoon = scanner.nextLine();
		//Loop End Tip
		System.out.println("Welcome to Next Use");
	//Select the appropriate method
	public static void menuChoose(String menuNum) {
		//Perform corresponding functions based on user selection
		switch (menuNum) {
		case "1":
		case "2":
			System.out.println("Sign in");
			//Call login method to save login status
			User.isLogin = Login.login();//A direct call to a method with a return value will not cause an error.
		case "3":
			System.out.println("Lucky Draw");
			System.out.println("Input error, please re-enter");

Register class code:

public class Register {
	public static void register() {
		System.out.println("Please register user's personal information");
		Scanner scanner =new Scanner(;
		//The scanner can only be closed once and cannot receive keyboard information when it is created during the execution of this program
		System.out.println("User name");
		//Receive the user name and place it in the user information =;
		User.password =scanner.nextInt();
		System.out.println("Card number");
		//Random number from 1000 to 2000, saved to user
		User.luckNumber = (int)(Math.random()*1001+1000);
		//Display user's registration information
		System.out.println("Display user's registration information");
		System.out.println("User name:";
		System.out.println("Card number"+User.luckNumber);

Login class code:

public class Login {
	//Control Loop Enter Password Three Times
	public static int number = 0;
	public static boolean login() {
		Scanner scanner = new Scanner(;
		System.out.println("enter one user name");
		String username = scanner.nextLine();
		System.out.println("Please input a password");
		int password = scanner.nextInt();
		//Determine if user name and password match
		if(username.equals( {
			//Login Successful
			System.out.println("Login Successful");
			return true;
			//Login failed, three chances to log in again
			System.out.println("Logon Failure");
			System.out.println("You still have"+(3-number)+"Second Opportunity");
			if(number!=3) {
			}else {
				//Three chances have used login failures
				System.out.println("Logon Failure");
			//Each branch has a return value when writing the return value
			return false;

Draw category code:

public class LuckDraw {
	static int a =0;
	public static void luckDraw() {
		//Whether or not to register, registration cannot be decimated to determine whether the user name exists
		//Log on or not, no logon no smoke
		if( {
			//Not registered, none of the following
			System.out.println("Not registered");
			return ;
		if(!User.isLogin) {
			System.out.println("Not logged in");
		//Is the card number correct
		if(!isLuckNumber()) {
		//Luck draw
		//First define a variable to hold the winning result
		Boolean isluck  = false ;
		//Print out the results with five random numbers and determine if they match the lucky number
		//Define a variable to hold an accumulative string
		String s  = "This lucky data:";
		for(int i = 0; i < 5; i++) {
			int num = (int)(Math.random()*1001+1000);
			//Decide whether to win or not
			if(User.luckNumber==num) {
				isluck = true;
			//Stitching strings for printing
			if(i!=4) {
			s = s  + num+","; 
			}else {
				s = s + num;
		//Print lucky numbers
		//Judging, print the closing sentence according to isluck
		if(isluck) {
			System.out.println("Congratulations on winning the prize");
		}else {
			System.out.println("You did not win the prize");
	public static boolean isLuckNumber() {
		System.out.println("Please enter the card number");
		Scanner scanner = new Scanner(;
		int luckNumber = scanner.nextInt();
		//Determine if the card number entered by the user was the one randomly generated by the user before
		if(User.luckNumber==luckNumber) {
			//Same Card Number Draw
			System.out.println("The draw will be held immediately");
			return true;
		}else {
			//Three Opportunities with Different Card Number
			System.out.println("Incorrect card number, please continue to enter");
			if(a!=3) {
			}else {
				System.out.println("Opportunity is used up");
			return false;

User Information Class Code:

public class User {
	public static String name;
	public static int password;
	public static int luckNumber;
	public static boolean isLogin; //Login status for judgment in lottery draw