ATM 机系统
import java.io.Serializable;
public class User implements Serializable {
private String id;
private String password;
private double balance;
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@Override
public String toString() {
return "User [id=" + id + ", password=" + password + ", balance=" + balance + "]";
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.regex.Pattern;
public class ATMSystem {
private ArrayList<User> users;
private Scanner scanner;
private int userindex;
public ATMSystem() {
// 给保存用户的动态数组分配空间
this.users = new ArrayList<User>();
this.scanner = new Scanner(System.in);
//读取本地缓存文件
reUsers();
// 显示初始菜单
startMenu();
}
public void startMenu() {
System.out.println("*****菜单******");
System.out.println("【1】注册");
System.out.println("【2】登入");
System.out.println("【3】退出");
System.out.print(">");
String sum = scanner.nextLine();
switch (sum) {
case "1":
// 注册
register();
startMenu();
break;
case "2":
// 登入
enter();
startMenu();
break;
}
}
public void bankMenu() {
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】注销银行卡");
System.out.println("【7】退出");
System.out.print(">");
String sum = scanner.nextLine();
switch (sum) {
case "1":
// 修改密码
amendPassword();
bankMenu();
break;
case "2":
// 查询余额
inquireBalance();
bankMenu();
break;
case "3":
// 存钱
drawMoney();
bankMenu();
break;
case "4":
fetchMoney();
bankMenu();
// 取钱
break;
case "5":
// 转账
transferAccounts();
bankMenu();
break;
case "6":
// 注销银行卡
writeOff();
bankMenu();
break;
case "7":
// 退出
System.exit(0);
break;
}
}
// 修改密码
private void amendPassword() {
while (true) {
System.out.println("请输入6位数的密码");
String pas = scanner.nextLine();
if (!Pattern.matches("[0-9]{6}", pas)) {
System.out.println("密码输入有误,请重新输入!");
continue;
}
System.err.println("请确定你的密码");
String password = scanner.nextLine();
// 判断两次密码是否相同
if (!pas.equals(password)) {
System.out.println("两次输入的密码不一样,请想好了再输!");
continue;
} else {
users.get(userindex).setPassword(password);
writeUsers();
System.out.println("修改成功");
startMenu();
break;
}
}
}
// 查询余额
private void inquireBalance() {
System.out.println("余额:" + users.get(userindex).getBalance());
}
// 存钱
private void drawMoney() {
System.out.println("输入要存的钱");
String money = scanner.nextLine();
users.get(userindex).setBalance(users.get(userindex).getBalance() + Double.parseDouble(money));
writeUsers();
inquireBalance();
}
// 取钱
private void fetchMoney() {
System.out.println("输入要取的钱");
String money = scanner.nextLine();
if (users.get(userindex).getBalance() - Double.parseDouble(money) >= 0) {
users.get(userindex).setBalance(users.get(userindex).getBalance() - Double.parseDouble(money));
writeUsers();
inquireBalance();
} else {
System.out.println("小伙想太多,你那有那么多钱");
}
}
// 转账
private void transferAccounts() {
System.out.println("输入您要转账人的卡号");
String id = scanner.nextLine();
int index = gerUserIndrex(id);
if (index == -1) {
System.out.println("用户不存在,请核实后再试!");
return;
}
System.out.println("请输入余额");
String money = scanner.nextLine();
if (users.get(userindex).getBalance() - Double.parseDouble(money) >= 0) {
System.out.println("将给用户" + users.get(index) + "转账,是否确定Y/N");
String r = scanner.nextLine();
if ("y".equals(r.toLowerCase())) {
users.get(userindex).setBalance(users.get(userindex).getBalance() - Double.parseDouble(money));
users.get(index).setBalance(users.get(index).getBalance() + Double.parseDouble(money));
writeUsers();
System.out.println("转账成功");
inquireBalance();
} else {
System.out.println("转账取消");
}
} else {
System.out.println("小伙想太多了,你哪有这么多钱啊!");
}
}
// 注册
private void register() {
// 定义User对象保存用户信息
User user = new User();
// 创建随机数工具类
Random ran = new Random();
// 生成银行卡的账号,生成15位随机账户
for (int i = 0; i < 15; i++) {
if (i == 0) {
user.setId(ran.nextInt(9) + 1 + "");
}else{
user.setId(user.getId() + ran.nextInt(10));
}
}
System.out.println("您生成的卡号为:" + user.getId() + " " + "请记好你的卡号!");
while (true) {
// 用户设置密码
System.out.println("请输入6位数的密码");
String pas = scanner.nextLine();
if (!Pattern.matches("[0-9]{6}", pas)) {
System.out.println("密码输入有误,请重新输入!");
continue;
}
System.err.println("请确定你的密码");
String password = scanner.nextLine();
// 判断两次密码是否相同
if (!pas.equals(password)) {
System.out.println("两次输入的密码不一样,请想好了再输!");
continue;
}
user.setPassword(password);
// 把用户保存到全局变量中
this.users.add(user);
writeUsers();
System.out.println("注册成功,您的卡号为:" + user.getId());
break;
}
}
//注销银行卡
private void writeOff(){
System.out.println("你确定注销卡号为:"+users.get(userindex).getId()+"的账户|Y/N"
+"您的卡号还有余额为"+users.get(userindex).getBalance());
String str = scanner.nextLine();
if("y".equals(str.toLowerCase())){
users.remove(userindex);
writeUsers();
System.out.println("注销成功");
System.exit(0);
}else{
System.out.println("注销已取消");
}
}
// 登录
private void enter() {
for(User f:users){
System.out.println(f);
}
System.out.println("输入卡号");
String id = scanner.nextLine();
int index = gerUserIndrex(id);
if (index == -1) {
System.out.println("没有该用户");
return;
}
System.out.println("请输入密码");
String password = scanner.nextLine();
if (users.get(index).getPassword().equals(password)) {
this.userindex = index;
bankMenu();
System.out.println("登入成功");
} else {
System.out.println("密码错误,请仔细检查后再登!");
}
}
// 根据卡号查询当前用户的下标
public int gerUserIndrex(String id) {
for (int i = 0; i < users.size(); i++) {
if (users.get(i).getId().equals(id)) {
return i;
}
}
return -1;
}
//读出本地数据
private void reUsers() {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("ATM");
try {
ois = new ObjectInputStream(fis);
Object obj = ois.readObject();
if (obj instanceof ArrayList) {
this.users = (ArrayList<User>) obj;
}
} catch (Exception e) {
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (ois != null) {
ois.close();
}
if (fis != null) {
fis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//将数据写入本地
private void writeUsers() {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("ATM");
oos = new ObjectOutputStream(fos);
//写入本地文件
oos.writeObject(this.users);
oos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if(oos != null) {
oos.close();
}
if(fos != null) {
fos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
new ATMSystem();
}
}