Java设计模式之外观模式示例详解
330
2022-07-28
本文实例为大家分享了java实现简单银行ATM系统的具体代码,供大家参考,具体内容如下
#ATM系统
##功能
模拟银行ATM机系统,具VMWiCMBLZ有注册、登录功能用户登录后可实现以下功能:1)存款 2)取款 3)转账 4)查询 5)退出
##设计思路
首先,要进行ATM机操作应该具有银行卡和ATM,所以我们要设计一个ATM类和Bankcard类,ATM具有存取款等操作,Bankcard用来记录用户存取款等操作后的越,其次因为ATM同时为多个用户服务,所以我们应该识别每张卡,这里增加一个Bank类来记录银行卡个数以及各个用户持有哪些银行卡。
##具体代码
###Bank类
import java.util.Arrays;
/**
* @author:zl
* @Date 22:30 2020/10/9
*/
public class Bank {
private BankCard[] cards;
private int size;// 有效用户个数
private static final int INITSIZE =10;
public Bank(){
this(INITSIZE);
}
public Bank(int num){
cards = new BankCard[num];
}
publhttp://ic boolean add(BankCard card){//如果注册过,增加失败
if(contains(card.getID(),card.getPasswd())!=null){
return false;
}
if(size==cards.length){
cards= Arrays.copyOf(cards,cards.length>>1);//长度不够时进行扩容
}
cards[size++] = card;//
return true;
}
public boolean search(int id,int passwd){//查询是否存在这张卡
boolean k=true;
for(int i=0;i if((cards[i].getID()==id)&&(cards[i].getPasswd()==passwd)){ k=true; } else k=false; } return k; } public BankCard contains(int id,int passwd){//查询并返回这张卡 BankCard card=null; for(int i=0;i if((cards[i].getID()==id)&&(cards[i].getPasswd()==passwd)){ card=cards[i]; break; } } return card; } } ###Bankcard类 import java.util.Scanner; public class BankCard { private int id; private int passwd; public int money=0; public BankCard(int id,int passwd){ this.id=id; this.passwd=passwd; } public int getID(){ return this.id; } public int getPasswd(){ return this.passwd; } /** * 存款 */ public void saveMoney(int money){ this.money+=money; } /** * 取款 */ public boolean withDraw(int money){ boolean flag=false; if(this.money>=money){ this.money-=money; flag=true; } return flag; } /** * 返回余额 * @return */ public int getMoney(){ return this.money; } } ###ATM类 impVMWiCMBLZort java.util.Scanner; import src3.BankCard; public class ATM { private Bank bank; private static Scanner scanner; public ATM(){ bank = new Bank(); scanner = new Scanner(System.in); } public void start(){ while (true) { System.out.println("1. 登陆 2.注册 3.关机"); int chioce = scanner.nextInt(); if (chioce == 3) { break; } System.out.println("请输入账号密码"); int id = scanner.nextInt(); int passwd = scanner.nextInt(); BankCard successCard = null; switch (chioce){ case 1: successCard = login(id,passwd); if(successCard != null){ System.out.println("登陆成功"); loginSuccess(successCard); }else{ System.out.println("登陆失败"); } break; case 2: if(regiter(id,passwd)){ System.out.println("注册成功"); }else{ System.out.println("注册失败"); } break; } } } private void loginSuccess(BankCard successCard){ while (true) { System.out.println("1. 存款 2.取款 3.转账 4.余额 5.退卡"); int chioce = scanner.nextInt(); if (chioce == 5) { break; } switch (chioce) { case 1: System.out.println("请输入存款金额"); int money = scanner.nextInt(); successCard.saveMoney(money); System.out.println("存款成功"); break; case 2: System.out.println("请输入取款金额"); money = scanner.nextInt(); if (successCard.withDraw(money)) { System.out.println("取款成功"); } else { System.out.println("取款失败"); } break; case 3: System.out.println("请输入转账金额"); money = scanner.nextInt(); System.out.println("请输入转账用户账号,密码"); int id = scanner.nextInt(); int passwd = scanner.nextInt(); BankCard userCard = bank.contains(id, passwd); if (userCard != null) { if (successCard.withDraw(money)) {// 当前卡取款成功 userCard.saveMoney(money); System.out.println("转账成功"); } else { System.out.println("余额不足"); } } else { System.out.println("没有此用户"); } break; case 4: System.out.println("余额:" + successCard.getMoney()); break; } } } private boolean regiter(int id,int passwd){ BankCard card = new BankCard(id,passwd); return bank.add(card); } private BankCard login(int id,int passwd){ return bank.contains(id,passwd); } } ###测试类 public class TestDemo { public static void main(String[] args) { ATM atm = new ATM(); atm.start(); } }
if((cards[i].getID()==id)&&(cards[i].getPasswd()==passwd)){
k=true;
}
else k=false;
}
return k;
}
public BankCard contains(int id,int passwd){//查询并返回这张卡
BankCard card=null;
for(int i=0;i if((cards[i].getID()==id)&&(cards[i].getPasswd()==passwd)){ card=cards[i]; break; } } return card; } } ###Bankcard类 import java.util.Scanner; public class BankCard { private int id; private int passwd; public int money=0; public BankCard(int id,int passwd){ this.id=id; this.passwd=passwd; } public int getID(){ return this.id; } public int getPasswd(){ return this.passwd; } /** * 存款 */ public void saveMoney(int money){ this.money+=money; } /** * 取款 */ public boolean withDraw(int money){ boolean flag=false; if(this.money>=money){ this.money-=money; flag=true; } return flag; } /** * 返回余额 * @return */ public int getMoney(){ return this.money; } } ###ATM类 impVMWiCMBLZort java.util.Scanner; import src3.BankCard; public class ATM { private Bank bank; private static Scanner scanner; public ATM(){ bank = new Bank(); scanner = new Scanner(System.in); } public void start(){ while (true) { System.out.println("1. 登陆 2.注册 3.关机"); int chioce = scanner.nextInt(); if (chioce == 3) { break; } System.out.println("请输入账号密码"); int id = scanner.nextInt(); int passwd = scanner.nextInt(); BankCard successCard = null; switch (chioce){ case 1: successCard = login(id,passwd); if(successCard != null){ System.out.println("登陆成功"); loginSuccess(successCard); }else{ System.out.println("登陆失败"); } break; case 2: if(regiter(id,passwd)){ System.out.println("注册成功"); }else{ System.out.println("注册失败"); } break; } } } private void loginSuccess(BankCard successCard){ while (true) { System.out.println("1. 存款 2.取款 3.转账 4.余额 5.退卡"); int chioce = scanner.nextInt(); if (chioce == 5) { break; } switch (chioce) { case 1: System.out.println("请输入存款金额"); int money = scanner.nextInt(); successCard.saveMoney(money); System.out.println("存款成功"); break; case 2: System.out.println("请输入取款金额"); money = scanner.nextInt(); if (successCard.withDraw(money)) { System.out.println("取款成功"); } else { System.out.println("取款失败"); } break; case 3: System.out.println("请输入转账金额"); money = scanner.nextInt(); System.out.println("请输入转账用户账号,密码"); int id = scanner.nextInt(); int passwd = scanner.nextInt(); BankCard userCard = bank.contains(id, passwd); if (userCard != null) { if (successCard.withDraw(money)) {// 当前卡取款成功 userCard.saveMoney(money); System.out.println("转账成功"); } else { System.out.println("余额不足"); } } else { System.out.println("没有此用户"); } break; case 4: System.out.println("余额:" + successCard.getMoney()); break; } } } private boolean regiter(int id,int passwd){ BankCard card = new BankCard(id,passwd); return bank.add(card); } private BankCard login(int id,int passwd){ return bank.contains(id,passwd); } } ###测试类 public class TestDemo { public static void main(String[] args) { ATM atm = new ATM(); atm.start(); } }
if((cards[i].getID()==id)&&(cards[i].getPasswd()==passwd)){
card=cards[i];
break;
}
}
return card;
}
}
###Bankcard类
import java.util.Scanner;
public class BankCard {
private int id;
private int passwd;
public int money=0;
public BankCard(int id,int passwd){
this.id=id;
this.passwd=passwd;
}
public int getID(){
return this.id;
}
public int getPasswd(){
return this.passwd;
}
/**
* 存款
*/
public void saveMoney(int money){
this.money+=money;
}
/**
* 取款
*/
public boolean withDraw(int money){
boolean flag=false;
if(this.money>=money){
this.money-=money;
flag=true;
}
return flag;
}
/**
* 返回余额
* @return
*/
public int getMoney(){
return this.money;
}
}
###ATM类
impVMWiCMBLZort java.util.Scanner;
import src3.BankCard;
public class ATM {
private Bank bank;
private static Scanner scanner;
public ATM(){
bank = new Bank();
scanner = new Scanner(System.in);
}
public void start(){
while (true) {
System.out.println("1. 登陆 2.注册 3.关机");
int chioce = scanner.nextInt();
if (chioce == 3) {
break;
}
System.out.println("请输入账号密码");
int id = scanner.nextInt();
int passwd = scanner.nextInt();
BankCard successCard = null;
switch (chioce){
case 1:
successCard = login(id,passwd);
if(successCard != null){
System.out.println("登陆成功");
loginSuccess(successCard);
}else{
System.out.println("登陆失败");
}
break;
case 2:
if(regiter(id,passwd)){
System.out.println("注册成功");
}else{
System.out.println("注册失败");
}
break;
}
}
}
private void loginSuccess(BankCard successCard){
while (true) {
System.out.println("1. 存款 2.取款 3.转账 4.余额 5.退卡");
int chioce = scanner.nextInt();
if (chioce == 5) {
break;
}
switch (chioce) {
case 1:
System.out.println("请输入存款金额");
int money = scanner.nextInt();
successCard.saveMoney(money);
System.out.println("存款成功");
break;
case 2:
System.out.println("请输入取款金额");
money = scanner.nextInt();
if (successCard.withDraw(money)) {
System.out.println("取款成功");
} else {
System.out.println("取款失败");
}
break;
case 3:
System.out.println("请输入转账金额");
money = scanner.nextInt();
System.out.println("请输入转账用户账号,密码");
int id = scanner.nextInt();
int passwd = scanner.nextInt();
BankCard userCard = bank.contains(id, passwd);
if (userCard != null) {
if (successCard.withDraw(money)) {// 当前卡取款成功
userCard.saveMoney(money);
System.out.println("转账成功");
} else {
System.out.println("余额不足");
}
} else {
System.out.println("没有此用户");
}
break;
case 4:
System.out.println("余额:" + successCard.getMoney());
break;
}
}
}
private boolean regiter(int id,int passwd){
BankCard card = new BankCard(id,passwd);
return bank.add(card);
}
private BankCard login(int id,int passwd){
return bank.contains(id,passwd);
}
}
###测试类
public class TestDemo {
public static void main(String[] args) {
ATM atm = new ATM();
atm.start();
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~