取钱存钱demo

取钱存钱demo

六月 18, 2023 评论 10 阅读 446 点赞 1 收藏 0

Account.java

public class Account {
    private int id;// 帐号
    private double balance;// 余额
    private double annualInteresRate;// 年利率

    public Account(int id,double balance, double annualInteresRate){
        this.id = id;
        this.balance = balance;
        this.annualInteresRate = annualInteresRate;
    }
    public int getId(){
        return id;
    }

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

    public double getBalance() {
        return balance;
    }

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

    public double getAnnualInteresRate() {
        return annualInteresRate;
    }

    public void setAnnualInteresRate(double annualInteresRate) {
        this.annualInteresRate = annualInteresRate;
    }

    public void withdraw( double amount){ // 取钱
        if( balance < amount ){
             System.out.println("余额不足,取款失败");
             return;
        }
    }
    public void deposit(double amount){// 存钱

    }
}

Customer.java

public class Customer {
    private String firstName;
    private String lastName;
    private Account account;

    public Customer(String f,String l){
        this.firstName = f;
        this.lastName = l;
    }

    public Account getAccount() {
        return account;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

}

CustomerTest.java

public class CustomerTest {
    public static void main(String[] args) {
        Customer cust = new Customer("jane","Smith");
        Account acct = new Account(1000,2000,0.0123);

        cust.setAccount(acct);
        cust.getAccount().deposit(100);
        cust.getAccount().withdraw(960);
        cust.getAccount().withdraw(2000);
         System.out.println("Customer["+cust.getLastName()+","+cust.getFirstName()+
                 "] has a account: id is " + cust.getAccount().getId()+", annualInteresRate is "
                 + cust.getAccount().getAnnualInteresRate() * 100 + "% , balance is " + cust.getAccount().getBalance() );// Customer[Smith,jane] has a account: id is 1000, annualInteresRate is 1.23% , balance is 2000.0


    }
}

执行命令

javac CustomerTest.java
java CustomerTest
*
*
*