Java面向对象练习

1 客户银行存取钱

1.1 定义客户类Customer

package cc.linux.homework;

public class Customer {
    private String name;
    private Account account;

    public Customer() {
    }

    public Customer(String name, Account account) {
        this.name = name;
        this.account = account;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Account getAccount() {
        return account;
    }

    public void setAccount(Account account) {
        this.account = account;
    }
}

1.2 定义银行账户类Account

package cc.linux.homework;

public class Account {
    private String id; // 账号ID
    private double balance; // 余额
    private double annualInterestRate; // 利率

    public Account() {
    }

    public Account(String id, double balance, double annualInterestRate) {
        this.id = id;
        this.balance = balance;
        this.annualInterestRate = annualInterestRate;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
        this.annualInterestRate = annualInterestRate;
    }

    /**
     * <h3>存款</h3>     *
     *
     * @param money 需要存入的金额
     */
    public void deposit(double money) {

        if (this.check(money)) {
            System.out.println("对不起,存款金额不能小于1");
            return;
        }

        this.setBalance(this.getBalance() + money);
        System.out.println("成功存入:" + money);

    }

    /**
     * <h3>取款</h3>     *
     *
     * @param money 取款金额
     */
    public void drawMoney(double money) {
        if (this.check(money)) {
            System.out.println("对不起,取款金额不能小于1");
            return;
        }

        if (money > this.getBalance()) {
            System.out.println("对不起,您的余额不足!");
            return;
        }
        // 余额充足
        this.setBalance(this.getBalance() - money);
        System.out.println("成功取出:" + money);

    }

    private boolean check(double num) {
        return num < 1;
    }
}

1.3 测试类 AccountTest

package cc.linux.homework;

public class AccountTest {
    public static void main(String[] args) {
        Account account1 = new Account("10000", 2000, 1.24);
        Customer c1 = new Customer("ZhangSan", account1);
        c1.getAccount().deposit(100);
        c1.getAccount().drawMoney(500);
        c1.getAccount().drawMoney(1960);

    }
}

1.4运行结果

创作不易 请尊重他人劳动成果,未经授权禁止转载!
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇