因为此系统用到了Scanner以及Random、ArrayList,直接复制进去可能会出现缺少包的错误,只需要根据提示导入即可 或者将 import java.util.ArrayList; import java.util.Random; import java.util.Scanner; 复制进去
首先定义一个Account类(账户类)
public class Account {
private String cardId;
private String userName;
private String passWord;
private double money;
private double quotaMoney;
public Account() {
}
public Account(String cardId, String userName, String passWord, double money, double quotaMoney) {
this.cardId = cardId;
this.userName = userName;
this.passWord = passWord;
this.money = money;
this.quotaMoney = quotaMoney;
}
public String getCardId() {
return cardId;
}
public void setCardId(String cardId) {
this.cardId = cardId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public double getQuotaMoney() {
return quotaMoney;
}
public void setQuotaMoney(double quotaMoney) {
this.quotaMoney = quotaMoney;
}
}
然后定义主菜单界面以及各种操作
public class ATMSystem {
public static void main(String[] args) {
ArrayList<Account> accounts = new ArrayList<>();
System.out.println("===================欢迎进入ATM系统===================");
while (true) {
System.out.println("1.登陆账户");
System.out.println("2.注册账户");
System.out.println("请输入您的选择:");
Scanner sc = new Scanner(System.in);
int command = sc.nextInt();
switch (command) {
case 1:Login(accounts,sc);
break;
case 2:
register(accounts, sc);
break;
default:
System.out.println("没有此操作");
}
}
}
private static void Login(ArrayList<Account> accounts, Scanner sc) {
System.out.println("=====================登陆操作====================");
if(accounts.size()>0){
while(true) {
System.out.println("请输入卡号:");
String cardId = sc.next();
Account account = getAccountByCardId(cardId, accounts);
if (account == null) {
System.out.println("卡号不正确,请重新输入");
} else {
while (true) {
System.out.println("请输入密码:");
String passWord = sc.next();
if (account.getPassWord().equals(passWord)) {
System.out.println("登陆成功,");
showCommand(sc,account,accounts);
return;
} else {
System.out.println("您输入的密码不正确");
}
}
}
}
}else{
System.out.println("当前系统无任何账户,请先注册~~~");
}
}
private static void showCommand(Scanner sc, Account account, 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(account);
break;
case 2:depositMoney(account,sc);
break;
case 3:drawMoney(account,sc);
break;
case 4:transferMoney(account,accounts,sc);
break;
case 5:updatePassWord(account,sc);
return;
case 6:
System.out.println("欢迎下次光临");
return;
case 7:accounts.remove(account);
System.out.println("您的账户已销毁,此账户无法进行登录等操作");
return;
default:
System.out.println("您操作的命令有误!请重新输入(1~7)");
}
}
}
private static void updatePassWord(Account account, Scanner sc) {
while(true){
System.out.println("请您输入当前账户密码:");
String oldPassWord=sc.next();
if(oldPassWord.equals(account.getPassWord())){
while(true){
System.out.println("请输入您要修改的密码:");
String passWord=sc.next();
System.out.println("请再次确认新密码:");
String okPassWord=sc.next();
if(passWord.equals(okPassWord)){
account.setPassWord(okPassWord);
return;
}else{
System.out.println("您输入的密码前后不一致,请重新输入密码");
}
}
}{
System.out.println("您输入的原来的旧密码不正确,无法修改密码");
}
}
}
private static void transferMoney(Account account, ArrayList<Account> accounts, Scanner sc) {
System.out.println("========================欢迎进去转账界面==================");
if(account.getMoney()<=0){
System.out.println("没钱怎么给别人转账,还是先去搬砖吧!");
return;
}
if(accounts.size()>=2){
while(true){
System.out.println("请您输入对方卡号:");
String cardId=sc.next();
Account account1=getAccountByCardId(cardId,accounts);
if(account1==account){
System.out.println("不能给自己转账,请重新选择转账对象!");
}else{
String rs="*"+account1.getUserName().substring(1);
System.out.println("请您输入["+rs+"]的姓氏来确认");
String preName=sc.next();
if(account1.getUserName().startsWith(preName)){
while(true){
System.out.println("请输入您要转账的金额:");
double money=sc.nextDouble();
if(money>account.getQuotaMoney()){
System.out.println("不能转这么多,超过限额了,少转点");
}else if(money>account.getMoney()){
System.out.println("您自己都没这么多钱,就别转给别人了嘛");
}else{
account.setMoney(account.getMoney()-money);
account1.setMoney(account1.getMoney()+money);
System.out.println("您已经完成转账,您当前还剩余"+account.getMoney()+"元");
return;
}
}
}else{
System.out.println("您输入的姓氏有问题");
}
}
}
}else{
System.out.println("本系统账户信息总共都没有两个,给谁转?");
}
}
private static void drawMoney(Account account, Scanner sc) {
System.out.println("===================取款界面===============");
while (true){
System.out.println("请输入要取款的金额:");
double money=sc.nextDouble();
if(money>account.getMoney()){
System.out.println("您的账户余额不足");
}else{
if(money>account.getQuotaMoney()){
System.out.println("您要取的金额超过单次上限");
}else{
account.setMoney(account.getMoney()-money);
System.out.println("取款成功!!!!");
break;
}
}
}
}
private static void depositMoney(Account account, Scanner sc) {
System.out.println("==================存款界面=======================");
System.out.println("请输入要存的金额:");
double money=sc.nextDouble();
account.setMoney(account.getMoney()+money);
showAccount(account);
}
private static void showAccount(Account account) {
System.out.println("===============您当前账户详细信息如下==================");
System.out.println("卡号:"+account.getCardId());
System.out.println("户主:"+account.getUserName());
System.out.println("余额:"+account.getMoney());
System.out.println("当次取现额度:"+account.getQuotaMoney());
}
private static void register (ArrayList < Account > accounts, Scanner sc){
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("");
System.out.println("请输入确认密码:");
String okPassword = sc.next();
if (password.equals(okPassword)) {
account.setPassWord(okPassword);
break;
} else {
System.out.println("您前后输入的密码不一致");
}
}
System.out.println("请您设置当次取现额度:");
double quotaMoney = sc.nextDouble();
account.setQuotaMoney(quotaMoney);
String cardId = createCardId(accounts);
account.setCardId(cardId);
accounts.add(account);
System.out.println("恭喜您" + account.getUserName() + "先生或女士,您开户完成,您的卡号是:" + account.getCardId());
}
private static String createCardId (ArrayList < Account > accounts) {
String cardId = "";
Random r = new Random();
while (true) {
for (int i = 0; i < 8; i++) {
cardId += r.nextInt(10);
}
Account account = getAccountByCardId(cardId, accounts);
if (account == null) {
return cardId;
}
}
}
private static Account getAccountByCardId (String cardId, ArrayList < Account > accounts){
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).getCardId().equals(cardId)) {
return accounts.get(i);
}
}
return null;
}
}
|