package ATM;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATMsystem {
public static void main(String[] args) throws IOException {
//1,定义账户类
//2。定义一个集合容器,负责以后存储账户对象,进行相关业务操作
ArrayList<Account> accounts = new ArrayList<>();
Scanner sc = new Scanner(System.in);
//展示系统首页
while (true) {
System.out.println("=========ATM系统=========");
System.out.println("1,账户登陆");
System.out.println("2,账户开户");
System.out.println("请输入你的操作:");
int command = sc.nextInt();
switch (command) {
case 1:
//用户登录操作
login(accounts, sc);
break;
case 2:
//用户开户操作
//alt+回车键自动创建方法
register(accounts, sc);
break;
default:
System.out.println("你输入的操作有误!");
}
}
}
/**
* 登陆功能
*
* @param accounts 全部账户对象的集合
* @param sc 扫描器
*/
private static void login(ArrayList<Account> accounts, Scanner sc) {
System.out.println("=============系统登陆操作===============");
//1、判断账户集合中是否存在账户,如果不存在账户,登录功能不能进行。
if (accounts.size() == 0) {
System.out.println("系统中没有账户,请先注册");
return;//卫语言风格,解决方法的执行
}
//2.正式进入登陆操作
while (true) {
System.out.println("请输入卡号");
String cardId=sc.next();
//3.判断卡号是否存在,根据卡号去账户集合中查询账户对象
Account acc=getAccountByCardId(cardId,accounts);
if (acc!=null){
while (true) {
//卡号存在
//4,让用户输入密码,认证密码
System.out.println("请输入密码:");
String passWord = sc.next();
//判断当前账户对象的密码是否与用户输入密码一致
if (acc.getPassWord().equals(passWord)){
//登陆成功
System.out.println("登陆成功"+acc.getUserName()+"先生/女士,你的卡号是"+acc.getCardId());
//....查询 转账 取款...
//展示登陆后的操作页
showUserCommand(sc,acc,accounts);
return;//停止登陆界面
}
else {
System.out.println("输入密码有误");
}
}
}
else {
System.out.println("对不起,系统中不存在此卡号");
}
}
}
/**
* 展示登陆后的操作页
*/
private static void showUserCommand(Scanner sc,Account acc,ArrayList<Account> accounts) {
while (true) {
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.println("请选择:");
int command=sc.nextInt();
switch (command){
case 1:
//查询账户(展示当前登陆的账户信息)
showAccount(acc);
break;
case 2:
//存款
depositMoney(acc,sc);
break;
case 3:
//取款
drawMoney(acc,sc);
break;
case 4:
//转账
transferMoney(acc,sc,accounts);
break;
case 5:
//修改密码
updatePassWord(sc,acc);
return;
case 6:
//退出
System.out.println("退出成功");
return;//当前方法停止执行
case 7:
//注销账户
if (deleteAccount(acc,sc,accounts)){
//销户成功,回到首页
return;//当方法停止执行跳出去
}else {
break;
}
default:
System.out.println("输入操作命令不正确");
}
}
}
/**
* 删除账户
* @param acc 当前账户
* @param sc 扫描器
* @param accounts 全部账户集合
*/
private static boolean deleteAccount(Account acc, Scanner sc, ArrayList<Account> accounts) {
//从当前账户集合中删除当前账户对象
System.out.println("=============删除账户操作===================");
System.out.println("确认销户?Y/N");
String rs=sc.next();
switch (rs){
case "Y":
//真正销户
if (acc.getMoney()>0){
System.out.println("账户还有余额不能销户");
}else {
accounts.remove(acc);
System.out.println("销户完成");
return true; //销户成功
}
break;
default:
System.out.println("确认退出");
}
return false;
}
/**
* 修改密码
* @param sc 扫描器
* @param acc 登陆成功的账户对象
*/
private static void updatePassWord(Scanner sc, Account acc) {
System.out.println("=============用户密码修改====================");
while (true) {
System.out.println("请输入当前密码:");
String passWord= sc.next();
//1,判断这个密码是否正确
if (acc.getPassWord().equals(passWord)){
while (true) {
//密码正确
//2,输入新密码
System.out.println("输入新密码:");
String newPassWord=sc.next();
System.out.println("确认新密码");
String okPassWord=sc.next();
if (newPassWord.equals(okPassWord)){
//两次密码不一致
acc.setPassWord(okPassWord);
System.out.println("密码修改成功");
return;
}else {
System.out.println("两次密码不一致");
}
}
}else {
System.out.println("密码不正确");
}
}
}
/**
*转账功能
* @param acc 自己的账户对象
* @param sc 扫描器
* @param accounts 全部账户的集合
*/
private static void transferMoney(Account acc, Scanner sc, ArrayList<Account> accounts) {
System.out.println("=============用户转账操作============");
//判断是否足够两个账户
if (accounts.size()<2){
System.out.println("没有转账对象");
return;//结束当前的方法
}
//2,判断自己的账户是否有钱
if (acc.getMoney()==0){
System.out.println("对不起账户余额不足");
return;//结束当前方法
}
while (true) {
//开始转账
System.out.println("请输入对方的账户:");
String cardId=sc.next();
//这个卡号不能是自己的卡号
if (cardId.equals(acc.getCardId())){
System.out.println("不能对自己转账");
continue;//结束当次循环,进入下次循环
}
//判断这个卡号是否存在
Account account=getAccountByCardId(cardId,accounts);
if (account==null){
System.out.println("卡号不存在");
}else {
//这个账户存在,认证对方的姓名
String userName = account.getUserName();
String tip="*"+userName.substring(1);
System.out.println("请输入["+tip+"]姓名");
String prNmae=sc.next();
//认证姓氏是否输入正确
if (userName.startsWith(prNmae)){
while (true) {
//认证通过,真正开始转账
System.out.println("输入转账金额:");
double money=sc.nextDouble();
//判断金额是否足够
if (money>acc.getMoney()){
System.out.println("对不起,余额不足,你最多转账:"+acc.getMoney());
}else {
//余额足够可以转账
acc.setMoney(acc.getMoney()-money);
account.setMoney(account.getMoney()+money);
System.out.println("转账成功!账户余额剩余:"+acc.getMoney());
return;//直接退出
}
}
}else {
System.out.println("对不起,你输入信息有误");
}
}
}
}
/**
* 取钱
* @param acc 当前账户对象
* @param sc 扫描器
*/
private static void drawMoney(Account acc, Scanner sc) {
System.out.println("=============用户取钱操作============");
//1,判断是否足够100元
if (acc.getMoney()<100){
System.out.println("对不起当前账户不足100元,不能取款");
return;
}
while (true) {
//2,提示用户输入取钱金额
System.out.println("请你输入取款金额:");
double money=sc.nextDouble();
//3.判断这个金额是否满足要求
if (money>acc.getQuotaMoney()){
System.out.println("对不起,你当前取款金额超过每次限额,每次最多可取"+acc.getQuotaMoney());
}else {
//没有超过当次限额
//4,判断是否超过账户总额
if (money>acc.getMoney()){
System.out.println("余额不足,你账户余额为:"+acc.getMoney());
}else {
//可以取钱
System.out.println("取钱"+money+"元,成功!");
//更新余额
acc.setMoney(acc.getMoney()-money);
//取钱结束
showAccount(acc);
return;
}
}
}
}
/**
* 存钱
* @param acc 当前账户对象
* @param sc 扫描器
*/
private static void depositMoney(Account acc, Scanner sc) {
System.out.println("==============用户存钱操作=================");
System.out.println("请输入存款金额:");
double money = sc.nextInt();
//更新账户余额,原来的钱+新存入的钱
acc.setMoney(acc.getMoney()+money);
System.out.println("存钱成功,当前账户信息如下:");
showAccount(acc);
}
/**
*展示账户信息
* @param acc
*/
private static void showAccount(Account acc) {
System.out.println("==============当前账户信息==============");
System.out.println("卡号:"+acc.getCardId());
System.out.println("户主:"+acc.getUserName());
System.out.println("密码:"+acc.getPassWord());
System.out.println("余额:"+acc.getMoney());
System.out.println("限额:"+acc.getQuotaMoney());
}
/*
* 用户开户功能的实现
* @param accounts 接受的账户集合
* */
private static void register(ArrayList<Account> accounts, Scanner sc) throws IOException {
System.out.println("=============系统开户操作===============");
//1,创建一个账户对象,用于后期封装信息
Account account = new Account();
System.out.println("请输入账户用户名:");
String userName = sc.next();
account.setUserName(userName);
while (true) {
System.out.println("请输入账户密码:");
String passWord = sc.next();
System.out.println("请确认密码");
String okPassWord = sc.next();
if (okPassWord.equals(passWord)) {
//密码认证通过,可以注入给账户对象
account.setPassWord(okPassWord);
break;//密码录入成功,死循环没有必要继续
} else {
System.out.println("两次密码不一致");
}
}
System.out.println("输入账户当次限额");
double quotaMoney = sc.nextDouble();
account.setQuotaMoney(quotaMoney);
//为账户随机一个8位数且与其他账户不重复的号码(独立功能,独立成方法)
String cardId = getRandomCardId(accounts);
account.setCardId(cardId);
//3.把账户对象添加到账户集合中去
accounts.add(account);
System.out.println("恭喜你" + userName + "先生/女士,开户成功,卡号是" + cardId);
}
/**
* 为账户生成8位与其他卡号不同的号码
*/
private static String getRandomCardId(ArrayList<Account> accounts) {
//1,生成8位数字
Random r = new Random();//Random随机输出数字
while (true) {
String cardId = "";
for (int i = 0; i < 8; i++) {
cardId+=r.nextInt(10);
}
//2,判断这8位卡号是否与其他账户重复
Account acc = getAccountByCardId(cardId, accounts);
if (acc == null) {
//说明cardId此时没有重复这个卡号是一个新卡号可以使用这个卡号为注册卡号
return cardId;
}
}
}
/**
* 根据卡号查询一个账户的对象出来
* cardId 卡号
* accounts 全部账户的集合
* return 账户对象 | null
*/
private static Account getAccountByCardId(String cardId, ArrayList<Account> accounts) {
for (int i = 0; i < accounts.size(); i++) {
Account acc = accounts.get(i);
if (acc.getCardId().equals(cardId)) {
return acc;
}
}
return null;//查无此账号
}
}
请问我想把这个账户集合用序列化添加txt文件中不要覆盖,登陆的时候用到反序列化怎么添代码
正常文件读写就可以了,你可以自定义存到文件里的格式,不同值之间用逗号分割之类的,取的时候自己split一下,文件追加的话可以用
FileOutputStream fos = new FileOutputStream(file, true),这里的true表示追加。
另外有的时候有修改,所以不能用追加,需要全部取出来进行修改后覆盖,具体还需要你自己判断。