首页 > 代码库 > JAVA - ATM机程序
JAVA - ATM机程序
ATM机程序
UnionPayTest.java
package oo.day06.work; public class UnionPayTest { } interface UnionPay{ //银联接口 public double getBalance(); //查询余额 public boolean drawMoney(double number); //取款 public boolean checkPwd(String input); //检查密码 } interface ABC extends UnionPay{ //农行接口 /**支付电话费*/ public boolean payTelBill(String phoneNum,double sum); } interface CCB extends UnionPay { /**增加的支付燃气费功能*/ public void payGasBill(double number); } class ABCImpl implements ABC{ //农行卡 private double money; private String pwd; public ABCImpl(double money,String pwd){ this.money = money; this.pwd = pwd; } @Override public double getBalance() { return money; } @Override public boolean drawMoney(double number) { if(number <= money){ money -=number; return true; } return false; } @Override public boolean checkPwd(String input) { if(pwd.equals(input)) return true; else return false; } public boolean payTelBill(String phoneNum,double sum){ if(sum <= money){ money -=sum; return true; } return false; } } /** * 类:用于描述建设银行实际发行的卡片 * 该卡片具有的功能来自于继承的已经符合银联规范的CCB接口 */ class CCBImpl implements CCB { private double money; private String pwd; public CCBImpl(double money,String pwd){ this.money = money; this.pwd = pwd; } @Override public double getBalance() { return money; } @Override public boolean drawMoney(double number) { if(number <= money){ money -=number; return true; } return false; } @Override public void payGasBill(double number) { if(number < money){ money-=number; } } @Override public boolean checkPwd(String input) { if(pwd.equals(input)) return true; else return false; } }
TestUnionPay.java (用于测试上面的程序使用)
package oo.day06.work; import java.util.Scanner; /** * 测试实现接口后的类的方法调用 */ public class TestUnionPay { public static void main(String[] args) { //ICBCImpl icbc = new ICBCImpl(2000,"123456"); //ICBC icbc = new ICBCImpl(2000,"123456"); //UnionPay icbc = new ICBCImpl(2000,"123456"); //UnionPay icbc = new ABCImpl(2000,"123456"); UnionPay ccb = new CCBImpl(2000,"123456"); //这里用了“向上造型” Scanner input = new Scanner(System.in); System.out.println("请输入密码:"); if(ccb.checkPwd(input.next())){ System.out.println("请输入金额:"); double num = Double.parseDouble(input.next()); if(ccb.drawMoney(num)){ System.out.println("取钱成功,卡余额为:"+ccb.getBalance()); } else{ System.out.println("取钱失败"); } }else{ System.out.println("密码错误"); } } }
ABCATMTest.java
package oo.day06.work; import java.util.Scanner; public class ABCATMTest { } /** * ABCATM机类,用于描述模拟插入银行卡后的操作 */ class ABCATM { private UnionPay card; public void insertCard(UnionPay userCard){ if (card == null) { card = userCard; } } private void outCard(){ card = null; } private void showBalance(){ System.out.println("当前余额是:" + card.getBalance()); } private void takeMoney(){ Scanner input = new Scanner(System.in); System.out.println("请输入取款数目:"); double number = Double.parseDouble(input.next()); if(card.drawMoney(number)){ System.out.println("取款成功"); }else{ System.out.println("取款失败"); } } private void payTelBill(){ Scanner input = new Scanner(System.in); if(card instanceof ABC){ ABC abcCard = (ABC)card; //这里用了“向下造型”。 System.out.println("请输入电话 号码:"); String telNum = input.next(); System.out.println("请输入缴费额:"); double sum = Double.parseDouble(input.next()); if(abcCard.payTelBill(telNum,sum)){ System.out.println("缴费成功"); }else{ System.out.println("缴费失败"); } }else{ System.out.println("您的卡不是农业银行卡,无法完成缴费"); } } public void allMenu(){ Scanner input = new Scanner(System.in); System.out.println("请输入密码:"); String pwd = input.next(); if(card.checkPwd(pwd)){ System.out.println("请选择功能:\n 1.查看余额 2.取款 3.缴电话费"); int choice = Integer.parseInt(input.next()); switch(choice){ case 1: showBalance(); break; case 2: takeMoney(); break; case 3: payTelBill(); break; default: System.out.println("非法输入"); } }else{ System.out.print("密码错误"); } this.outCard(); } }
TestATM.java (用于测试上面的程序使用)
package oo.day06.work; public class TestATM { public static void main(String[] args) { ABCATM atm =new ABCATM(); // ICBCImpl icbc = new ICBCImpl(3000, "123456");//工商银行的卡 ABCImpl abc = new ABCImpl(1000, "123456");//农业银行的卡 // CCBImpl abc = new CCBImpl(1000, "123456");//农业银行的卡 //atm.insertCard(icbc); atm.insertCard(abc); atm.allMenu(); } }
JAVA - ATM机程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。