1 package oo.day06; 2 3 import java.util.Scanner; 4 5 public class ATM { 6 7 UnionPay card; 8 9 public void insertcard(UnionPay card){ 10 11 this.card = card; 12 } 13 14 15 public void PayTelbill(){ 16 17 Scanner input = new Scanner(System.in); 18 19 if(card instanceof ABC){ 20 ABC Card = (ABC)card; 21 System.out.println("请输入电话号码:"); 22 String phoneNum = input.next(); 23 System.out.println("请输入缴费额:"); 24 //double number = Double.parseDouble(input.next()); 25 double number = input.nextDouble(); 26 27 if(Card.payTelBill(phoneNum,number)){ 28 //System.out.println(card.getBalance()); 29 System.out.println("缴费成功"); 30 }else{ 31 System.out.println("缴费失败"); 32 } 33 34 }else{ 35 System.out.println("您的卡不是农业银行卡,无法完成缴费"); 36 } 37 } 38 39 40 public void drawmoney(){ 41 Scanner input = new Scanner(System.in); 42 System.out.println("请输入取钱金额:"); 43 double number = input.nextDouble(); 44 if(card.drawMoney(number)){ 45 46 System.out.println("取钱成功!"); 47 } 48 } 49 50 public void checkPwd(){ 51 52 Scanner input = new Scanner(System.in); 53 System.out.println("请输入密码:"); 54 String mima =input.next(); 55 System.out.println("请选择功能:"); 56 System.out.println("1.查询余额 2.取款 3.缴电话费"); 57 int n = input.nextInt(); 58 switch(n){ 59 60 case 1: 61 //card.getBalance(); 62 System.out.println(card.getBalance()); 63 break; 64 case 2: 65 drawmoney();break; 66 case 3: 67 PayTelbill();break; 68 } 69 70 71 } 72 73 74 75 }
1 package oo.day06; 2 3 4 //农行卡 5 class ABCImpl implements ABC{ 6 7 private String pwd; 8 private double balance; 9 ABCImpl(String pwd,double balance){ 10 11 this.balance=balance; 12 this.pwd = pwd; 13 } 14 15 16 @Override 17 public boolean payTelBill(String phoneNum, double number) { 18 19 if(phoneNum.length() == 11 && (balance-number)>=-2000){ 20 21 balance-=number; 22 23 return true; 24 } 25 else 26 return false; 27 } 28 29 @Override 30 public boolean checkPwd(String mima) { 31 if(pwd.equals("mima")){ 32 33 return true; 34 } 35 else 36 return false; 37 } 38 39 @Override 40 public boolean drawMoney(double number) { 41 if((balance-number)>-2000){ 42 balance -= number; 43 return true; 44 } 45 else 46 return false; 47 } 48 49 @Override 50 public double getBalance() { 51 return balance; 52 } 53 }
package oo.day06; import java.util.Scanner; public class UnionPayText { public static void main(String[] args) { ATM atm = new ATM(); ABCImpl card = new ABCImpl("123456",2000); atm.insertcard(card); atm.checkPwd(); /* UnionPayICBC icbc = new ICBCImpl("123456",3000); Scanner scan =new Scanner(System.in); System.out.println("请输入密码:"); String input = scan.next(); boolean str= icbc.checkPwd(input); System.out.println("请输入金额:"); double number = scan.nextInt(); //icbc.payOnline(100); if(icbc.drawMoney(number)){ System.out.println("取钱成功,卡上余额为:"+icbc.getBalance()); }*/ /* UnionPay abc = new ABCImpl("123456",3000); Scanner scann =new Scanner(System.in); System.out.println("请输入密码:"); String inp = scann.next(); boolean str1= abc.checkPwd(inp); System.out.println("请输入金额:"); double number1 = scann.nextInt(); //icbc.payOnline(100); if(abc.drawMoney(number1)){ System.out.println("取钱成功,卡上余额为:"+abc.getBalance()); } */ } }
package oo.day06; public interface UnionPay{ //查询余额 double getBalance(); //取钱 boolean drawMoney(double number); //检查密码 boolean checkPwd(String input); } interface ICBC extends UnionPay{ //增加的在线支付功能 public void payOnline(double number); } interface ABC extends UnionPay{ //增加的在线支付功能 public boolean payTelBill(String phoneNum, double number); }