java课程设计-彩票购买抽奖程序
java课程设计-彩票购买抽奖程序
一、项目介绍
题目要求
功能要求: 模拟福利彩票36选7,实现彩票的抽奖与中奖通知功能。
1.允许注册用户,用户信息包括用户id,用户名,密码,账户金额,电话号码等属性。
2.允许注册用户购买彩票:手动选号、随机选号,并允许设置投注数。
3.抽奖功能:要求屏幕上能够显示抽奖的号码滚动过程,按“停止”按钮给出抽奖结果。特等奖,7个号码全中。一等奖,6个号码全中。抽出号码后,显示中奖用户id,中奖的倍数。
4.彩票抽奖通知功能:抽出号码后,当用户登录系统后,系统提示是否中奖。
5.设计一自动注册用户(注册10万个)、并自动买彩票为用户买彩票的程序。然后模拟抽奖。
二、团队成员介绍
本人
三、项目git地址
https://gitee.com/wangyanyuvc/lottery/tree/master
四、项目git提交记录截图
五、项目功能架构图与主要功能流程图
项目功能架构图
UML类图
包分类
六、项目运行截图
七、项目关键代码
用户登入代码
public Boolean login(int accountnumber, String password) {
File f = new File();
Map<Integer, User> map = new LinkedHashMap<Integer, User>();
f.userInput(map);
Collection<User> users = map.values();
Iterator<User> iterator = users.iterator();
while (iterator.hasNext()) {
User eg = iterator.next();
if ((accountnumber == eg.getId() || accountnumber == eg.getTel()) && password.equals(eg.getPassword())) {
return true;
}
}
return false;
}
充值代码
public int recharge(User user, int money) {
Map<Integer, User> map = new LinkedHashMap<Integer, User>();
File f = new File();
f.userInput(map);
user.recharge(money);
Collection<User> users = map.values();
Iterator<User> iterator = users.iterator();
int balance = 0;
while (iterator.hasNext()) {
User eg = iterator.next();
if (eg.getId() == user.getId()) {
balance = eg.getBalance();
eg.setBalance(user.getBalance());
break;
}
}
f.userOutput(map);
return balance;
}
public void recharge(int money) {// 账户充值
int totalmoney = money + getBalance();
setBalance(totalmoney);
}
开奖代码
public int[] drawLottery() {
File f = new File();
Map<Integer, Backstage> map = new LinkedHashMap<Integer, Backstage>();
int period = f.winNumIntput(map);
int[] l = new int[7];
Backstage b = new Backstage(period, l);
b.setBalls(b.drawLottery());
map.put(period, b);
f.winNumOutput(map);
return b.getBalls();
}
创建测试代码
public void createtest() {
File f = new File();
Map<Integer, User> map = new LinkedHashMap<Integer, User>();
Map<Integer, Backstage> map2 = new LinkedHashMap<Integer, Backstage>();
List<Lottery> arr = new ArrayList<Lottery>();
f.lotteryInput(arr);
int n = f.winNumIntput(map2);
for (int i = 1; i <= 100000; i++) {
User user = new User(i, "1", "1", 100, i);
map.put(i, user);
int[] balls = new int[8];
Lottery lottery = new Lottery(n, balls);
lottery.setId(i);
lottery.testSelect(user);
arr.add(lottery);
}
f.lotteryOutput(arr);
f.userOutput(map);
}
开奖测试代码
public void lotterytest() {
File f = new File();
Map<Integer, Backstage> map = new LinkedHashMap<Integer, Backstage>();
List<Lottery> arr = new ArrayList<Lottery>();
f.lotteryInput(arr);
int period = f.winNumIntput(map);
int[] l = new int[7];
Backstage b = new Backstage(period, l);
b.setBalls(b.drawLottery());
for (Lottery eg : arr) {
if (eg.getPeriod() == period) {
System.out.println(eg.toString());
System.out.println("您获得了" + b.getLevel(eg.getBalls(), b.getBalls()) + "等奖");
}
}
map.put(period, b);
f.winNumOutput(map);
}
注册代码
public int register(String name, String password, int tel) {
File f = new File();
Map<Integer, User> map = new LinkedHashMap<Integer, User>();
int count = f.userInput1(map);
User user = new User(count, name, password, 100, tel);
map.put(count, user);
f.userOutput(map);
return count;
}
通知中奖代码
File f = new File();
Map<Integer, Backstage> map11 = new LinkedHashMap<Integer, Backstage>();
List<Lottery> arr = new ArrayList<Lottery>();
int n2 = f.winNumIntput(map11) - 1;
int id = user.getId();
String str = new String();
f.lotteryInput(arr);
Collection<Backstage> winnums = map11.values();
Iterator<Backstage> iterator1 = winnums.iterator();
int winballs[] = new int[7];
while (iterator1.hasNext()) {
Backstage eg1 = iterator1.next();
if (eg1.getPeriod() == n2) {
winballs = eg1.getBalls();
}
}
for (Lottery eg2 : arr) {
if (eg2.getId() == id && eg2.getPeriod() == n2) {
str += "中奖号码" + Arrays.toString(winballs) + "\n" + "您的" + eg2.toString() + "\n" + "您获得了"
+ eg2.getLevel(eg2.getBalls(), winballs) + "等奖" + "\n";
}
}
JOptionPane.showMessageDialog(null, str);
购票代码
手选购票
public void handSelect(User user, int balls[]) {
Map<Integer, User> map = new LinkedHashMap<Integer, User>();
Map<Integer, Backstage> map2 = new LinkedHashMap<Integer, Backstage>();
File f = new File();
f.userInput(map);
int period = f.winNumIntput(map2);
List<Lottery> arr = new ArrayList<Lottery>();
Lottery lottery = new Lottery(period, balls);
lottery.handSelect(user, balls);
arr.add(lottery);
f.lotteryOutput(arr);
Collection<User> users = map.values();
Iterator<User> iterator = users.iterator();
while (iterator.hasNext()) {
User eg = iterator.next();
if (eg.getId() == user.getId()) {
eg.setBalance(user.getBalance());
break;
}
}
f.userOutput(map);
}
public void handSelect(User user, int balls[]) {// 手动选号
int count = balls[7];
int nowbalance = user.getBalance() - count * 2;
user.setBalance(nowbalance);
setId(user.getId());
setBalls(balls);
setPrice(count * 2);
}
机选购票
public int[] machineSelect(User user, int count) {
Map<Integer, User> map = new LinkedHashMap<Integer, User>();
Map<Integer, Backstage> map2 = new LinkedHashMap<Integer, Backstage>();
File f = new File();
f.userInput(map);
List<Lottery> arr = new ArrayList<Lottery>();
int period = f.winNumIntput(map2);
int[] balls = new int[8];
Lottery lottery = new Lottery(period, balls);
balls = lottery.machineSelect(user, count);
lottery.setPeriod(period);
arr.add(lottery);
f.lotteryOutput(arr);
Collection<User> users = map.values();
Iterator<User> iterator = users.iterator();
while (iterator.hasNext()) {
User eg = iterator.next();
if (eg.getId() == user.getId()) {
eg.setBalance(user.getBalance());
break;
}
}
f.userOutput(map);
return balls;
}
public int[] machineSelect(User user, int count) {// 机器选号
Set<Integer> set = new HashSet<Integer>();
int[] balls = new int[8];
do {
int redBall = (int) (Math.random() * 33) + 1;
set.add(redBall);
} while (set.size() <= 6);
int buleBall = (int) (Math.random() * 16) + 1;
set.add(buleBall);
int i = 0;
for (int num : set) {
balls[i] = num;
i++;
if (i == 6) {
break;
}
}
int blueBall = (int) (Math.random() * 16) + 1;
balls[6] = blueBall;
balls[7] = count;
int nowbalance = user.getBalance() - count * 2;
user.setBalance(nowbalance);
setId(user.getId());
setBalls(balls);
setPrice(count * 2);
return balls;
}
兑奖
public int getLevel(int[] balls, int[] luckyBalls) {
int level = 0;
int redCount = 0;
int blueCount = 0;
for (int i = 0; i < 7; i++) {
if (i != 6) {
for (int j = 0; j < 6; j++) {
if (balls[i] == luckyBalls[j]) {
redCount++;
}
}
} else {
if (balls[6] == luckyBalls[6]) {
blueCount++;
}
}
}
if (redCount == 6 && blueCount == 1) {
level = 1;
}
if (redCount == 6 && blueCount == 0) {
level = 2;
}
if (redCount == 5 && blueCount == 1) {
level = 3;
}
if ((redCount == 5 && blueCount == 0) || (redCount == 4 && blueCount == 1)) {
level = 4;
}
if ((redCount == 4 && blueCount == 0) || (redCount == 3 && blueCount == 1)) {
level = 5;
}
if ((redCount == 2 && blueCount == 1) || (redCount == 1 && blueCount == 1)
|| (redCount == 0 && blueCount == 1)) {
level = 6;
}
return level;
}
八、程序的不足与改进
此程序使用的是文件的存储,如果使用数据库存储会更方便一些,文件存储读取操作教复杂。界面可以做更多的美化。动态的开奖没有实现,只使用静态开奖的方式。有时间的话会继续完善。