App下載

Java抽獎(jiǎng)系統(tǒng)(附完整代碼)

猿友 2021-03-16 18:01:01 瀏覽數(shù) (5465)
反饋

很多大公司的年會都會抽年終獎(jiǎng),為一年一來的付出做出獎(jiǎng)勵(lì),你知道如何用 JAVA 做抽獎(jiǎng)系統(tǒng)么?小編來告訴你。

抽獎(jiǎng)系統(tǒng)需求分析

1)實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)的三大主要功能是登錄、注冊和抽獎(jiǎng)。

2)登錄需求指的是讓用戶輸入其賬號密碼從而進(jìn)行登錄,用戶輸入其賬號去匹配系統(tǒng)內(nèi)已注冊用戶,如果輸入賬號不存在就退出,密碼則能輸入三次,登錄成功后主界面會顯示已登錄用戶的賬號信息。

3)注冊需求讓用戶先輸入其賬號,再有系統(tǒng)查詢用戶賬號名是否已存在,如果已存在就讓用戶重新輸入,不存在就進(jìn)行下一步密碼輸入,密碼的要求是6位數(shù)字字符串,用戶注冊成功后系統(tǒng)會隨機(jī)分配與已有賬號不一樣的四位數(shù)字 id 編號。

4)抽獎(jiǎng)需求功能實(shí)現(xiàn)的前提條件是要有用戶處于已登錄狀態(tài)。當(dāng)前提滿足時(shí)系統(tǒng)從已存在用戶中隨機(jī)去抽取5位不同的用戶標(biāo)記為幸運(yùn)用戶,并接著判斷此時(shí)在線的用戶是否有被抽中。

5)數(shù)據(jù)存儲采用的是文件系統(tǒng),導(dǎo)入 java.io.*包,

6)登錄用戶信息保存在 ArrayList 中,幸運(yùn)用戶編號與 id 保存在長度為5的 ?HasMap<String id,String name>? 其中它的 id 為 Key,name 為 Value。

實(shí)現(xiàn)結(jié)果

1)登錄:

抽獎(jiǎng)登錄

2)注冊:

抽獎(jiǎng)注冊

3)抽獎(jiǎng):

抽獎(jiǎng)效果

注意事項(xiàng)

在運(yùn)行代碼之前務(wù)必在 user.txt 中創(chuàng)建五個(gè)以上的用戶。

完整代碼

import java.util.Scanner;
import java.util.ArrayList;
import java.io.*;
import java.util.StringTokenizer;

public class Dos {
    static boolean logined=false;
    public static void main(String[] args) {
        User user=new User();
        int k=0;
        while( (k=Main(user))>=1&&k<5){
            switch (k){
                case 1:
                    System.out.print((k=user.login(user))==-1?"此用戶不存在!\n":"");
                    System.out.print((k==-2)?"===<<警告>>用戶:["+user.userName+"]已處于登錄狀態(tài),無需重復(fù)登錄!\n":"");
                    break;
                case 2:
                    user.regist();
                    break;
                case 3:
                    user.getLuckly();
                    break;
                default:System.exit(0);
            }
        }
    }
    static int Main(User user){
        System.out.println("**********************************************");
        System.out.println("********************主菜單********************");
        System.out.println("**********************************************");
        System.out.println("******          <1> 登   錄             ******");
        System.out.println("******          <2> 注   冊             ******");
        System.out.println("******          <3> 抽   獎(jiǎng)             ******");
        System.out.println("******          <4> 退   出             ******");
        System.out.println("**********************************************");
        System.out.println("==============================================");
        System.out.println(logined ? "-[已登錄]-  (1)用戶名:"+user.userName+"   (2)用戶賬號:"+user.userId:"-[未登錄]-   (1)用戶名:NaN   (2)用戶賬號:NaN");
        System.out.println("==============================================");
        System.out.print("###===>請輸入您的選擇:");
        return (new Scanner(System.in)).nextInt();
    }
}
public class User{
    String userName,userId,userPwd;
    public User(){}
    public User(String userName, String userId, String userPwd) {
        this.userName = userName;
        this.userId = userId;
        this.userPwd = userPwd;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        if(!userName.equals("")) {
            ArrayList<String> temp;
            this.userId=(temp=(new operatorFile(this.userName = userName)).getUserMess(0)).get(1);
            this.userPwd=temp.get(2);
        }
    }
    public String getUserId() {
        return userId;
    }
    public String setUserId() {
        String userId="";
        while((new operatorFile(userId=String.valueOf ((int) (Math.random()*9000+1000)))).getUserMess(1).size()>0){ }
        return (this.userId = userId);
    }
    public String getUserPwd() {
        return userPwd;
    }
    public int login(User u) {
        int inputTimes=3;
        Scanner scanner=new Scanner(System.in);
        operatorFile getUserMessage=new operatorFile();
        System.out.print("======>請輸入您的用戶名:");
        String uName="";
        getUserMessage.setUser(uName=scanner.nextLine());
        ArrayList<String> userMess=getUserMessage.getUserMess(0);
        if(userMess.size()<1)       return -1;//返回-1表示用戶不存在
        if (uName.equals(userName))     return -2;//返回-2表示用戶重復(fù)登錄
        System.out.print("======>請輸入您的登錄密碼:");
        while(!scanner.next().equals(userMess.get(2))&&inputTimes>0)
            System.out.print("===>密碼輸入錯(cuò)誤!"+((--inputTimes)>0?"您還剩"+inputTimes+"次機(jī)會!":"三次機(jī)會已經(jīng)用完了!輸入任意退出"));
        System.out.println(inputTimes>0?"==>登錄成功!您本次輸入密碼"+(4-inputTimes)+"次!":"==>登錄失?。?);
        setUserName(inputTimes>0?uName:"");
        Dos.logined=inputTimes>0?true:false;
        return 0;
    }
    public void regist() {
        User u=new User();
        Scanner scanner=new Scanner(System.in);
        System.out.print("===>請輸入新的用戶名:");
        String name;
        while(new operatorFile(name=scanner.nextLine()).getUserMess(0).size() > 0)
            System.out.print("已存在此用戶,注冊失?。n===>請重新輸入新的用戶名:");
        System.out.print("======>請?jiān)O(shè)置您的(六位數(shù)字)登錄密碼:");
        String regex = "[0-9]{6}", pwd;
        while (!(pwd = scanner.nextLine()).matches(regex))
            System.out.print("==>密碼格式不正確,請重新設(shè)置您的(六位數(shù)字)登錄密碼:");
        System.out.println("已為用戶:"+(u.userName=name)+" 生成唯一ID: "+(u.userPwd=pwd));
        (new operatorFile()).writeUserMess(u);
        System.out.println("=======>注冊成功!");
    }
    public static HashMap<String,String> lucklyUsers=new HashMap<>();
    public void getLuckly() {
        if (!Dos.logined)   {
            System.out.println("===>警告:沒有用戶登錄,無法抽獎(jiǎng)!");
            return ;
        }
        while(lucklyUsers.size()<5)
        {
            String id="";
            ArrayList<String> u;
            while((u=(new operatorFile(id=String.valueOf ((int) (Math.random()*9000+1000)))).getUserMess(1)).size()<1){ }
            lucklyUsers.put(u.get(1),u.get(0));
        }
        Iterator iterator=lucklyUsers.entrySet().iterator();
        int no=1;
        boolean LUCKLY=false;
        System.out.println("====>恭喜以下用戶獲得幸運(yùn)稱號:");
        while(iterator.hasNext()){
            Map.Entry entry=(Map.Entry) iterator.next();
            System.out.println("幸運(yùn)用戶["+(no++)+"]  用戶名:"+entry.getValue()+"   用戶編號:"+entry.getKey());
            LUCKLY = entry.getKey().equals(this.userId) ? true : LUCKLY;
        }
        System.out.println(LUCKLY?"=========>恭喜您在本次抽獎(jiǎng)中獲得幸運(yùn)稱號!":"=========>很遺憾,今日您未獲獎(jiǎng) !-_-!");
    }
    public String toString(){
        return this.userName+" "+this.userId+" "+this.userPwd;
    }
}
public class operatorFile {
    String user;
    public void setUser(String user) {
        this.user = user;
    }
    public operatorFile(String user) {
        this.user = user;
    }
    public operatorFile() { }
    public ArrayList<String> getUserMess(int index){
        ArrayList<String> temp=new ArrayList<String>();
        File file=new File("user.txt");
        String line="";
        try{
            BufferedReader br=new BufferedReader(new FileReader(file));
            while ((line = br.readLine())!=null && line!="\n"){
                temp.clear();
                StringTokenizer sk=new StringTokenizer(line);
                while (sk.hasMoreTokens()) {
                    temp.add(sk.nextToken());
                }
                if (temp.get(index).equals(this.user))  break;
            }
        }
        catch(IOException e){}
        return (line==null)?new ArrayList<String>():temp;
    }
    public void writeUserMess(User u){
        try{
            BufferedWriter bw=new BufferedWriter(new FileWriter(new File("user.txt"),true));
            bw.write(u.toString()+"\n");
            bw.close();
        }
        catch (IOException e){ }
    }
}

以上就是使用 java 制作抽獎(jiǎng)系統(tǒng)的全部內(nèi)容,請務(wù)必三連。


0 人點(diǎn)贊