系统类
import java.util.Scanner;
/**
* 需求分析: 1、注册IC 2、查询IC 3、注销IC 4、充值 5、登录
*
* 概要设计: 1、用户属性:姓名、学号、联系方式、金额、卡号、密码 2、存储方式:数组、
*
* @author FuJianshi
*
*/
public class ICManager {
private static User[] users = new User[1000];
private static Scanner scanner = new Scanner(System.in);
static {
users[0] = new User("fwe", "16020340116", "18570466893", 200, "10001");
users[1] = new User("dds", "16020340113", "18552841452", 300, "10002");
users[2] = new User("ead", "16020340114", "18512548762", 203, "10003");
users[3] = new User("zdw", "16020340118", "18504681245", 120, "10004");
users[4] = new User("dse", "16020340109", "18525896413", 220, "10005");
}
/**
* 主函数
*
* @param args
*/
public static void main(String[] args) {
// 输出登录界面
showLoginMenu();
// 输出主菜单
showMainMenu();
}
/**
* 登录菜单
*/
private static void showMainMenu() {
while (true) {
System.out.println("********************************");
System.out.println("* 1、注册 *");
System.out.println("* 2、查询 *");
System.out.println("* 3、充值 *");
System.out.println("* 4、注销 *");
System.out.println("* 0、退出 *");
System.out.println("********************************");
System.out.print("*请选择操作:");
int i = scanner.nextInt();
switch (i) {
case 1:
showRegistra();
break;
case 2:
showAllUsers();
break;
case 3:
addMoney();
break;
case 4:
remove();
break;
case 0:
System.out.println("感谢您的使用!");
System.exit(-1);
break;
default:
System.out.println("输入不合法,请重新输入!");
scanner.nextLine();
scanner.nextLine();
showMainMenu();
}
}
}
/**
* 注销
*/
private static void remove() {
while (true) {
System.out.println("********************************");
System.out.println("* 注销界面 *");
System.out.println("********************************");
// 输入要注销的卡号
System.out.print("请输入卡号:(输入数字0返回......)");
scanner.nextLine();
String card = scanner.next();
// 遍历用户数组
String fan = "0";
if (card.equals(fan)) {
return;
} else {
for (int i = 0; i < users.length; i++) {
// 查找用户
if (users[i] != null && card.equals(users[i].getCard())) {
// 将该用户设置为null
users[i] = null;
// 将后面的用户向前推进 1 2 3 nuill 2 4 3
for (int j = i + 1; j < users.length; j++) {
if (users[j] != null) {
users[j - 1] = users[j];
users[j] = null;
} else {
break;
}
}
System.out.println("注销成功!按任意键继续......");
scanner.nextLine();
scanner.nextLine();
return;
}
}
System.out.println("没有找到用户,请重新输入......");
}
}
}
/**
* 充值
*/
private static void addMoney() {
// 读入卡号,如果没找到啧重读卡号
// 查询用户
// 读入金额
// 将金额加入到用户余额中
while (true) {
System.out.println("********************************");
System.out.println("* 充值界面 *");
System.out.println("********************************");
System.out.print("请输入卡号:(输入数字0返回......)");
scanner.nextLine();
String card = scanner.next();
String fan = "0";
if (card.equals(fan)) {
return;
} else {
User user = null;
for (int i = 0; i < users.length; i++) {
if (users[i] != null && card.equals(users[i].getCard())) {
user = users[i];
break;
}
}
System.out.println("卡号\t姓名\t学号\t\t手机号\t\t余额\t");
System.out.println(user);
if (user != null) {
System.out.print("请输入充值金额:");
int money = scanner.nextInt();
user.setAmount(money + user.getAmount());
System.out.println("充值金额成功,按任意键继续......");
scanner.nextLine();
System.out.println("卡号\t姓名\t学号\t\t手机号\t\t余额\t");
System.out.println(user);
scanner.nextLine();
break;
} else {
System.out.println("没有找到用户,请重新输入......");
}
}
}
}
/**
* 查询所有用户
*/
private static void showAllUsers() {
System.out.println("********************************");
System.out.println("* 查询界面 *");
System.out.println("********************************");
System.out.println("卡号\t姓名\t学号\t\t手机号\t\t余额\t");
if (users[0] == null) {
System.out.println("无用户!");
} else {
for (User user : users) {
if (user != null) {
System.out.println(user);
} else {
break;
}
}
}
System.out.println("按任意键继续......");
scanner.nextLine();
scanner.nextLine();
}
/**
* 注册
*/
private static void showRegistra() {
User user = new User();
System.out.println("********************************");
System.out.println("* 注册界面 *");
System.out.println("********************************");
System.out.print("请输入姓名:(输入数字0返回......)");
scanner.nextLine();
String name = scanner.next();
String fan = "0";
if(name.equals(fan)){
return;
}else{
user.setName(name);
System.out.print("请输入学号:");
String number = scanner.next();
user.setNumber(number);
System.out.print("请输入密码:");
String password = scanner.next();
user.setPassword(password);
System.out.print("请输入联系方式:");
String call = scanner.next();
user.setCall(call);
System.out.print("请输入金额:");
int amount = scanner.nextInt();
user.setAmount(amount);
scanner.nextLine();
System.out.println("信息输入完毕......");
addUser(user);
System.out.println();
System.out.println("请记住您的卡号:" + user.getCard());
System.out.println("按任意键继续......");
scanner.nextLine();
}
}
/**
* 将用户注册到系统
*
* @param user
*/
private static void addUser(User user) {
for (int i = 0; i < users.length; i++) {
if (users[i] == null) {
users[i] = user;
break;
}
}
}
/**
* 登录界面
*
*/
private static void showLoginMenu() {
int count = 3;
while (true) {
System.out.println("********************************");
System.out.println("* 欢迎使用本IC系统 *");
System.out.println("********************************");
System.out.println("********************************");
System.out.print("*请输入账号:");
String name = scanner.next();
System.out.print("*请输入密码:");
String password = scanner.next();
if ("fjs".equals(name) && "123".equals(password)) {
System.out.println("登录成功>>>>");
return;
} else {
if (count == 1) {
System.out.println("登录失败,系统即将自动退出......");
} else {
count--;
}
System.out.println("输入错误,请重新输入,您还有:" + count + "次机会!!!");
System.out.println("按任意键继续......");
scanner.nextLine();
scanner.nextLine();
}
}
}
}
用户类
package ICManager;
/**
* 用户类:
* 姓名:name、学号:number、手机号:cell、金额:amount、卡号:card、密码:password
* @author FuJianshi
*
*/
public class User {
private String name;
private String number;
private String call;
private double amount;
private String card;
private String password;
/**
* 自动生成卡号
*/
public String card(){
long time = System.currentTimeMillis(); //获取系统时间戳
String sn = "" + time;//将数字转成字符串
//截取后六位,返回字符串长度
sn = sn.substring(sn.length() - 6 , sn.length());
return sn;
}
public User() {
super();
this.card = this.card();
}
public User(String name, String number, String call, double amount, String card) {
super();
this.name = name;
this.number = number;
this.call = call;
this.amount = amount;
this.card = card;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getCall() {
return call;
}
public void setCall(String call) {
this.call = call;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getCard() {
return card;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return this.card + "\t" +this.name + "\t" + this.number + "\t"
+ this.call + "\t" + this.amount;
}
}