JAVA實(shí)現(xiàn)用戶抽獎(jiǎng)功能(附完整代碼)
需求分析
1)實(shí)現(xiàn)三個(gè)基本功能:登錄、注冊(cè)、抽獎(jiǎng)。
2)登錄:用戶輸入賬號(hào)密碼進(jìn)行登錄,輸入賬號(hào)后會(huì)匹配已注冊(cè)的用戶,若輸入用戶不存在則退出,密碼有三次輸入機(jī)會(huì),登錄成功后主界面會(huì)顯示已登錄用戶的賬號(hào)信息。
3)注冊(cè):用戶首先輸入賬號(hào)名稱,系統(tǒng)查詢此名稱是否存在,如存在則請(qǐng)求用戶換一個(gè)名稱,否則進(jìn)入密碼輸入,密碼要求6位數(shù)字字符串,注冊(cè)成功后,系統(tǒng)隨機(jī)分配一個(gè)與已有用戶不重復(fù)的四位數(shù)字id編號(hào)。
4)抽獎(jiǎng):功能實(shí)現(xiàn)前提:需有用戶處于登錄狀態(tài)。該前提滿足時(shí),系統(tǒng)從已存在用戶中隨機(jī)抽取5位不同的用戶標(biāo)記為幸運(yùn)用戶,并判斷正在登錄狀態(tài)的用戶是否被抽中。
5)數(shù)據(jù)存儲(chǔ):采用文件系統(tǒng),導(dǎo)入java.io.*包,
6)數(shù)據(jù)結(jié)構(gòu):登錄用戶信息保存于ArrayList,幸運(yùn)用戶編號(hào)和id保存于長(zhǎng)度為5的HasMap<String id,String name> 其中id為Key,name為Value。
實(shí)現(xiàn)結(jié)果
1)登錄:
2)注冊(cè):
3)抽獎(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),無(wú)需重復(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> 注 冊(cè) ******"); System.out.println("****** <3> 抽 獎(jiǎng) ******"); System.out.println("****** <4> 退 出 ******"); System.out.println("**********************************************"); System.out.println("=============================================="); System.out.println(logined ? "-[已登錄](méi)- (1)用戶名:"+user.userName+" (2)用戶賬號(hào):"+user.userId:"-[未登錄](méi)- (1)用戶名:NaN (2)用戶賬號(hào):NaN"); System.out.println("=============================================="); System.out.print("###===>請(qǐng)輸入您的選擇:"); 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("======>請(qǐng)輸入您的用戶名:"); 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("======>請(qǐng)輸入您的登錄密碼:"); while(!scanner.next().equals(userMess.get(2))&&inputTimes>0) System.out.print("===>密碼輸入錯(cuò)誤!"+((--inputTimes)>0?"您還剩"+inputTimes+"次機(jī)會(huì)!":"三次機(jī)會(huì)已經(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("===>請(qǐng)輸入新的用戶名:"); String name; while(new operatorFile(name=scanner.nextLine()).getUserMess(0).size() > 0) System.out.print("已存在此用戶,注冊(cè)失??!\n===>請(qǐng)重新輸入新的用戶名:"); System.out.print("======>請(qǐng)?jiān)O(shè)置您的(六位數(shù)字)登錄密碼:"); String regex = "[0-9]{6}", pwd; while (!(pwd = scanner.nextLine()).matches(regex)) System.out.print("==>密碼格式不正確,請(qǐng)重新設(shè)置您的(六位數(shù)字)登錄密碼:"); System.out.println("已為用戶: "+(u.userName=name)+" 生成唯一ID: "+(u.userPwd=pwd)); (new operatorFile()).writeUserMess(u); System.out.println("=======>注冊(cè)成功!"); } public static HashMap<String,String> lucklyUsers=new HashMap<>(); public void getLuckly() { if (!Dos.logined) { System.out.println("===>警告:沒(méi)有用戶登錄,無(wú)法抽獎(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)稱號(hào):"); while(iterator.hasNext()){ Map.Entry entry=(Map.Entry) iterator.next(); System.out.println("幸運(yùn)用戶["+(no++)+"] 用戶名:"+entry.getValue()+" 用戶編號(hào):"+entry.getKey()); LUCKLY = entry.getKey().equals(this.userId) ? true : LUCKLY; } System.out.println(LUCKLY?"=========>恭喜您在本次抽獎(jiǎng)中獲得幸運(yùn)稱號(hào)!":"=========>很遺憾,今日您未獲獎(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){ } } }
總結(jié)
到此這篇關(guān)于JAVA實(shí)現(xiàn)用戶抽獎(jiǎng)功能(附完整代碼)的文章就介紹到這了,更多相關(guān)JAVA實(shí)現(xiàn)用戶抽獎(jiǎng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 基于Java實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
- 簡(jiǎn)單實(shí)現(xiàn)java抽獎(jiǎng)系統(tǒng)
- 純java代碼實(shí)現(xiàn)抽獎(jiǎng)系統(tǒng)
- Java抽獎(jiǎng)算法第二例
- Java實(shí)現(xiàn)雙色球抽獎(jiǎng)隨機(jī)算法示例
- Java簡(jiǎn)易抽獎(jiǎng)系統(tǒng)小項(xiàng)目
- Java抽獎(jiǎng)?chuàng)屬?gòu)算法
- java實(shí)現(xiàn)幸運(yùn)抽獎(jiǎng)系統(tǒng)
- JAVA使用隨機(jī)數(shù)實(shí)現(xiàn)概率抽獎(jiǎng)
- Java使用HashMap映射實(shí)現(xiàn)消費(fèi)抽獎(jiǎng)功能
相關(guān)文章
Java Web學(xué)習(xí)之Cookie和Session的深入理解
這篇文章主要給大家介紹了關(guān)于Java Web學(xué)習(xí)之Cookie和Session的相關(guān)資料,需要的朋友可以參考下2018-04-04三分鐘帶你了解SpringBoot真正的啟動(dòng)引導(dǎo)類
這篇文章主要介紹了三分鐘帶你了解SpringBoot真正的啟動(dòng)引導(dǎo)類,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11java+opencv實(shí)現(xiàn)人臉識(shí)別功能
這篇文章主要介紹了java+opencv實(shí)現(xiàn)人臉識(shí)別功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)系統(tǒng)多級(jí)文件夾復(fù)制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Java中關(guān)于String StringBuffer StringBuilder特性深度解析
這篇文章主要介紹了Java中關(guān)于String StringBuffer StringBuilder特性深度解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09Spring事件監(jiān)聽(tīng)機(jī)制之@EventListener實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Spring事件監(jiān)聽(tīng)機(jī)制之@EventListener實(shí)現(xiàn)方式詳解,ApplicationContext的refresh方法還是初始化了SimpleApplicationEventMulticaster,發(fā)送事件式還是先獲取ResolvableType類型,再獲取發(fā)送監(jiān)聽(tīng)列表,需要的朋友可以參考下2023-12-12