简单的控制台练手代码,欢迎您提出宝贵的意见
简单的用了类和对象的里面的一些属性和方法,希望能对您的学习提供微不足道的帮助
1.是会员类
package cn.kgc.entity;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 会员类
*
* @author xiaobubu 2019年2月23日下午4:59:53
*/
public class VIP implements Serializable {
private long vipCard;// 会员卡号
private String name;// 会员的名字
private String password;// 会员的密码
private int integral;// 会员积分
private Date registerDate;// 开卡日期
public long getVipCard() {
return vipCard;
}
public void setVipCard(long vipCard) {
this.vipCard = vipCard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getIntegral() {
return integral;
}
public void setIntegral(int integral) {
this.integral = integral;
}
public Date getRegisterDate() {
return registerDate;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public VIP() {
super();
}
public VIP(long vipCard, String name, String password, int integral, Date registerDate) {
super();
this.vipCard = vipCard;
this.name = name;
this.password = password;
this.integral = integral;
this.registerDate = registerDate;
}
// 重写toString
@Override
public String toString() {
return "您的会员卡号为:" + vipCard + ", 您的会员名是:" + name + ", 会员密码:" + password + ", 会员积分:" + integral + "开卡日期"
+ new SimpleDateFormat("yyyy-MM-dd").format(registerDate);
}
}
2.是程序入口测试类
package cn.kgc.main;
import cn.kgc.mgr.VIPmgr;
/**
*
* @author xiaobubu
*2019年2月23日下午4:54:03
*/
public class ClientMain {
public static void main(String[] args) {
VIPmgr vip = new VIPmgr();
vip.start();
}
}
3.是会员操作类
package cn.kgc.mgr;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
import cn.kgc.entity.VIP;
/**
* 会员操作类
*
* @author xiaobubu 2019年2月23日下午4:55:34
*/
public class VIPmgr {
// 导入Scanner类
private Scanner input = new Scanner(System.in);
// 创建集合
private static ArrayList vipList = new ArrayList();
// 初始化会员信息
static {
vipList.add(new VIP(11111111, "马云", "11111111", 11111111, new Date()));
vipList.add(new VIP(22222222, "小韩", "22222222", 22222222, new Date()));
vipList.add(new VIP(33333333, "小杜", "33333333", 33333333, new Date()));
}
/**
* 系统启动
*/
public void start() {
// 先用到循环不确定次数的用while或者是do{}while()
do {
// 展示菜单
showMenu();
// 提示输入
System.out.println("请输入你要选择的服务:");
// 用string类型可以防止用户输入字符的错误
String choose = input.next();
switch (choose) {
case "1":// 积分累计
if (addIntegral()) {
System.out.println("积分添加成功!");
} else {
System.out.println("积分添加失败");
}
break;
case "2":// 积分兑换
if (exchangeIntegral()) {
System.out.println("积分兑换成功!");
} else {
System.out.println("积分兑换失败!");
}
break;
case "3":// 查剩余积分
findVIPInfo();
break;
case "4":// 修改密码
VIP vip = vipLogin();// 登录
if (vip != null) {// 登录成功
System.out.println("请输入您的新密:");
String password = "";
do {
password = input.next();
if (password.length() < 6) {// 判断新的密码是否合格
System.out.println("密码长度不能小于6");
continue;
}
break;
} while (true);
// 设置新的密码
vip.setPassword(password);
System.out.println("密码修改成功,请您牢记密码!");
} else {
System.out.println("登录失败!");
}
break;
case "5":// 开卡
addVIP();
break;
case "6":// 退出
System.out.println("欢迎下次光临!");
break;
default:
System.out.println("输入有误,程序员小韩正在解决!");
break;
}
} while (true);
}
/**
* 展示菜单
*/
public void showMenu() {
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("6.退出");
}
/**
****** 登录功能
*
* @return
*/
private VIP vipLogin() {
VIP result = null;
System.out.println("请输入会员卡号:");
int vipCard = input.nextInt();
System.out.println("请输入会员密码:");
String password = input.next();
// 查询会员所有的信息
for (VIP vip : vipList) {
if (vip.getVipCard() == vipCard && vip.getPassword().equals(password)) {
result = vip;
}
}
return result;
}
/**
****** 1.积分累加
*
* @return
*/
public boolean addIntegral() {
boolean fag = false;
VIP vip = vipLogin();// 登录
if (vip != null) {
System.out.println("请输入您的消费金额(消费一元等于一级分)");
int integral = input.nextInt();
// 积分进行累加
vip.setIntegral(vip.getIntegral() + integral);
fag = true;
} else {
System.out.println("登录失败!");
fag = false;
}
return fag;
}
/**
* *****2.积分兑换
*
* @return
*/
private boolean exchangeIntegral() {
boolean fag = false;
VIP vip = vipLogin();// 登录
if (vip != null) {
System.out.println("请输入要兑换的积分数量:");
int integral = input.nextInt();
if (integral <= vip.getIntegral()) {// 判断兑换的积分数量是否满足兑换的要求
vip.setIntegral(vip.getIntegral() - integral);
System.out.println("您兑换了:" + (integral / 100 * 0.1) + "元");
fag = true;
} else {
System.out.println("积分不足!");
}
}
return fag;
}
/**
* ******4.查询剩余积分
*/
private void findVIPInfo() {
VIP vip = vipLogin();// 登录
if (vip != null) {
System.out.println(vip);
} else {
System.out.println("查询失败!");
}
}
/**
* *****5开卡
*/
private void addVIP() {
// 创建对象vip
VIP vip = new VIP();
System.out.println("请输入注册的姓名:");
vip.setName(input.next());
System.out.println("请输入注册的密码:");
String password = "";
do {
password = input.next();
if (password.length() < 6) {
System.out.println("密码长度不能小于6,请重新输入!");
continue;
}
break;
} while (true);
vip.setPassword(password);
vip.setVipCard(creatCard());
vip.setIntegral(100);
vip.setRegisterDate(new Date());
vipList.add(vip);
System.out.println(vip);
}
/**
* 生成随机卡号8位
*
* @return
*/
private long creatCard() {
long id = 0;
boolean fag;
do {
fag = true;
id = (int) (Math.random() * (100000000 - 10000000)) + 10000000;
// 遍历所有的会员列表,判断是否有重复
for (VIP vip : vipList) {
if (vip.getVipCard() == id) {
fag = false;
}
}
} while (!fag);
return id;
}
}