博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA - ATM机程序
阅读量:5129 次
发布时间:2019-06-13

本文共 5599 字,大约阅读时间需要 18 分钟。

    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();    }}

 

转载于:https://www.cnblogs.com/tangshengwei/p/6195797.html

你可能感兴趣的文章
mv命令
查看>>
mysql读写分离
查看>>
webRTC源码下载 Windows Mac(iOS) Linux(Android)全
查看>>
函数和方法的区别
查看>>
树剖想法题——BZOJ3626
查看>>
master
查看>>
Duilib使用wke显示echarts
查看>>
linux lsof用法
查看>>
windows(64位)下使用curl命令
查看>>
杭电2093
查看>>
字符串 “ ” 的方法
查看>>
Android初学第72天
查看>>
Fiddler抓包后保存为JMX(jmeter脚本,不限jmeter使用版本)
查看>>
[SimplePlayer] 3. 视频帧同步
查看>>
UVA 11027 - Palindromic Permutation
查看>>
Android LayoutInflater原理分析
查看>>
AS不能真机调试 (转)
查看>>
SQL SERVER代码生成器必备
查看>>
使用NET USE将USB端口模拟为LPT1
查看>>
二维数组和指向指针的指针
查看>>