JAVA课程设计——彩票购买抽奖程序
一、团队分工介绍:
任务分配 | ||
团队成员 | 黄坤(组长) |
1.允许注册用户,用户信息包括用户id,用户名,密码,账户金额,电话号码等属性。 2.彩票抽奖通知功能:抽出号码后,当用户登录系统后,系统提示是否中奖。 3.构建总体GUI菜单界面 4.代码整合 |
徐彬晶 |
1.允许注册用户购买彩票:手动选号、随机选号,并允许设置投注数,同时用户金额自动减少。 2.抽奖功能:要求屏幕上能够显示抽奖的号码,按“停止”按钮给出抽奖结果。特等奖,7个号码全中。一等奖,6个号码全中。抽出号码后,显示中奖用户名。 3.将项目上传到git仓库 4.实现用户充值金额 5.将不同用户的信息存入相对应用户的文本中
|
二、参考代码:
(27条消息) 事件监听器的添加(登陆界面的完善)_Mr.kun的博客-CSDN博客
Java第05次实验提纲(Java图形界面编程) - zhrb - 博客园 (cnblogs.com)
三、本项目的git地址:
Welfare Lottery: 1111111111 (gitee.com)
四、项目git提交记录截图:
五、前期调查:
六、项目功能架构图及主要功能流程图:
1、项目功能架构图:
2、主要功能流程图:
七、面向对象设计包图、类图:
八、项目运行截图:
九、项目关键代码描述:
1、NumberSelection
// 自动输入号码 public String automaticInput() { ArrayList list = new ArrayList(); int i = 0; while (i < 7) { //随机生成1~37内的数字 int number = (int) (Math.random() * 37+1); //确保生成的7个随机数字不重复 if (!list.contains(number)) { list.add(number); i++; }else{ continue; } } return list.get(0)+" "+list.get(1)+" "+list.get(2)+" "+list.get(3)+" "+list.get(4)+" "+list.get(5)+" "+list.get(6); }
//将购买的彩票信息存入TXT public void inputTxt(String name) { //将号数保存进txt try { File file=new File("UsersNumber.txt");//增加用户的号数 //当文件不存在的时候,生成文件,提高代码可靠性 if((!file.exists())) file.createNewFile(); FileWriter fileWritter=new FileWriter(file.getName(),true); for (int j = 0; j < 7; j++) { //在保证彩票号为都为有效数字时,才允许录入文本 if (yourNumber[j] >= 1 && yourNumber[j] <= 36) { fileWritter.write(yourNumber[j]+" "); } } //保证录入格式为:彩票号+用户名 fileWritter.write(" "+name); fileWritter.write("\n"); fileWritter.close(); }catch(IOException e) { e.printStackTrace(); } }
2、RegisterAndLogin
// 注册 public void regist(String name,String password,double amount,String telNumber) { u1.setName(name); u1.setPassword(password); u1.setAccountAmount(amount); u1.setTelNumber(telNumber); try { //生成专门存储该用户信息的文本文件 File file = new File("C:\\Users\\lin'juan\\eclipse-workspace\\welfarelottery\\src\\" +u1.getName() + ".txt"); if ((!file.exists())) { file.createNewFile(); } FileWriter fileWritter = new FileWriter("C:\\Users\\lin'juan\\eclipse-workspace\\welfarelottery\\src\\" +u1.getName() + ".txt", true); //将用户信息录入文本 fileWritter.write(u1.getName() + "\n" + u1.getPassword() + "\n" + u1.getAccountAmount() + "\n" + u1.getTelNumber()); fileWritter.close(); } catch (IOException e) { e.printStackTrace(); } }
// 获取文本数据 public String getdata(String name) throws IOException { //获得用户信息 String fileName = "D:\\Users\\HP\\eclipse-workspace\\welfarelottery\\" + name + ".txt";// 读取用户信息 String str = null; try (Scanner scanner = new Scanner(new FileReader(fileName))) { //按照UTF-8格式录入 InputStreamReader Reader = new InputStreamReader(new FileInputStream(fileName), "UTF-8"); BufferedReader bufferedReader = new BufferedReader(Reader); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { list.add(lineTxt); } //将信息录入u1中 u1.setName(list.get(0)); u1.setPassword(list.get(1)); u1.setAccountAmount(Double.valueOf(list.get(2))); u1.setTelNumber(list.get(3)); str = u1.getName()+u1.getPassword(); Reader.close(); } catch (FileNotFoundException e) { return null; } return str; } public void clear(){ list.clear(); }
3、Recharge
//充值并写入文本 public void recharge(double topupAmount) { double amount=Double.valueOf(user.getAccountAmount()); //控制小数点位数 DecimalFormat df=new DecimalFormat("0.00"); amount+=topupAmount; user.setAccountAmount(amount); //清空原本文本内容,重新写入 clearTxt(); try { FileWriter fileWritter = new FileWriter("C:\\Users\\lin'juan\\eclipse-workspace\\welfarelottery\\src\\"+ user.getName() + ".txt", true); fileWritter.write(user.getName() + "\n" + user.getPassword() + "\n" +df.format(user.getAccountAmount()) + "\n" + user.getTelNumber()); fileWritter.close(); } catch (IOException e) { e.printStackTrace(); } }
4、JudgeWinner
public class JudgeWinner { // 判断是否中奖 public int judgeWinner(String name) throws FileNotFoundException { UserData u1 = new UserData(); WinNumbers wn = new WinNumbers(); String fileName2 = "C:\\Users\\lin'juan\\eclipse-workspace\\welfarelottery\\src\\UsersNumber.txt"; try (Scanner sc = new Scanner(new FileReader(fileName2))) { while (sc.hasNextLine()) { int count = 0; String line = sc.nextLine(); if (line.contains(name)) { String sel = line.substring(0,13); String[] arr1 = sel.split(" "); //两层循环比较用户所购买的彩票号码中包含几个中奖号码的数字 for (String s : arr1) { for (int i = 0; i < 7; i++) { if (s.equals(wn.readWinNumbers()[i])) { count++; } } } if (count == 6) {// 一等奖 return 2; } else if (count == 7) {// 特等奖 return 1; } } } } return 0; } }
5、 LotteryMenu
//找到所有获奖用户并显示 try(Scanner sc = new Scanner(new FileReader(fileName2))) { while (sc.hasNextLine()) { int count=0; String line = sc.nextLine(); int lenLine=line.length(); String sel=line.substring(0,13); userNumber.arr = sel.split(" "); userNumber.name=line.substring(14,lenLine); //将String[]数组转为int[]数组 int[] arri = Arrays.asList(userNumber.arr).stream().mapToInt(Integer::parseInt).toArray(); for (int j = 0; j < arri.length; j++) { if(list.contains(arri[j])) count++; } if(count==6 || count==7) //将所有获奖用户的用户名拼接成一个String型字符串 s+=userNumber.name; t2.setText(s); } }catch(IOException e1) { e1.printStackTrace(); }
6、RLMenu
/* RLMenu可以让用户进行登录注册,登录时 若用户名文件不存在,即提示用户不存在,若用户输入的姓名密码与文本不匹配,登录失败,若匹配成功,根据密码判断普通用户和管理员 */ login.addActionListener(new ActionListener() {//登录按钮的事件监听器 @Override public void actionPerformed(ActionEvent e) { String s1 = nameText.getText(); String s2 = new String(password.getPassword()); String flag; try { flag = rl.getdata(s1); if (flag == null) {//用户不存在 JOptionPane.showMessageDialog(null, "登录失败!\n找不到此用户!"); }else if(!flag.equals(s1+s2)) { JOptionPane.showMessageDialog(null, "登录失败!"); } else if(flag.equals(s1+s2)){//用户存在 if (!s2.subSequence(0, 4).equals("xhxk")) {//普通用户 JOptionPane.showMessageDialog(null, "登录成功!"); JudgeWinner jw = new JudgeWinner(); int count = jw.readSelNumber(s1);//中奖提示 if (count==1) { JOptionPane.showMessageDialog(null, "恭喜获得特等奖!"); }else if(count==2) { JOptionPane.showMessageDialog(null, "恭喜获得一等奖!"); } m.lotteryMenu(s1);//跳转选号界面 frame.dispose(); }else if (s2.substring(0, 4).equals("xhxk") ) {//管理员 JOptionPane.showMessageDialog(null, "欢迎管理员!"); LotteryMenu a=new LotteryMenu();//获得中奖用户界面 a.main(null); } } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } });
7、NumberSelectionMenu
/* NumberSelectionMenu中用户需先输入姓名确保号码信息保存正确,之后选择投注数和选号方式,若选号过程中余额不足,则不能继续进行选号,只能先充值 */ Label1.addActionListener(new ActionListener() {//手动选号监听器 @Override public void actionPerformed(ActionEvent e) { int count = Integer.valueOf(labelText.getText()); String s1 = nameText.getText(); AmountDecreased ad = new AmountDecreased(); Recharge r = new Recharge(); for (int i = 0; i < count; i++) { JFrame jf = new JFrame("温馨提示:请输入7个1-36范围内的号数"); jf.setSize(300,350); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout layout = new FlowLayout(); jf.setLayout(layout); JLabel label = new JLabel("号数:"); jf.add(label); JTextField labelText = new JTextField(); labelText.setPreferredSize(new Dimension(220,30)); jf.add(labelText); jf.pack(); jf.setVisible(true); JButton verify = new JButton("确定"); jf.add(verify); verify.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { double n; try { n = r.addToUser(s1); if (n<2) { JOptionPane.showMessageDialog(null, "余额不足,请充值!"); jf.dispose(); }else if(n>=2){ String s = labelText.getText(); String[] arr = s.split(" "); try { ad.addToUser(s1); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } ad.decrease(); double newCharge = r.addToUser(s1); JOptionPane.showMessageDialog(null, "选号成功!您的余额为:"+newCharge+"元\n祝您生活愉快!"); tp.inputTxt(arr); tp.addName(nameText.getText()); jf.dispose(); } } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } } }); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(3); jf.setVisible(true); } } }); Label3.addActionListener(new ActionListener() {//余额 @Override public void actionPerformed(ActionEvent e) { String s1 = nameText.getText(); Recharge r = new Recharge(); AmountDecreased ad = new AmountDecreased(); try { ad.addToUser(s1); r.addToUser(s1); JFrame f1 = new JFrame("请输入充值金额:"); f1.setSize(300,350); f1.setLocationRelativeTo(null); f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); FlowLayout layout = new FlowLayout(); f1.setLayout(layout); JTextField rechargeText = new JTextField(); rechargeText.setPreferredSize(new Dimension(220,30)); f1.add(rechargeText); f1.pack(); f1.setVisible(true); JButton verify = new JButton("确定"); f1.add(verify); verify.addActionListener(new ActionListener() {//余额充值 确定按钮的事件监听器 @Override public void actionPerformed(ActionEvent e) { r.recharge(Double.valueOf(rechargeText.getText())); try { JOptionPane.showMessageDialog(null, "充值成功!"+"您的账户余额为:"+r.addToUser(s1)+"元"); } catch (HeadlessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } f1.dispose(); } }); f1.setLocationRelativeTo(null); f1.setDefaultCloseOperation(3); f1.setVisible(true); } catch (Exception e2) { // TODO: handle exception e2.printStackTrace(); } } });
十、项目总结(不足与展望、想要进一步完成的任务):
1、重新梳理代码,减少其中代码冗余
2、美化GUI界面
3、提高代码的可靠性