package com.atguigu.exer;
public class Account {
protected int id;//账号
protected double balance;//余额
protected double annualInterestRate;//年利率
public Account(int id, double balance, double annualInterestRate) {
super();
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 this.annualInterestRate/12;
}
//存款
public void deposit (double amount){
balance += amount;
}
//取款
public void withdraw (double amount){
if(this.balance >= amount)
balance -= amount;
else
System.out.println("余额不足!");
}
}
package com.atguigu.exer;
/*
- 创建Account类的一个子类CheckAccount代表可透支的账户,
- 该账户中定义一个属性overdraft代表可透支限额。在CheckAccount类中重写withdraw方法,
- 其算法如下:
如果(取款金额<账户余额),
可直接取款
如果(取款金额>账户余额),
计算需要透支的额度
判断可透支额overdraft是否足够支付本次透支需要,如果可以
将账户余额修改为0,冲减可透支金额
如果不可以
提示用户超过可透支额的限额
*/
public class CheckAccount extends Account {
private double overdraft;// 可透支额度
public double getOverdraft() {
return overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
public CheckAccount(int id, double init_balance, double annualIterestRate,
double overdraft) {
super(id, init_balance, annualIterestRate);
this.overdraft = overdraft;
}
//存在透支额度的取钱的操作
public void withdraw (double amount){
if(balance >= amount){
balance -= amount;
}else if(overdraft >= amount - balance){
overdraft -= (amount - balance);
balance = 0;
}else{
System.out.println("超过可透支额的限额");
}
}
}
package com.atguigu.exer;
/*
- 定义类Kids继承ManKind,并包括
成员变量int yearsOld;
方法printAge()打印yearsOld的值。
*/
public class Kids extends ManKind{
private int yearsOld;
public int getYearsOld() {
return yearsOld;
}
public void setYearsOld(int yearsOld) {
this.yearsOld = yearsOld;
}
public void printAge(){
System.out.println(this.yearsOld + " years old");
}
public void employees(){
super.employees();
System.out.println("Kids should study and no job");
}
}
package com.atguigu.exer;
/*
- 定义一个ManKind类,包括
成员变量int sex和int salary;
方法void manOrWorman():根据sex的值显示“man”(sex1)或者“woman”(sex0);
方法void employeed():根据salary的值显示“no job”(salary==0)或者“ job”(salary!=0)。
*/
public class ManKind {
private int sex;
private int salary;
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public void manOrWoman(){
if(sex == 1)
System.out.println("Man");
else if(sex == 0)
System.out.println("Woman");
else
System.out.println("输入的有误");
}
public void employees(){
if(salary == 0)
System.out.println("no job!");
else if(salary > 0)
System.out.println("job");
}
}
package com.atguigu.exer;
/*
- 写一个用户程序测试Account类。在用户程序中,创建一个账号为1122、余额为20000、年利率4.5%的Account对象。
- 使用withdraw方法提款30000元,并打印余额。
再使用withdraw方法提款2500元,使用deposit方法存款3000元,然后打印余额和月利率。
*/
public class TestAccount {
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);
acct.deposit(3000);
System.out.println("当前余额为:"+acct.getBalance());
System.out.println("月利率为:" + acct.getMonthlyInterest());
}
}
package com.atguigu.exer;
/*
- 写一个用户程序测试CheckAccount类。在用户程序中,
- 创建一个账号为1