JAVA實現(xiàn)用戶抽獎功能(附完整代碼)
1)實現(xiàn)三個基本功能:登錄、注冊、抽獎。
2)登錄:用戶輸入賬號密碼進行登錄,輸入賬號后會匹配已注冊的用戶,若輸入用戶不存在則退出,密碼有三次輸入機會,登錄成功后主界面會顯示已登錄用戶的賬號信息。
3)注冊:用戶首先輸入賬號名稱,系統(tǒng)查詢此名稱是否存在,如存在則請求用戶換一個名稱,否則進入密碼輸入,密碼要求6位數(shù)字字符串,注冊成功后,系統(tǒng)隨機分配一個與已有用戶不重復(fù)的四位數(shù)字id編號。
4)抽獎:功能實現(xiàn)前提:需有用戶處于登錄狀態(tài)。該前提滿足時,系統(tǒng)從已存在用戶中隨機抽取5位不同的用戶標記為幸運用戶,并判斷正在登錄狀態(tài)的用戶是否被抽中。
5)數(shù)據(jù)存儲:采用文件系統(tǒng),導(dǎo)入java.io.*包,
6)數(shù)據(jù)結(jié)構(gòu):登錄用戶信息保存于ArrayList,幸運用戶編號和id保存于長度為5的HasMap<String id,String name> 其中id為Key,name為Value。
實現(xiàn)結(jié)果1)登錄:
2)注冊:
3)抽獎:
運行代碼之前務(wù)必在user.txt中創(chuàng)建五個以上的用戶。
完整代碼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> 抽 獎 ******'); 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('===>密碼輸入錯誤!'+((--inputTimes)>0?'您還剩'+inputTimes+'次機會!':'三次機會已經(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('======>請設(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('===>警告:沒有用戶登錄,無法抽獎!'); 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('====>恭喜以下用戶獲得幸運稱號:'); while(iterator.hasNext()){ Map.Entry entry=(Map.Entry) iterator.next(); System.out.println('幸運用戶['+(no++)+'] 用戶名:'+entry.getValue()+' 用戶編號:'+entry.getKey()); LUCKLY = entry.getKey().equals(this.userId) ? true : LUCKLY; } System.out.println(LUCKLY?'=========>恭喜您在本次抽獎中獲得幸運稱號!':'=========>很遺憾,今日您未獲獎 !-_-!'); } 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實現(xiàn)用戶抽獎功能(附完整代碼)的文章就介紹到這了,更多相關(guān)JAVA實現(xiàn)用戶抽獎內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python如何實現(xiàn)word批量轉(zhuǎn)HTML2. python excel和yaml文件的讀取封裝3. python3實現(xiàn)往mysql中插入datetime類型的數(shù)據(jù)4. python爬蟲實戰(zhàn)之制作屬于自己的一個IP代理模塊5. moment轉(zhuǎn)化時間戳出現(xiàn)Invalid Date的問題及解決6. java虛擬機詳述-第三章(二)7. Android中的緩存8. php實現(xiàn)當前用戶在線人數(shù)9. 關(guān)于 Android WebView 的內(nèi)存泄露問題10. Python中內(nèi)建模塊collections如何使用
