Understanding inheritance and different classes - java

I'm messing around with inheritance and confused on how to do a few different things. Here is what I have:
Looking at the Account class and write a main method in a different class Bank to brief experiment with some instances of the Account class.
Using the Account class as a base class, write two derived classes called SavingsAccount and CheckingAccount. A SavingsAccount object, in addition to the attributes of an Account object, should have an interest variable and a method which adds interest to the account. A CheckingAccount object, in addition to the instance variables of an Account object, should have an overdraft limit variable. Ensure that you have overridden methods of the Account class as necessary in both derived classes.
Now create a Bank class, an object of which contains an array of Account objects. Accounts in the array could be instances of the Account class, the SavingsAccount class, or the CheckingAccount class. Create some test accounts (some of each type).
Write an update method in the bank class. It iterates through each account, updating it in the following ways: Savings accounts get interest added (via the method you already wrote); Checking Account get a letter sent if they are in overdraft.
public class Account {
private double balance;
private int acctNum;
public Account (int num)
{
balance = 0.0;
acctNum = num;
}
public void deposit (double amt)
{
if (amt >0)
balance +=amt;
else
System.out.println("Account.deposit(...): "
+"cannot deposit negative amount.");
}
public void withdraw (double amt)
{
if (amt>0)
balance -=amt;
else
System.err.println("Account.withdraw(...): "
+"cannot withdraw negative amount.");
}
public double getBalance()
{
return balance;
}
public double getAccountNumber()
{
return acctNum;
}
public String toString()
{
return "Acc " + acctNum + ": " + "balance = "+ balance;
}
public final void print()
{
System.out.println( toString());
}
}
Now the SavingsAccount
public class SavingsAccount extends Account {
private double interest;
public SavingsAccount(int acctNum, double interest) {
super(acctNum);
this.interest=interest;
}
public double getInterest() {
double x= getBalance() + getBalance()*interest;
return x;
// public void setInterest((interest))
// this.interest=interest;
}
public void AddInterest (double interest) {
double x = super.getBalance() * interest;
super.deposit(x);
}
public String toString() {
return super.toString()+" Interest : " + interest;
}
}
CheckingAccount
public class CheckingAccount extends Account {
private double limit;
public CheckingAccount(int acctNum, double limit) {
super(acctNum);
this.limit=limit;
}
public double getLimit() {
return this.limit;
}
public void setLimit(double limit) {
this.limit=limit;
}
public void withdraw (double limit) {
if (limit <= this.limit)
super.withdraw(limit);
else {
System.out.println(" Sorry, Limit Exceeded" );
}
}
public String toString() {
return super.toString() +"Limit : "+limit;
}
}
Bank class
public class Bank {
public static void main(String[] args) {
Account[] accounts = new Account[2];
accounts[0] = new SavingsAccount(2, 0.25);
accounts[1] = new CheckingAccount(23, 50);
for(int i=0; i<accounts.length;i++) {
if (accounts[0].equals(SavingsAccount)
System.out.println(accounts[0].getInterest());
}
}
So here is my problems that I am noticing.
In the SavingsAccount I'm not sure how to get the setInterest() to work. I have tried changing it to a int, but then that changes the Account class. How do I get that to work properly?
In the bank class - the for loop I have written. Its not working, I have tried if(accounts[i].equals so so, but does not seem to function properly. What is the correct way?
As well, I'm assuming everything is coming from my SavingsAccount class.

If I understand what you are trying to do in your for-loop you need to use instanceof to check classes, not equals().
So, for example
Account[] accounts = new Account[2];
accounts[0] = new SavingsAccount(2, 0.25);
accounts[1] = new CheckingAccount(23, 50);
for(int i=0; i<accounts.length;i++) {
if (accounts[i] instanceof SavingsAccount) {
// You must cast an Account to use any of the descendant's methods
SavingsAccount account = (SavingsAccount) accounts[i];
System.out.println(account.getInterest());
} else { // it's a CheckingAccount
}
}
Other than that, StackOverflow is not a place for you to dump homework requirements, so your question is overly broad to answer in completion.

Related

Cannot make a static reference to the non-static field message ..int randomNumber=r.nextInt(message.length); [duplicate]

I apologize ahead of time if this code isn't formatted correctly, trying to paste instead of retyping each line. If it isn't right, can someone tell me an easy way to paste multiple lines of code at once?
My main question is that I keep getting an error message stating: Cannot make a static reference to the non-static field balance.
I have tried making the methods static, with no result, and making the main method non-static by removing "static" from the header, but then I get the message: java.lang.NoSuchMethodError: main Exception in thread "main"
Does anyone have any ideas? Any help is appreciated.
public class Account {
public static void main(String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(balance, 2500);
account.deposit(balance, 3000);
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
System.out.println("The account was created " + account.getDateCreated());
}
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
public java.util.Date dateCreated;
public Account() {
}
public Account(int id, double balance, double annualInterestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = annualInterestRate;
}
public void setId(int i) {
id = i;
}
public int getID() {
return id;
}
public void setBalance(double b){
balance = b;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double interest) {
annualInterestRate = interest;
}
public java.util.Date getDateCreated() {
return this.dateCreated;
}
public void setDateCreated(java.util.Date dateCreated) {
this.dateCreated = dateCreated;
}
public static double withdraw(double balance, double withdrawAmount) {
double newBalance = balance - withdrawAmount;
return newBalance;
}
public static double deposit(double balance, double depositAmount) {
double newBalance = balance + depositAmount;
return newBalance;
}
}
main is a static method. It cannot refer to balance, which is an attribute (non-static variable). balance has meaning only when it is referred through an object reference (such as myAccount.balance or yourAccount.balance). But it doesn't have any meaning when it is referred through class (such as Account.balance (whose balance is that?))
I made some changes to your code so that it compiles.
public static void main(String[] args) {
Account account = new Account(1122, 20000, 4.5);
account.withdraw(2500);
account.deposit(3000);
and:
public void withdraw(double withdrawAmount) {
balance -= withdrawAmount;
}
public void deposit(double depositAmount) {
balance += depositAmount;
}
the lines
account.withdraw(balance, 2500);
account.deposit(balance, 3000);
you might want to make withdraw and deposit non-static and let it modify the balance
public void withdraw(double withdrawAmount) {
balance = balance - withdrawAmount;
}
public void deposit(double depositAmount) {
balance = balance + depositAmount;
}
and remove the balance parameter from the call
You are trying to access non static field directly from static method which is not legal in java. balance is a non static field, so either access it using object reference or make it static.
The static calls to withdraw and deposit are your problem.
account.withdraw(balance, 2500);
This line can't work , since "balance" is an instance variable of Account. The code doesn't make much sense anyway, wouldn't withdraw/deposit be encapsulated inside the Account object itself? so the withdraw should be more like
public void withdraw(double withdrawAmount)
{
balance -= withdrawAmount;
}
Of course depending on your problem you could do additional validation here to prevent negative balance etc.
Just write:
private static double balance = 0;
and you could also write those like that:
private static int id = 0;
private static double annualInterestRate = 0;
public static java.util.Date dateCreated;
To access instance variables it is a must to create an object, these are not available in the memory, before instantiation.
Therefore, you cannot make static reference to non-static fields(variables) in Java. If you still, try to do so a compile time error is generated saying “non-static variable math cannot be referenced from a static context”.
you can keep your withdraw and deposit methods static if you want however you'd have to write it like the code below.
sb = starting balance and eB = ending balance.
Account account = new Account(1122, 20000, 4.5);
double sB = Account.withdraw(account.getBalance(), 2500);
double eB = Account.deposit(sB, 3000);
System.out.println("Balance is " + eB);
System.out.println("Monthly interest is " + (account.getAnnualInterestRate()/12));
account.setDateCreated(new Date());
System.out.println("The account was created " + account.getDateCreated());

Im trying to get my FlexibleSavings Account and CDSavingsAccount to add interest each month and display it

My flexiblesavingsaccount is supposed to add 1.2% interest each year and it compounds monthly. CD Savings account on the other hand adds interest at 4% per year and compounds quarterly. I've managed to get the program to add interest to the balance the first month, but after that it just stays the same. and on month zero, it says the balance is 0 when it needs to be 2000 for flexiblesavingsaccount and 3000 for CDSavingsaccount. Once the program goes through 12 months it is supposed to add up the balance of the two accounts. that part works i believe, it is just that the balance I get right now it wrong
Here is my driver class:
import java.util.*;
public class SavingsAccountDriver1{
public static void main(String[] args){
FlexibleSavingsAccount1 saver1 = new FlexibleSavingsAccount1(10002,2000); //could be FlexibleSavingsAccount saver1
CDSavingsAccount1 saver2 = new CDSavingsAccount1(10003,3000);//CD SavingsAccount saver 2
saver1.annualInterestRate=.012;
saver2.annualInterestRate = .04;
int i;
System.out.println("Month\tAccount # Balance\tAccount # Balance");
System.out.println("-----\t--------- -------\t--------- -------");
System.out.print(" 0");
printFlex(saver1);
printCD(saver2);
System.out.println();
for(i=1;i<=12;i++)
{System.out.printf("%5d",i);
printIntFlex(saver1);
printIntCD(saver2);
System.out.println();
}
System.out.printf("Final balance of both accounts combined: %.2f\n",(saver1.getMonthlyInterest()+saver2.getMonthlyInterest()));
}
public static void printIntFlex(FlexibleSavingsAccount1 f){
getIntFlex(f);
printFlex(f);
}
public static void printIntCD(CDSavingsAccount1 c){
getIntCD(c);
printCD(c);
}
public static void printFlex(FlexibleSavingsAccount1 f){
System.out.printf("%12d%10.2f ",f.getAccount_Number(),f.getMonthlyInterest());
}
public static void printCD(CDSavingsAccount1 c){
System.out.printf("%12d%10.2f ",c.getAccount_Number(),c.getMonthlyInterest());
}
public static void getIntFlex(FlexibleSavingsAccount1 f){
f.addMonthlyInterest(f.getBalance());
}
public static void getIntCD(CDSavingsAccount1 c){
c.addMonthlyInterest(c.getBalance());
}
}
here is my SavingsAccount superclass:
public abstract class SavingsAccount1
{
private double balance;
private final int ACCOUNT_NUMBER;
public SavingsAccount1(){
this.balance = 0.0;
this.ACCOUNT_NUMBER =0;
}
public SavingsAccount1(int ACCOUNT_NUMBER, double balance)
{
this.balance = balance;
this.ACCOUNT_NUMBER = ACCOUNT_NUMBER;
}
public abstract void addMonthlyInterest(double balance);
public double getBalance()
{ return balance;
}
public void setBalance(double balance){
this.balance = balance;
}
public int getAccount_Number()
{
return ACCOUNT_NUMBER;
}
public String toString(){
return String.format("%s", getAccount_Number());
}//end of toString
}
Here is my FlexibleSavingsAccount subsclass:
public class FlexibleSavingsAccount1 extends SavingsAccount1{
public static double annualInterestRate;
public static double b;
public FlexibleSavingsAccount1 (int ACCOUNT_NUMBER, double balance){
super(ACCOUNT_NUMBER, balance);
}//end of
#Override public void addMonthlyInterest(double balance){
b =balance +(balance * (annualInterestRate / 12));
this.b = b;
}
public double getMonthlyInterest(){
return b;
}//end of getMonthlyInterest
public String toString(){
return String.format("%s %s", super.toString(), getMonthlyInterest());
}//end of toString
}//end of FlexibleSavings Account
Here is my CDSavingsAccount subclass:
public class CDSavingsAccount1 extends SavingsAccount1{
public static double annualInterestRate;
public static double b;
public CDSavingsAccount1 (int ACCOUNT_NUMBER, double balance){
super(ACCOUNT_NUMBER, balance);
}//end of
#Override public void addMonthlyInterest(double balance){
b =balance +(balance * (annualInterestRate / 4));
this.b = b;
}
public double getMonthlyInterest(){
return b;
}//end of getMonthlyInterest
public String toString(){
return String.format("%s %s", super.toString(), getMonthlyInterest());
}//end of toString
}//end of CDSavings Account
Sorry for all the code, I've never messed with polymorphism or inheritance before and my teacher is really bad. I assume that is where my issue is. Any help you could give would be awesome.
InSavingsAccount1 change the constructor to allow an annualInterestRate parameter to be given. Create a getter for it and it's done.
Regarding theFlexibleSavingsAccount1class (Due to the similarities with theCDSavingsAccount1class, everything I will mention applies to both classes):
When you extend a class you inherit all theattributes and methodsof the parent class so there is no need to defineannualInterestRateattribute in the child class. You see, this is precisely why we use inheritance. 1- is so we don't repite ourselves, and keep typing the same code more than once. 2- it allows us to force a common signature/behaviour. A class that extends from an abstract class must either provide an implementation to the abstract methods or must itself be abstract, adding one more layer of abstraction.
This variable b was supossely to hold the value of the monthlyInterest but you seem never to actually use it in your design.
ps: If you were to keep it, you would have to rename it to something more meaningful then justb.
So, you should eliminate both of the attributes because you never use them.
In theaddMonthlyInterestmethod you are calculating the value of the monthlyInterest and assigning to b and then you are assigning b to itself. This is obviously wrong, you need to change the actual balance of the account.
public void addMonthlyInterest(double balance) {
setBalance(balance + (balance * (annualInterestRate / 12)));
}
Pretty self-explanatory, I set a new value for the balance which is the result of the sum of the monthlyInterest with the current balance.
FlexibleSavingsAccount1 saver1 = new FlexibleSavingsAccount1(10002, 2000, .012);
CDSavingsAccount1 saver2 = new CDSavingsAccount1(10003, 3000, .04);
System.out.println("Month\tAccount # Balance\tAccount # Balance");
System.out.println("-----\t--------- -------\t--------- -------");
System.out.print(" 0");
print(saver1);
print(saver2);
System.out.println();
for (int i = 1; i <= 12; i++) {
System.out.printf("%5d", i);
printInt(saver1);
printInt(saver2);
System.out.println();
}
System.out.printf("Final balance of both accounts combined: %.2f\n",
(saver1.getBalance() + saver2.getBalance()));
}
Both FlexibleSavingsAccount1 and CDSavingsAccount1 are a type of SavingsAccount1 you and so you can eliminate several duplicated methods that you had by changing the method signature to accept a SavingsAccount1 as argument instead.
public static void printInt(SavingsAccount1 f) {
getInt(f);
print(f);
}
public static void print(SavingsAccount1 f) {
System.out.printf("%12d%10.2f ", f.getAccount_Number(),
f.getBalance());
}
public static void getInt(SavingsAccount1 f) {
f.addMonthlyInterest(f.getBalance());
}
The methodgetInt(SavingsAccoutn1 f) is a wonderful example what polymorphism is all about. You will now call this method using both child objects and both of them will addMonthlyInterest according to their own unique implementation defined in their classes. That is the definition of polymorphism: two related but different objects getting the same order but acting upon it differently.
You should rename this methods to something more explanatory.
Finally, a word about your class naming convention, namely the fact that all your classes end with a 1 attached, such as SavingsAccount1 to it's end makes me think that you not fully grasped the concept of a class. A class is a blueprint, it enables us to create many objects of the same type, as such it's name shouldn't seem to be "tied" to any particular instance/object.
NOTE: I agree with MarsAtomic in that you should stick with BigDecimal class for monetary transactions in order to not lose precision but as you state this is some kinda of assignment, I will take in assumption that the BigDecimal is beyond the scope of what your teacher taught you so I just used double.

Constructor and classes in Java

I have one class Account (abstract).
I have one class SavingsAccount (inherits Account).
I have one class CreditAccount (inherints Account).
SavingsAccount and CreditAccount are similar, except CreditAccount has a credit limit (variable limit).
I have the following problems:
I need help with how constructor in CreditAccount looks like. How can I add local variable limit to the constructor?
Is it better (easier) if I have a variable limit in base class instead, and let SavingsAccount Always set it to be zero, while CreditAccount let the user set the limit?
My teacher says since SavingsAccount and CreditAccount are almost the same, #Override are exactly the same, he recommends me to implement those methods in base class (Account). How will the code look like if I do that?
Thank you for your help
class Account:
public abstract class Account {
protected double balance = 0;
protected String accountId;
protected double interest = 0;
public Account() {}
public Account(double bal, String id, double inte) {
if (balance >= 0) {
balance = bal;
}
else {
balance = 0;
}
accountId = id;
interest = inte;
}
public abstract String getAccountId();
public abstract double getBalance();
public abstract double getInterest();
}
class SavingsAccount:
public class SavingsAccount extends Account {
public SavingsAccount() {
super();
}
public SavingsAccount(double bal, String id, double inte) {
super(bal, id, inte);
}
#Override
public String getAccountId() {
return accountId;
}
#Override
public double getInterest() {
return interest;
}
#Override
public double getBalance() {
return balance;
}
}
Class CreditAccount:
class CreditAccount extends Account {
private int limit;
public CreditAccount() {
super();
}
public CreditAccount(double bal, String id, double inte) { //How can I add local variable limit to this???
super(bal, id, inte);
}
public void setLimit(int limit) {
this.limit = limit;
}
#Override
public String getAccountId() {
return accountId;
}
#Override
public double getInterest(){
return interest;
}
#Override
public double getBalance() {
return balance;
}
}
in main:
System.out.print("Savings or Credit (S/C): ");
answer = input.next();
if (null != answer) switch (answer) {
case "S":
case "s":{
System.out.print("Amount to deposit: ");
double amount = input.nextDouble();
System.out.print("Interest: ");
double interest = input.nextDouble();
SavingsAccount savingsAccount = new SavingsAccount(amount, accountId, interest);
newCustomer.addAccount(savingsAccount);
bank.addCustomer(newCustomer);
break;
}
case "C":
case "c":{
System.out.print("Amount to deposit: ");
double amount = input.nextDouble();
System.out.println("Interest: ");
double interest = input.nextDouble();
CreditAccount creditAccount = new CreditAccount(amount, accountId, interest);
newCustomer.addAccount(creditAccount);
bank.addCustomer(newCustomer);
break;
}
default:
System.out.println("invalid answer");
break;
}
To answer your questions without giving your assignment away...
1) I need help with how constructor in CreditAccount looks like. How can I add local variable limit to the constructor?
I suppose you're confused about "having" to implement the constructor of the base. Well, that doesn't prevent you from providing one specific to the needs of the sub.
public CreditAccount(..., double limit) {
//call the super from this
this.limit = limit;
}
Is it better (easier) if I have a variable limit in base class instead, and let SavingsAccount Always set it to be zero, while CreditAccount let the user set the limit?
Better; nope. Easier; that's subjective. Is it easier to set an untold number of sub class' needless variable easier than not including it in the first place?
Easier isn't a factor that overrides good design. The whole point of all this is providing nice little data holders void of clutter. On top of that, it just doesn't seem right; does it; what does your gut say?
My teacher says since SavingsAccount and CreditAccount are almost the same, #Override are exactly the same, he recommends me to implement those methods in base class (Account). How will the code look like if I do that?
As opposed to the abstract class leaving the methods unimplemented, implement them. Put the simple getters inside the abstract class and remove them from the subs. The resulting code would look something like...
public CreditAccount ... {
protected double limit;
//those constructors you need
public double getLimit() { ... }
}
...for starters.

program cant start, methods withdraw / deposit

im new to methods and classes.
I coded a withdraw/deposit program which ask for name and balance, allowing withdraw and deposit functions. eclipse doesn't allow me to run the program, why is that so?? am I suppose to create (public static void main (string[] args) on a separate class?? and if I have to, what methods(get / set) stay on this class or get transferred to main class?
import java.util.Scanner;
public class bank {
private String name;
private double balance;
private double withdraw;
private double deposit;
private double withdrawTotal;
private double depositTotal;
bank()
{
name = null;
balance = 0;
withdraw = 0;
deposit = 0;
withdrawTotal = 0;
depositTotal = 0;
}
public String getname() {
return name;
}
public double getBalance(){
return balance;
}
//public double getAnnualInterestRate(){
//return annualInterestRate;
//}
public double getWithdraw() {
return withdraw;
}
public double getDeposit() {
return deposit;
}
public double getWithdrawTotal() {
return withdrawTotal;
}
public double getDepositTotal() {
return depositTotal;
}
public void setname(String newname){
Scanner namescan = new Scanner(System.in);
name = namescan.nextLine();
}
public void setBalance(double newBalance){
Scanner bscan = new Scanner(System.in);
balance = bscan.nextInt();
}
public void setWithdraw(double newWithdraw){
withdraw = newWithdraw;
}
public void setDeposit(double newDeposit){
deposit = newDeposit;
}
public void setWithdrawTotal(double newWithdrawTotal){
deposit = newWithdrawTotal;
}
public void setDepositTotal(double newDepositTotal){
deposit = newDepositTotal;
}
//calculate method
public double withdrawCalculuation() {
return balance - withdraw;
}
public double depositCalculation(){
return balance + deposit;
}
//public double annualInterestRateCalculation(){
// return depositTotal * .045;
//}
public void print() {
bank account = new bank();
account.setname(name);
account.setBalance(20000.00);
account.setWithdraw(2500);
account.setWithdrawTotal(17500.00);
account.setDeposit(3000.00);
account.setDepositTotal(20500.00);
//account.setAnnualInterestRate(.045);
System.out.println("The Accound name is:" + account.getname());
System.out.println("The Balance is:" + account.getBalance());
System.out.println("Amount of withdrawal is:" + account.getWithdraw());
System.out.println("The Balance after withdrawal is:" + account.getWithdrawTotal());
System.out.println("Amount of deposit is:" + account.getDeposit());
System.out.println("The Balance after depsoit is:" + account.getDepositTotal());
//System.out.println("The monthly interest for total amount is:" + account.getAnnualInterestRate());
}
}
eclipse doesn't allow me to run the program, why is that so??
Because JVM is not able to find the main() method.
am I suppose to create (public static void main (string[] args) on a separate class??
No. You can add your main() in same class.
Please start with a Basic Java tutorial. You need to have a main method to execute a java program.

Method Retrieval and Inheritance Confusion

Ok,so I am getting a lot of trouble, I am still learning Java and my book has set me a task that I find common over the net, the part that I am stuck on is...
I must create a bank account program, an account holder is given a savings account (which has an interest rate and no overdraft facility), and a checking account (which has an overfraft facility of £100 and no interest).
I am not implementing the overdraft yet and am only half way to getting the withdraw and deposit function ready but my question is with the interest, I have defined in my superclass the savings account balance and the checking account balance so when working out my interest in the savings account class I cannot reference savebalance as I have made it private. I am trying to use the set.name method but i am clearly doing it wrong....
A big smile and a thank you for any one who can help or give advice!
Superclass is as follows:
public class BankDetails
{
private String customer;
private String accountno;
private double savebalance;
private double checkbalance;
//Constructor Methods
public BankDetails(String customerIn, String accountnoIn, double savebalanceIn, double checkbalanceIn)
{
customer = customerIn;
accountno = accountnoIn;
savebalance = savebalanceIn;
checkbalance = checkbalanceIn;
}
// Get name
public String getcustomername()
{
return (customer);
}
// Get account number
public String getaccountnumber()
{
return (accountno);
}
public double getcheckbalanceamount()
{
return (checkbalance);
}
public double getsavebalanceamount()
{
return (savebalance);
}
public void savewithdraw(double savewithdrawAmountIn)
{
savebalance = savebalance - savewithdrawAmountIn;
}
public void checkwithdraw(double checkwithdrawAmountIn)
{
checkbalance = checkbalance - checkwithdrawAmountIn;
}
public void savedeposit(double savedepositAmountIn)
{
savebalance = savebalance - savedepositAmountIn;
}
public void checkdeposit(double checkdepositAmountIn)
{
checkbalance = checkbalance - checkdepositAmountIn;
}
} // End Class BankDetails
Sub Class is as follows:
import java.util.*;
public class Savings extends BankDetails
{
private String saveaccount;
private double interest;
public Savings(String customerIn, String accountnoIn, float interestIn,
String saveaccountIn, double savebalanceIn)
{
super (customerIn, accountnoIn, savebalanceIn, interestIn);
saveaccount = saveaccountIn;
interest = interestIn;
}
public String getsaveaccountno()
{
return (saveaccount);
}
public double getinterestamount()
{
return (interest);
}
public void interestamount(String[] args)
{
BankDetails.getsavebalanceamount(savebalance);
interest = (savebalance / 100) * 1.75;
}
}
Use the superclass's getSaveBalance() method to access the balance (which is suspiciously-named, since you have a savings account class, but keep the balance elsewhere).
(Currently it's getsavebalanceamount(), I assume a renaming to keep with Java conventions.)
I'd recommend using consistent CamelCase when naming your getters and setters, e.g., getInterestAmount(), getSaveAccountNo(), etc.
I recommend against commenting simple getters/setters, but if you do, use Javadoc conventions, e.g.:
/** Returns current savings account balance. */
public double getSaveBalance() { ... etc ... }
I also recommend avoid unnecessary parentheses, as currently in your getters, e.g.:
public double getSaveBalance() {
return saveBalance; // No parens required.
}
I suggest you do something like this,
interface Account{
int getAccountNumber();
float getBalance();
}
public class SavingAccount implements Account, Interest{
int accountNumber;
public int getAccountNumber(){
return accountNumber;
}
float balance;
public float getBalance(){
return balance;
}
float savingInterestRate;
public float getInterestRate(){
return savingInterestRate;
}
}
public class CheckingAccount implements Account, OverDraft{
int accountNumber;
public int getAccountNumber(){
return accountNumber;
}
float balance;
public float getBalance(){
return balance;
}
}
interface Interest{
float getInterestRate();
}
interface OverDraft{
....
}

Categories

Resources