#使用Java控制台编写关于宝可梦的抽奖系统的经验分享#
大家好!今天我想分享一下我以前使用Java控制台和数据库编写的一个关于宝可梦的一个小型抽奖系统。这个项目是我在学习完javase和数据库基础之后,写的第一个自己构思的项目。这个项目由于当时刚开始接触数据库,java也学的不扎实,用了很多例如在java中建数据库的表,数据库设计得非常随便等一些可笑的操作,这里记录并且分享给大家。
功能描述
这个抽奖系统主要包括以下几个功能:
- 登录功能
- 注册功能
- 找回密码功能
- 查看个人信息功能
- 登录成功之后,可以选择抽卡
- 查看背包宝可梦
- 获得抽卡道具等功能
- 双奖池抽取
系统核心
其中关于用户登录登出的系统的核心就是通过Arrays进行的对数据库中用户信息的储存,现在来看应该就相当于一个缓存,将数据库中的数据缓存到Arrays中,做到只用取一次就可以一直的去使用。抽奖系统的核心是随机数生成。我使用了Java的Random类来实现这一点。通过Random类生成一个随机索引,这个索引对应数组中的一个元素,也就是抽取到的宝可梦。这里的用户数据就是简单的存储在数据库中,使用的是jdbc技术,而由于当时技术的学的不扎实,进入系统之后的数据就没有进行记录。
下面进行代码的讲解和分享。
用户类
package kaohe05;
import java.util.ArrayList;
public class User {
private String userName;
private int password;
private String phonenumber;
private int cardCount;//计算所拥有的卡片
private ArrayList<Item> item;//存放宝可梦
private int prop;//设置抽卡道具
public User(String userName, int password, String phonenumber, int cardCount,int prop) {
this.userName = userName;
this.password = password;
this.phonenumber = phonenumber;
this.cardCount = cardCount;
this.prop = 0;
}
// 关于抽卡道具增加的方法
public void addprop(){
prop++;
}
// 关于抽卡道具减少的方法
public void reduceprop(){
prop--;
}
public int getProp() {
return prop;
}
public void setProp(int prop) {
this.prop = prop;
}
public User() {
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public int getCardCount() {
return cardCount;
}
public void setCardCount(int cardCount) {
this.cardCount = cardCount;
}
public ArrayList<Item> getItem() {
return item;
}
public void setItem(ArrayList<Item> item) {
this.item = item;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password=" + password +
", phonenumber='" + phonenumber + '\'' +
", cardCount=" + cardCount +
", item=" + item +
", prop=" + prop +
'}';
}
}
这里的用户类中记录了用户名,密码,以及用于找回密码的手机号码。同时用一个集合绑定了用户抽奖抽到的宝可梦,以及抽奖道具等,类中书写了关于抽奖道具增减的方法。
卡池类
package kaohe05;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class CardPool {
private String name;
private List<Item> items;
public CardPool(String name) {
this.name = name;
this.items = new ArrayList<>();
}
public void addItem(Item item) {
items.add(item);
}
public Item drawItem() {
Random random = new Random();//随机抽取
int index = random.nextInt(items.size());
return items.get(index);
}
}
这里定义了卡池相关的类,定义了添加你的卡池名称以及你要抽取的宝可梦,还有随机抽取放入卡池中的宝可梦的函数。
关于抽卡的类
class CardSystem {
public Item drawCard(CardPool cardPool) {
Item item = cardPool.drawItem();
return item;
}
public Item[] drawCards(CardPool cardPool) {
Item[] items = new Item[3];
for (int i = 0; i < 3; i++) {
items[i] = cardPool.drawItem();
}
return items;
}
}
这里我设置了及三连抽的方法,用于对于抽奖的选择。
关于宝可梦的类
package kaohe05;
class Item {
private String name;//pkm名字
private String type;//pkm属性
private String quality;//pkm稀有度
public Item(String name, String type, String quality) {
this.name = name;
this.type = type;
this.quality = quality;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getQuality() {
return quality;
}
public void setQuality(String quality) {
this.quality = quality;
}
}
这里定义了宝可梦的一些相关的信息
主系统的类
package kaohe05;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class LoginInterface {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
while (true) {
String url = "jdbc:mysql://localhost:3306/这里改成你自己的数据库的表";
String user = "改成自己的";
String password = "改成自己的";
Connection conn = DriverManager.getConnection(url, user, password);
createTable(conn);
ArrayList<User> list = loadUser(conn);
System.out.println("=====欢迎来到宝可梦世界=======");
System.out.println("1.登录");
System.out.println("2.注册");
System.out.println("3.忘记密码");
System.out.println("4.查看个人信息");
System.out.println("5.退出");
System.out.println("请输入选项");
Scanner sc = new Scanner(System.in);
String choose = sc.next();
switch (choose) {
case "1": //登录
login(list);
break;
case "2"://注册
register(list,conn);
break;
case "3":
//忘记密码
forgetpassword(list,conn);
break;
case "4":
checkUser(list);
break;
case "5"://退出
System.out.println("谢谢使用,再见!");
System.exit(0);
default:
System.out.println("没有这个选项!");
}
conn.close();
}
}
// 当时是初学者傻傻地在java中使用数据库建表
private static void createTable(Connection conn) throws SQLException {
String query = "create table if not exists users (\n" +
" userName varchar(100) primary key,\n" +
" password int,\n" +
" phone varchar(100),\n" +
" cardCount int,\n" +
" prop int\n" +
")";
Statement statement = conn.createStatement();
statement.execute(query);//执行代码
}
private static ArrayList<User> loadUser(Connection conn) throws SQLException {
ArrayList<User> list = new ArrayList<>();
String query = "select * from users";
Statement sm = conn.createStatement();
ResultSet rs = sm.executeQuery(query);//操控数据库执行
while(rs.next()){
String userName = rs.getString("userName");
int password = rs.getInt(
"password");
String phonenumber = rs.getString("phone");
int cardCount = rs.getInt("cardCount");
int prop = rs.getInt("prop");
User us = new User(userName,password,phonenumber,cardCount,prop);
list.add(us);
}
return list;
}
private static void checkUser(ArrayList<User> list) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要查看的用户名对应的手机号码");
String phoneNumber = sc.next();
int index = Panduan1(list, phoneNumber);
if (index >= 0) {
System.out.println("用户名" + "\t" + "密码");
User user = list.get(index);
System.out.println(user.getUserName() + user.getPassword());
} else {
System.out.println("系统没有录入你要查询的手机号码。");
}
}//检验用户名
private static void forgetpassword(ArrayList<User> list,Connection conn) throws IOException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名");
String username = sc.next();
int index = Panduan(list, username);
if (index >= 0) {
User user = list.get(index);
System.out.println("请输入当时登记的手机号码");
String PhoneNumber = sc.next();
if (PhoneNumber.equals(user.getPhonenumber())) {
System.out.println("请输入新的密码");
int newNumber = sc.nextInt();
user.setPassword(newNumber);
System.out.println("密码更改成功。");
String query = "update users set password = ? where userName = ?";
PreparedStatement statement = conn.prepareStatement(query);
statement.setInt(1, newNumber);
statement.setString(2, username);
statement.executeUpdate();
}
} else {
System.out.println("用户名和密码不对应。");
}
}//找回密码
private static void login(ArrayList<User> list) throws IOException, ClassNotFoundException {
Scanner sc = new Scanner(System.in);
String userName;
while (true) {
System.out.println("请输入用户名");
userName = sc.next();
int index = Panduan(list, userName);
if (index >= 0) {
break;
} else {
System.out.println("用户名不存在。");
return;
}
}
int index = Panduan(list, userName);//获取对应的密码的用户
System.out.println("请输入密码");
int password = sc.nextInt();
User user = list.get(index);
if (password == user.getPassword()) {
System.out.println("登录成功");
System.out.println("=========欢迎进入游戏=========");
Scanner scanner = new Scanner(System.in);
CardSystem cardSystem = new CardSystem();
ArrayList<Item> items = new ArrayList<>();
User master = list.get(index);
master.setItem(items);
CardPool pool1 = new CardPool("初代卡池");
pool1.addItem(new Item("妙蛙种子", "草", "普通"));
pool1.addItem(new Item("小火龙", "火", "普通"));
pool1.addItem(new Item("杰尼龟", "水", "普通"));
pool1.addItem(new Item("超梦", "超能力", "传说"));
pool1.addItem(new Item("皮皮", "一般", "稀有"));
pool1.addItem(new Item("闪电鸟", "电 飞行", "传说"));
pool1.addItem(new Item("火焰鸟", "火 飞行", "传说"));
pool1.addItem(new Item("急冻鸟", "冰 飞行", "传说"));
CardPool pool2 = new CardPool("丰源卡池");
pool2.addItem(new Item("烈空坐", "飞 龙", "传说"));
pool2.addItem(new Item("盖欧卡", "水", "传说"));
pool2.addItem(new Item("固拉多", "地面", "传说"));
pool2.addItem(new Item("火球鼠", "火", "普通"));
pool2.addItem(new Item("小锯鳄", "水", "普通"));
pool2.addItem(new Item("草蛇", "草", "普通"));
pool2.addItem(new Item("小猪仔", "一般", "普通"));
pool2.addItem(new Item("大钢蛇", "钢", "稀有"));
pool2.addItem(new Item("珍珠贝", "水", "稀有"));
while (true) {
System.out.println("1.抽卡");
System.out.println("2.查看抽卡道具");
System.out.println("3.查看背包宝可梦");
System.out.println("4.获得抽卡道具");
System.out.println("5.退出游戏");
System.out.println("请输入你的选择");
String choose = sc.next();
switch (choose) {
case "1": {
//master.setItem(items);
while (true) {
System.out.println("请选择卡池:");
System.out.println("1. 初代卡池");
System.out.println("2. 丰源卡池");
int chose = scanner.nextInt();
CardPool selectedPool;
if (chose == 1) {
selectedPool = pool1;
} else if (chose == 2) {
selectedPool = pool2;
} else {
System.out.println("无效的选项,请重新选择。");
continue;
}
System.out.println("请选择抽卡方式:");
System.out.println("1. 单抽");
System.out.println("2. 三连抽");
int drawChoice = scanner.nextInt();
if (drawChoice == 1) {
if (master.getProp() == 0) {
System.out.println("抽卡道具不足,请获取道具后进行抽取。");
break;
}
Item drawnItem = cardSystem.drawCard(selectedPool);
System.out.println("恭喜你抽到了:" + drawnItem.getName());
master.getItem().add(drawnItem);
master.setCardCount(+1);
master.reduceprop();//抽卡道具-1
} else if (drawChoice == 2) {
if (master.getProp() < 3) {
System.out.println("抽卡道具不足,请获取后再抽取。");
break;
}
Item[] drawnItems = cardSystem.drawCards(selectedPool);
System.out.println("恭喜你抽到了");
for (int i = 0; i < 3; i++) {
System.out.println(drawnItems[i].getName());
}
for (int i = 0; i < drawnItems.length; i++) {
master.getItem().add(drawnItems[i]);
master.setCardCount(+1);
master.reduceprop();
}
} else {
System.out.println("无效的选项,请重新选择。");
}
System.out.println("继续抽卡请按Y");
String continueChoice = scanner.next();
if (!continueChoice.equalsIgnoreCase("Y")) {//忽略大小写
break;
}
}
}//游戏实现
break;
case "2":
checkprop(master);
break;
case "3":
examine_(master);//查看所抽取的宝可梦
break;
case "4":
getprop(master, list);//得到道具
break;
case "5":
return;
default:
System.out.println("没有此选项,请重新选择");
break;
}
}
} else {
System.out.println("密码错误");
}
}//登录
private static void checkprop(User master) {
System.out.println("剩余抽卡道具:" + master.getProp());
}
private static void getprop(User master, ArrayList<User> list) throws IOException {
System.out.println("猜数字游戏(游戏胜利可以获得一个)");
Scanner sc = new Scanner(System.in);
Random random = new Random();
while (true) {
int num = random.nextInt(100) + 1;
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("请输入猜测的数:");
int toGuess = scanner.nextInt();
if (toGuess > num) {
System.out.println("猜大了");
} else if (toGuess < num) {
System.out.println("猜小了");
} else {
System.out.println("猜对了!");
master.addprop();//道具+1
break;
}
}
System.out.println("请问还要继续玩吗?1.继续 2.退出");
while (true) {
int chse = sc.nextInt();
if (chse == 1) {
break;
} else if (chse == 2) {
return;
} else {
System.out.println("输入错误!");
}
}
}
}//道具获取途径
private static void examine_(User master) {
System.out.println("宝可梦名称" + "\t" + "宝可梦属性" + "\t" + "宝可梦稀有度");
for (int i = 0; i < master.getItem().size(); i++) {
Item item = master.getItem().get(i);
System.out.println(item.getName() + "\t" + item.getType() + "\t" + item.getQuality());
}
}//查看宝可梦信息
private static int Panduan1(ArrayList<User> list, String userPhone) {
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
String userPhone1 = user.getPhonenumber();
if (userPhone1.equals(userPhone)) {
return i;
}
}
return -1;
}//判断1
private static int Panduan(ArrayList<User> list, String userName) {
for (int i = 0; i < list.size(); i++) {
User user = list.get(i);
String userName1 = user.getUserName();
if (userName1.equals(userName)) {
return i;
}
}
return -1;
}//判断2
private static void register(ArrayList<User> list,Connection conn) throws IOException, SQLException {
Scanner sc = new Scanner(System.in);
String userName;
while (true) {
while (true) {
System.out.println("请输入用户名(用户名只能包含字母、数字和下划线,且长度在6到20之间)");
userName = sc.next();
boolean indx = isValidUsername(userName);
if (!indx) {
System.out.println("用户名格式错误,请重新输入");
} else {
break;
}
}
int index = Panduan(list, userName);
if (index >= 0) {
System.out.println("用户名已经重复,请重新输入");
} else {
break;
}
}
while (true) {
int passWord = 0;
while (true) {
System.out.println("请输入密码(密码只能是6位数字)");
passWord = sc.nextInt();
boolean inde = isValidPassword(passWord);
if (!inde) {
System.out.println("密码格式错误,请重新输入。");
} else {
break;
}
}
System.out.println("请再次输入密码");
int passWord1 = sc.nextInt();
if (passWord == passWord1) {
User user = new User();
user.setUserName(userName);
user.setPassword(passWord);
String PhoneNumber = null;
while (true) {
System.out.println("请输入手机号码,将用于找回密码以及查询信息");
PhoneNumber = sc.next();
boolean indx = isValidPhoneNumber(PhoneNumber);
if (!indx) {
System.out.println("请输入正确的手机号码");
} else {
break;
}
}
user.setPhonenumber(PhoneNumber);
list.add(user);
System.out.println("注册成功");
String query = "insert into users(userName,password,phone)value(?,?,?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1,userName );
ps.setInt(2, passWord);
ps.setString(3, PhoneNumber);
ps.executeUpdate();
return;
} else {
System.out.println("两次输入不一致,请重新输入。");
}
}
}//注册
private static boolean isValidUsername(String username) {
// 用户名只能包含字母、数字和下划线,且长度在6到20之间
String regex = "^[a-zA-Z0-9_]{6,20}$";
return username.matches(regex);
}//校验用户名
private static boolean isValidPassword(int password) {
// 密码只能是6位数字
String regex = "^[0-9]{6}$";
return String.valueOf(password).matches(regex);
}//密码
private static boolean isValidPhoneNumber(String phoneNumber) {
String regex = "^(?:(?:\\+|00)86)?1[3-9]\\d{9}$";
return phoneNumber.matches(regex);
}//手机号
}
这个系统中关于抽奖道具的获得我就把当时练手的猜数字游戏写了上去,对于数据的校验现在来看也写的比较随便,不过通过当时对于这个项目的练习,我学习到了非常多的宝贵的经验,加深了对Java基本概念的理解,对jdbc的使用(虽然之后就没怎么用过了),并且学会了抽奖系统的书写方法。
结语
希望我的分享能对大家有所帮助,也欢迎大家提出宝贵的意见和建议。