java - day13 - UnionPay
银行功能实现,主要为抽象类及接口的运用
1 package com.javatest.bankimple; 2 /* UnionPay */ 3 //银联接口 4 public interface UnionPay { 5 public static final double EXCHANGERATE = 6.13; //汇率 6 public abstract void save(); //存钱 7 double pick(double pick); //取钱 8 void check(); //查询余额 9 boolean checkPassWord(int password); //验证密码 10 } 11 12 interface ICBC extends UnionPay{ //工商行接口 13 public abstract void onlineCharge(); //在线支付 14 } 15 16 interface ABC extends UnionPay{ //农行接口 17 public abstract void onlineChargeForMobile(); //在线支付电话费 18 }
1 package com.javatest.bankimple; 2 import com.javatest.bankimple.*; 3 4 /* 工商行及农行的接口实现 */ 5 6 //实现工商行接口 7 public class ICBCImple implements ICBC{ 8 private double currentSum; //卡上目前余额,钱的来源就是要存储、读取记录的地方 9 private int passwordPerson; 10 ICBCImple(double currentSum,int passwordPerson){ 11 this.currentSum = currentSum; 12 this.passwordPerson = passwordPerson; 13 } 14 15 /* 16 * 存钱 17 * */ 18 public void save(){} 19 20 /* 21 * 取钱 22 * */ 23 public double pick(double pickNum){ 24 25 //核查是否可取款 26 if(pickNum<=currentSum){ 27 currentSum -= pickNum;//卡上余额 28 System.out.println("取款成功,余额为:"+currentSum); 29 }else{ 30 System.out.println("卡上余额不足"); 31 } 32 return currentSum; 33 } 34 35 /* 36 * 查询余额 37 * */ 38 public void check(){ 39 System.out.println("卡上余额为:"+currentSum); 40 } 41 42 /* 43 * 验证密码 44 * */ 45 public boolean checkPassWord(int passwordICBC){ 46 boolean flag; 47 if(passwordICBC == passwordPerson){ 48 System.out.println("输入正确"); 49 flag = true; 50 }else{ 51 System.out.println("输入错误"); 52 flag = false; 53 } 54 return flag; 55 } 56 57 /* 58 * 在线支付 59 * */ 60 public void onlineCharge(){} 61 } 62 63 64 //农行接口 65 class ABCImple implements ABC{ 66 boolean flag; 67 68 public void save(){} //存 69 public double pick(double pickNum){ 70 return 0; 71 } //取钱 72 public void check(){ 73 } //查钱 74 public boolean checkPassWord(int passwordABC){ 75 return flag; 76 } //验证密码 77 public void onlineChargeForMobile(){} //在线支付电话费 78 }
1 package com.javatest.bankimple; 2 import com.javatest.bankimple.UnionPay; 3 import com.javatest.bankimple.ICBCImple; 4 import java.util.Scanner; 5 6 /* main测试 */ 7 8 public class BankImple { 9 public static void main(String[] args){ 10 UnionPay icbc = new ICBCImple(50000,123);//建工商行--向上造型 11 12 //实现工商行功能 13 System.out.println("工商银行欢迎你"); 14 Scanner scanICBC = new Scanner(System.in); 15 16 /* 17 * 验证密码 18 * */ 19 System.out.print("输入密码:"); 20 int password = scanICBC.nextInt(); 21 22 if(icbc.checkPassWord(password)){ 23 /* 24 * 查询余额 25 * */ 26 System.out.println("是否要查询余额(Y/N):"); 27 String inputRemain = scanICBC.next(); 28 if(inputRemain.equals("Y")){ 29 icbc.check(); 30 }else if(inputRemain.equals("N")){ 31 System.out.println("不查询余额"); 32 } 33 34 /* 35 * 取钱 36 * */ 37 System.out.print("请输入取款数目:"); 38 double pickICBC = scanICBC.nextDouble(); 39 icbc.pick(pickICBC); 40 } 41 else{ 42 System.out.println("密码错误,无权访问"); 43 } 44 } 45 }
其中,类成员变量私有、对应方法公开以供外部访问/调用的用法 --供外部访问,但不允许外部修改
1 public class ICBCImple implements ICBC{ 2 private double money; //卡上余额 3 private int pwd; //密码 4 ICBCImple(double money,int pwd){ 5 this.money = money; 6 this.pwd = pwd; 7 } 8 .... 9 }