Java 练习(super练习)

java

银行账号,取钱,存钱,透支 练习

Account.java

package com.klvchen.exer2;

public class Account {

private int id;

private double balance;

private double annualInterestRate;

public Account(int id, double balance, double annualInterestRate) {

this.id = id;

this.balance = balance;

this.annualInterestRate = annualInterestRate;

}

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 getAnnualInterestRate() {

return annualInterestRate;

}

public void setAnnualInterestRate(double annualInterestRate) {

this.annualInterestRate = annualInterestRate;

}

//返回月利率

public double getMonthlyInterest() {

return annualInterestRate / 12;

}

//取钱

public void withdraw(double amount) {

if(balance >= amount ) {

balance -= amount;

return;

}

System.out.println("余额不足");

}

//存钱

public void deposit(double amount) {

if(amount > 0) {

balance += amount;

}

}

}

AccountTest.java

package com.klvchen.exer2;

public class AccountTest {

public static void main(String[] args) {

Account acct = new Account(1122, 20000, 0.045);

acct.withdraw(30000);

System.out.println("您的账户余额为: " + acct.getBalance());

acct.withdraw(2500);

System.out.println("您的账户余额为: " + acct.getBalance());

acct.deposit(3000);

System.out.println("您的账户余额为: " + acct.getBalance());

System.out.println("月利率为: " + (acct.getMonthlyInterest() * 100 ) + "%");

}

}

CheckAccount.java

package com.klvchen.exer2;

public class CheckAccount extends Account {

private double overdraft; //可透支限额

public CheckAccount(int id, double balance, double annualInterestRate, double overdraft) {

super(id, balance, annualInterestRate);

this.overdraft = overdraft;

}

public double getOverdraft() {

return overdraft;

}

public void setOverdraft(double overdraft) {

this.overdraft = overdraft;

}

@Override

public void withdraw(double amount) {

if(getBalance() >= amount) {

super.withdraw(amount);

}else if(overdraft >= amount - getBalance()) { //透支额度+余额足够消费

overdraft -= (amount - getBalance());

//setBalance(0);

//或

super.withdraw(getBalance());

}else {

System.out.println("超过可透支限额!");

}

}

}

CheckAccountTest.java

package com.klvchen.exer2;

public class CheckAccountTest {

public static void main(String[] args) {

CheckAccount acct = new CheckAccount(1122, 20000, 0.045, 5000);

acct.withdraw(50000);

System.out.println("您的账号余额为: " + acct.getBalance());

System.out.println("您的可透支额度为: " + acct.getOverdraft());

System.out.println("***********************************");

acct.withdraw(18000);

System.out.println("您的账号余额为: " + acct.getBalance());

System.out.println("您的可透支额度为: " + acct.getOverdraft());

System.out.println("***********************************");

acct.withdraw(3000);

System.out.println("您的账号余额为: " + acct.getBalance());

System.out.println("您的可透支额度为: " + acct.getOverdraft());

}

}

运行结果:

以上是 Java 练习(super练习) 的全部内容, 来源链接: utcz.com/z/391778.html

回到顶部