-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubclass.java
More file actions
133 lines (114 loc) · 3.73 KB
/
Copy pathsubclass.java
File metadata and controls
133 lines (114 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.util.Date;
public class subclass {
Account account = new Account(12345678, 5, 0);
CheckingAccount checkingAccount = new CheckingAccount(12345679, 5, 0);
SavingsAccount savingsAccount = new SavingsAccount(12345677, 5);
}
//The Account class
class Account {
private int id; // field named id for the account (default 0 )
private double balance; // field named balance for the account (default 0 )
private double annualInterestRate; // field named annualInterestRate that stores the current interest rate (default 0 )
Date dateCreated = new java.util.Date();// field dateCreated that stores the date when the account was created.
// constructor that creates a default account.
Account() {
this (0,0,0);
}
// constructor that creates an account with the specified id and initial balance.
Account(int newId, double newBalance, double annualInterestRate) {
this.id = newId;
this.balance = newBalance;
this.annualInterestRate = annualInterestRate;
}
// accessor and mutator methods for id , balance , and annualInterestRate
public void setId(int newId) {
this.id = newId;
}
public void setBalance(double newBalance) {
this.balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate) {
this.annualInterestRate = newAnnualInterestRate;
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
// method named getMonthlyInterestRate() that returns the monthly interest rate.
public double getMonthlyInterestRate() {
return (annualInterestRate / 12);
}
/* Hint: The method getMonthlyInterest() is to return monthly interest, not the interest rate.
* Monthly interest is balance * monthlyInterestRate . monthlyInterestRate
* is annualInterestRate / 12 . Note that annualInterestRate is a percentage,
* e.g., like 4.5%. You need to divide it by 100.
*/
// method named getMonthlyInterest() that returns the monthly interest
public double getMonthlyInterest() {
return balance * (getMonthlyInterestRate() / 100);
}
// method named withdraw that withdraws a specified amount from the account.
public void withdraw(double amount) {
balance -= amount;
}
// method named deposit that deposits a specified amount to the account.
public void deposit(double amount) {
balance += amount;
}
//
//public String getdateCreated() {
//return dateCreated.toString();
//}
// get date created
public java.util.Date getDateCreated() {
return dateCreated;
}
public String toString() {
return "Account " + id + " created on " + dateCreated + " has a balance of " + balance +
" and annual interest of " + annualInterestRate;
}
}
//The CheckingAccount class
class CheckingAccount
extends Account{
// CheckingAccount() {
//super();
//}
public double overdraft = -0.01;
// constructor for checking account
CheckingAccount(int id, double balance, double overdraft) {
super();
this.overdraft = overdraft;
}
// setter and getter for overdraft
public double getOverdraft() {
return this.overdraft;
}
public void setOverdraft(double overdraft) {
this.overdraft = overdraft;
}
// conditional statement for overdraft alert
public void withdraw(double amount) {
if (getBalance() - amount > overdraft)
super.withdraw(amount);
}
public String toString() {
return "Overdraft limit is: " + overdraft;
}
}
// The SavingsAccount class
class SavingsAccount
extends Account {
// constructor for savings account
SavingsAccount(int id, double balance) {
super();
}
public String toString() {
return "Balance is";
}
}