package practice;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class ATM_project {
public static void main(String[] args) {
//1.创建Account类来存储用户信息
//3.建立集合,一个账户就是一个元素
ArrayList<Account> accounts=new ArrayList<>();
//4.制作页面首页
while (true) {
System.out.println("===========ATM系统==========");
System.out.println("1.账户登入");
System.out.println("2.账户开户");
//5.选择登入or注册
Scanner sc=new Scanner(System.in);
int command=sc.nextInt();
switch (command){
case 1:
//6.选了登入,引用登入方法
System.out.println("========进入登陆状态========");
login(accounts,sc);
break;
case 2:
//7.选了开户,引用注册方法
System.out.println("========进入开户状态=======");
register(accounts,sc);
break;
default:
System.out.println("输入操作错误,请重新输入");
}
}
}
//===============================后面都是定义的方法,在main里,按ctrl+enter就能进入对应代码============================
/**
* 定义一个登入方法
* @param accounts
* @param sc
*/
private static void login(ArrayList<Account> accounts, Scanner sc) {
//情况一:系统中无账户
if(accounts.size()==0){
System.out.println("对不起,目前系统中无任何账户,请先开户");
return;
}
//情况二:系统中有账户,分卡号是否正确,密码是否正确讨论
System.out.println("please enter your cardId");
while (true) {
String cardnumber=sc.next();
for (int i = 0; i < accounts.size(); i++) {
Account m=accounts.get(i);
if(m.getCardId().equals(cardnumber)){
//系统里有该卡号,后面判断密码正确与否
System.out.println("please enter your account passsword");
while (true) {
String password=sc.next();
if(m.getPassword().equals(password)){
System.out.println("the password is correct,successful login");
// ======开始设计操作页面=========
// 1.引用设计操作页 方法
showuserCommand(sc,m,accounts);
return;
}else{
System.out.println("the password is wrong,please enter again");
}
}
// break; //这个用的很妙
}else{
System.out.println("the cardId you enter was wrong,please enter again");
}
}
}
}
/**
* 定义 设计操作页 方法
* @param sc
* @param m 当前用户在集合里面的地址
* @param accounts
*/
private static void showuserCommand(Scanner sc, Account m, ArrayList<Account> accounts) {
while (true) {
System.out.println("========进入操作页面========");
System.out.println("1.Inquiry");
System.out.println("2.Deposit");
System.out.println("3.Withdraw");
System.out.println("4.Transfer");
System.out.println("5.Change Passwprd");
System.out.println("6.Exit");
System.out.println("7.Close Account");
System.out.println("Please Enter The Number");
int command = sc.nextInt();
switch (command) {
case 1:
// 进入查询操作
// 1. 引用方法
showAccount(m); // m是当前用户的所在集合元素的地址
break;
case 2:
// 进入存款操作
// 2.引用方法
deposit(m, sc);
//判断是否继续存款
jixu1(sc,m);
break;
case 3:
// 进入取款操作
// 3.引用方法
withdraw(sc,m);
jixu2(sc,m);
break;
case 4:
// 进入转账操作
transformoney(sc,m,accounts);
jixu3(sc,m,accounts);
break;
case 5:
// 进入修改密码操作
updatepassword(sc,m);
return;
case 6:
// 进入退出操作
return;
case 7:
// 进入注销账户操作
deleteAccount(sc,m,accounts);
return;
}
}
}
/**
* 判断是否继续转账
* @param sc
* @param m
* @param accounts
*/
private static void jixu3(Scanner sc, Account m, ArrayList<Account> accounts) {
while (true) {
System.out.println(" to contiune press 1,exit press 2");
int n1 = sc.nextInt();
if (n1 == 1) {
transformoney(sc,m,accounts);
} else {
return;
}
}
}
/**
* 判断是否继续取款
* @param sc
* @param m
*/
private static void jixu2(Scanner sc, Account m) {
while (true) {
System.out.println(" to contiune press 1,exit press 2");
int n1 = sc.nextInt();
if (n1 == 1) {
withdraw(sc,m);
} else {
return;
}
}
}
/**
* 判断是否继续存款
* @param sc
* @param m
*/
private static void jixu1(Scanner sc, Account m) {
while (true) {
System.out.println(" to contiune press 1,exit press 2");
int n1 = sc.nextInt();
if (n1 == 1) {
deposit(m, sc);
} else {
return;
}
}
}
/**
* 销户操作
* @param sc
* @param m
* @param accounts
*/
private static void deleteAccount(Scanner sc, Account m, ArrayList<Account> accounts) {
System.out.println("=============进入销户操作================");
System.out.println("请问真的要销户吗?y/n");
String ans=sc.next();
switch(ans){
case "y": //注意:String型一定要加""
if(m.getBlance()>0){
System.out.println("账户中还有"+m.getBlance()+"元,无法销户");
}else{
accounts.remove(m);
System.out.println("销户完成");
return;
}
break;
default:
System.out.println("好的,当前账户继续保留");
break;
}
}
/**
* 修改密码操作
* @param sc
* @param m 当前用户在集合里的地址
*/
private static void updatepassword(Scanner sc, Account m) {
System.out.println("===========进入修改密码页面=============");
System.out.println("请输入旧密码");
while (true) {
String oldpassword=sc.next();
if(m.getPassword().equals(oldpassword)){
//输入旧密码正确,可开始验证新密码
while (true) {
System.out.println("请您输入新密码");
String newpassword=sc.next();
System.out.println("请您输入确认密码");
String okpassword=sc.next();
if(newpassword.equals(okpassword)){
//两次密码一致,可更正密码
m.setPassword(okpassword);
System.out.println("修改密码成功");
return;
}else{
//两次不一致,需重新输入新密码
System.out.println("两次密码输入不一致");
}
}
}else{
System.out.println("密码不正确,请再次输入");
}
}
}
/**
* 定义转账操作
* @param sc
* @param m 当前用户
* @param accounts 用于查询目标账户是否在集合中
*/
private static void transformoney(Scanner sc, Account m, ArrayList<Account> accounts) {
if(accounts.size()<2){
System.out.println("系统里用户少于2人,需先退出,去开户");
return;
}
while (true) {
System.out.println("请输入对方的卡号");
String id = sc.next();
//排除输入的卡号是自己卡号的情况
if(id.equals(m.getCardId())){
System.out.println("这是你自己的卡号,请输入对方卡号");
continue; //结束当此循环,进入下一次
}
//1.调用方法,验证卡号在集合里有存档
Account acc=getAccountbyCardID(accounts,id); //acc是对方在集合里面的地址
if(acc==null){ //说明集合里面无存档
System.out.println("输入卡号错误,请重新输入");
continue;
}else{
//开始转账
System.out.println("请输入转账金额");
int money= sc.nextInt();
if(money>m.getBlance()){
System.out.println("对不起,账户余额不够,当前余额"+m.getBlance());
return;
}else{
m.setBlance(m.getBlance()-money);
acc.setBlance(acc.getBlance()+money);
System.out.println("转账成功,您当前余额为"+m.getBlance());
return;
}
}
}
}
/**
* 定义取款操作
* @param sc
* @param m
*/
private static void withdraw(Scanner sc, Account m) {
System.out.println("==========Withdraw=========");
System.out.println("please enter the amount of your withdraw: ");
while (true) {
int num = sc.nextInt();
if(num>5000||num>m.getBlance()){
System.out.println("sorry, the number is out of the quato 5000$ or out of blance,please enter again ");
}else{
int oldnum= m.getBlance();
m.setBlance(oldnum-num);
System.out.println("successfully wothdraw,now your blance is "+m.getBlance());
break;
}
}
}
/**
* 定义存款操作
* @param m
* @param sc
*/
private static void deposit(Account m, Scanner sc) {
System.out.println("========Deposit========");
int num= 0;
System.out.println("please enter the amount of your deposit: ");
while (true) {
num = sc.nextInt();
if(num>5000){
System.out.println("sorry, the number is out of the quato 5000$,please enter again ");
}else{
break;
}
}
int oldnum=m.getBlance();
m.setBlance(num+oldnum);
System.out.println("successfully deposit,now your balnce is "+m.getBlance());
}
/**
* 定义查询操作
* @param m 是当前用户的所在集合元素的地址
*/
private static void showAccount(Account m) {
System.out.println("==========The Result Are As Follows=========");
System.out.println("CardId:"+m.getCardId());
System.out.println("UserName:"+m.getName());
System.out.println("Balnce:"+m.getBlance());
System.out.println("Quato:"+m.getQuato());
}
/** 快捷键:/**然后按回车
* 定义注册方法
* @param accounts
* @param sc
*/
public static void register(ArrayList<Account> accounts,Scanner sc){ //注意:输入sc 是Scanner型
// 1.注册账户和密码
Account user =new Account(); //创建Account对象
System.out.println("请输入用户名字");
String username=sc.next();
user.setName(username); //把输入的用户名填到Account类的user对象里面
while (true) {
System.out.println("请您输入密码");
String password=sc.next();
System.out.println("请您输入确认密码");
String okpassword=sc.next();
if(password.equals(okpassword)){
user.setPassword(okpassword); //把输入的密码填到Account类的user对象里面
break; //因为有一个while,如果没有while硬加break会报错,因为break是跳出循环用的
}else{
System.out.println("密码输入错误,请重新输入"); //需要一个死循环
}
}
//2.引用方法,随机产生不重复卡号
String cardid=returncardid(accounts); //n return给cardid
user.setCardId(cardid); //把输入的卡号填到Account类的user对象里面
//3.设置单次取款限制额度
user.setQuato(5000);
//4.把user对象添加到集合accounts里面
accounts.add(user);
System.out.println("恭喜您开户成功,您的卡号为"+cardid+",单次取款限额为"+user.getQuato());
}
/**
* 定义一个方法,随机产生不重复的卡号
* @param accounts
* @return
*/
public static String returncardid(ArrayList<Account> accounts){
//生成八位卡号
while (true) {
//1.生成卡号,用id表示
Random sc=new Random();
String id="";
for (int i = 0; i < 8; i++) {
int m=sc.nextInt(10);
id+=m;
}
//2.引用方法,判断卡号重复否
Account n=getAccountbyCardID(accounts,id);
if(n==null){ //说明此时id没有重复,可以用
return id;
}else{
System.out.println("稍等,卡号重复,重新生成中");
}
}
}
/**
* 定义方法,判断卡号重复否
* @param accounts
* @return 返回集合地址/ null
*/
public static Account getAccountbyCardID(ArrayList<Account> accounts,String id){
for (int i = 0; i < accounts.size(); i++)
{
Account s =accounts.get(i);
if(s.getCardId().equals(id)){
return s; //return 之后,后面的语句不执行
}
}
return null; //表示当前集合里无和卡号id一样的账户
}
}
Account代码段
package practice;
//2.Account类包含 卡号,姓名,密码,余额,取现额度
public class Account {
private String cardId;
private String name;
private String password;
private int blance; //余额
private int quato; //单次取款限额
//没有写有参构造器,所以默认就有无参构造器,所以不打无参构造器了
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
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 getBlance() {
return blance;
}
public void setBlance(int blance) {
this.blance = blance;
}
public int getQuato() {
return quato;
}
public void setQuato(int quato) {
this.quato = quato;
}
}