Java学习1209

2016-12-09  本文已影响0人  a04b20daaf33

面向对象入门

package com.baidu;

/**
 * 银行账户
 * @author Yizw
 *
 */
public class Account {
    private String id;
    private double balance;
    private String password;
    /**
     * 构造器
     * @param id 银行账户
     * @param password 初始密码
     * @param balance 初始余额
     */
    public Account(String id,String password,double balance){
        this.id = id;
        this.password = password;
        this.balance = balance;
    }
    /**
     * 验证身份
     * @param password  输入的密码
     * @return  密码匹配返回true否则返回false
     */
    public boolean isValid(String password){
        return this.password.equals(password);
    }
    /**
     * 存钱
     * @param money 存入金额
     */
    public void deposit(double money){
        balance += money;
    }
    /**
     * 取钱
     * @param money 取出金额
     * @return 能否成功取款
     */
    public boolean withdraw(double money){
        if(money <= balance){
            balance -= money;
            return true;
        }
        return false;
    }
    /**
     * 获取银行账户
     * @return 账户
     */
    public String getId() {
        return id;
    }
    /**
     * 获取账户余额
     * @return 账户余额
     */
    public double getBalance() {
        return balance;
    }
    /**
     * 设置密码
     * @param password 密码
     */
    public void setPassword(String password) {
        this.password = password;
    }
    
}

package com.baidu;
/**
 * 自动取款机
 * @author Yizw
 *
 */
public class ATM {
    private Account currentAccount;
    private Account[] accountsArray={
            new Account("1122334455","123456",10000),
            new Account("2233445566","123456",1000),
            new Account("3344556677","123456",100000)
    };
    /**
     * 读卡
     * @param id 银行账户id
     * @return 读卡成功返回true否则返回false
     */
    public boolean readCard(String id){
        for(Account account : accountsArray){
            if(account.getId().equals(id)){
                currentAccount = account;
                return true;
            }
        }
        return false;
    }
    /**
     * 验证密码
     * @param password 正确密码
     * @return 密码匹配返回true 否则返回false
     */
    public boolean verfify(String password){
        if(currentAccount != null){
            return currentAccount.isValid(password);
        }
        return false;
    }
    /**
     * 显示余额
     * @return 余额
     */
    public double showBalance(){
        return currentAccount.getBalance();
    }
    /**
     * 存钱
     * @param money 金额
     */
    public void deposit(int money){
        currentAccount.deposit(money);
    }
    /**
     * 取钱
     * @param money 金额
     * @return 取钱成功返回true否则返回false
     */
    public boolean withdraw(int money){
        return currentAccount.withdraw(money);
    }
    /**
     * 设置密码
     * @param newPassword 新密码
     */
    public void changePassword(String newPassword){
        currentAccount.setPassword(newPassword);
    }
    /**
     * 退卡
     */
    public void quitCard(){
        currentAccount = null;
    }
    /**
     * 转账
     * @param otherId 另一个账户
     * @param money 转账金额
     */
    public boolean transfer(String otherId,double money){
        for(Account account : accountsArray){
            if(account.getId().equals(otherId)){
                Account otherAccount = account;
                if(currentAccount.getBalance() >= money){
                    currentAccount.withdraw(money);
                    otherAccount.deposit(money);
                    return true;
                }
                else{
                    return false;
                }
            }
                
        }
        return false;
    }
}

package com.baid.test;

import java.util.Scanner;

import com.baidu.ATM;

public class ATMTest {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ATM atm = new ATM();
        boolean isRunning = true;
        while(isRunning){
            System.out.println("欢迎光临");
            System.out.println("请插入银行卡: ");
            String id = input.next();
            atm.readCard(id);
            if(atm.readCard(id)){
                int counter = 0;
                boolean isCorrect = false;
                do{
                counter += 1;
                System.out.print("请输入密码: ");
                String password = input.next();
                isCorrect = atm.verfify(password);
                }while(counter < 3 && !isCorrect);
                if(isCorrect){
                    boolean flag = true;
                    while(flag){
                        System.out.println("1.查询余额");
                        System.out.println("2.存钱");
                        System.out.println("3.取钱");
                        System.out.println("4.转账");
                        System.out.println("5.修改密码");
                        System.out.println("6.退卡");
                        int choice;
                        do{
                            System.out.println("请选择:");
                            choice = input.nextInt();
                        }while(choice < 1|| choice > 6);
                        switch (choice) {
                        case 1:
                            System.out.println("账户余额:" + atm.showBalance());
                            break;
                        case 2:{
                            // 一对花括号就构成了一个作用域(space)
                            // 在某个作用域中定义的变量只在该作用域生效
                            System.out.println("请放入钞票:");
                            int money = input.nextInt();
                            atm.deposit(money);
                            System.out.println("存款成功!");
                            break;
                        }
                        case 3:{
                            System.out.println("请输入或选择取款金额: ");
                            int money = input.nextInt();
                            if(atm.withdraw(money)){
                                System.out.println("取款成功!");
                            }
                            else{
                                System.out.println("余额不足!");
                            }
                            break;
                        }
                        case 4:{
                            System.out.println("请输入转入账号: ");
                            String otherId = input.next();
                            System.out.println("请输入转账金额: ");
                            double money = input.nextDouble();
                            if(atm.transfer(otherId, money)){
                                System.out.println("转账成功!");
                            }
                            else{
                                System.out.println("转账失败,请检查转入账号和账户余额是否正确");
                            }
                            break;
                        }
                        case 5:
                            System.out.println("请输入新密码 : ");
                            String newPwd = input.next();
                            System.out.println("请再输入一次 : ");
                            String rePwd = input.next();
                            if(newPwd.equals(rePwd)){
                                atm.changePassword(newPwd);
                                System.out.println("密码修改成功!");
                            }
                            else{
                                System.out.println("两次密码不一致。");
                            }
                            break;
                        case 6:
                            System.out.println("请取走您的卡片");
                        flag = false;
                        break;
                        }
                    }
                }
                else{
                    System.out.println("密码错误3次,你的卡被吞了");
                }
            }
        }
        input.close();
    }

}

上一篇下一篇

猜你喜欢

热点阅读