Overriding Methods

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 5224 Views - Last Post: 09 July 2011 - 09:59 PM Rate Topic: -----

#1 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 06-July 11

Overriding Methods

Posted 06 July 2011 - 09:43 PM

I'm having some trouble trying to finish this homework assignment. I'm supposed to ask the user for an account type, saving accounts can not be overdrawn and checkings accounts have a -500 overdraft limit. I already have the mainframe of the code, I'm having trouble overriding the withdraw method in the account class.

Test program
import javax.swing.JOptionPane;

public class testAccount {
	public static void main(String[] args) {
        boolean done = true;
        Account account = null;
        Account account2 = null;
        String accountTypeString = JOptionPane.showInputDialog(null, "           Choose an account type: \n" + "\n" + "1- Saving Account \n" + "\n" +
        		"2- Checking Accoun \n" + "\n");
        int accountType = Integer.parseInt(accountTypeString);
        
        if(accountType == 1 || accountType == 2){
        	Account oneAccount = new Account(1, 100, .045);
        	account = oneAccount;
        	Account twoAccount = new Account(2, 100, .045);
        	account2 = twoAccount;
        }
        else if(accountType < 1 || accountType > 2){
        	JOptionPane.showMessageDialog(null, "Invalid input");
        }
        
        if(accountType == 1)
        	while(done){
        		String savingsAccountActionsString = JOptionPane.showInputDialog( null, "           Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" + 
        				"2- Deposit \n" + "\n");
        		int savingsAccountActions = Integer.parseInt(savingsAccountActionsString);
        		if(savingsAccountActions == 1){
        			String savingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
        			double savingsWithdrawAmount = Double.parseDouble(savingsWithdrawAmountString);
        			if(savingsWithdrawAmount > account.getBalance()){
        				JOptionPane.showMessageDialog(null, "Insufficient funds");
        			}
        			else {
        				account.withdraw(savingsWithdrawAmount);
        				JOptionPane.showMessageDialog(null, "The balance is " + account.getBalance());
        				done = false;
        			}
        		}
        		
        }
        
        if(accountType == 2)
        	while(done){
        		String checkingsAccountActionsString = JOptionPane.showInputDialog( null, "           Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" + 
        				"2- Deposit \n" + "\n");
        		int checkingsAccountActions = Integer.parseInt(checkingsAccountActionsString);
        		if(checkingsAccountActions == 1){
        			String checkingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
        			double checkingsWithdrawAmount = Double.parseDouble(checkingsWithdrawAmountString);
        			if(checkingsWithdrawAmount > account2.getBalance()){
        				JOptionPane.showMessageDialog(null, "Insufficient funds");
        			}
        			else {
        				account2.withdraw(checkingsWithdrawAmount);
        				JOptionPane.showMessageDialog(null, "The balance is " + account2.getBalance());
        				done = false;
        			}
        		}
        		
        }
	}
}



Account class
I'm supposed to override the withdraw method so I can do the overdraft in the checkings account.
import java.util.Date;

//Create the class and define the variables needed
public class Account {
	private int id;
	protected static double balance;
	private double annualInterestRate;
	private Date dateCreated;

	//Constructor with no arguments that creates a default account
	Account(){
		id = 0;
		balance = 0;
		annualInterestRate = 0;
		dateCreated = new Date();
	}
	
	//A constructor that takes in parameters and does a couple of operations.
	Account(int newId, double newBalance, double newAnnualInterestRate) {
		id = newId;
		balance = newBalance;
		annualInterestRate = newAnnualInterestRate;
		dateCreated = new Date();
	}
	//All the accesors used for the different variables
	public int getId() {
		return id;
	}
	public double getBalance() {
		return balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public Date getDateCreated() {
		return dateCreated;
	}
	
	//All the mutators used for the different variables
	public void setNewDate(Date newDate) {
		dateCreated = newDate;
	}
	public void setId(int newId) {
		id = newId;
	}
	public void setBalance(double newBalance) {
		balance = newBalance;
	}
	public void setAnnualInterestRate(double newAnnualInterestRate){
		annualInterestRate = newAnnualInterestRate;
	}
	
	//Method used to return the monthly rate and the monthly balance accordingly
	public static double getMonthlyInterestRate(double annualInterestRate) {
		double monthlyRate = 0;
		monthlyRate = annualInterestRate / 12;
		monthlyRate = monthlyRate * balance;
		return monthlyRate;
	}
	
	//Method used to withdraw money
	public void withdraw(double wd) {
		balance = balance - wd;
	}
	//Method used to deposit money
	public void deposit(double dp) {
		balance = balance + dp;
	}
}



Savings account class
I'm not sure why we there is a savings account as it does the same thing as the withdraw method.
//import javax.swing.JOptionPane;

public class savingsAccount extends Account {
	
	public void withdraw(double wd) {
		balance = balance + wd;
	}
}



Checkings accounts class
It's supposed to allow the user to withdraw more than their balance with the limit being -500
public class checkingAccounts extends Account {
	
	public checkingAccounts(){
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Overriding Methods

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Overriding Methods

Posted 06 July 2011 - 09:57 PM

It is NOT in the Account class that you overload but in the SavingAccount class

in you SavingAccount class your withdraw() method that overloads the Account withdraw() method should do


public void withdraw(double wd) {
        // in saving account I cannot be -0
        if(wd > balance)
           return;   // sorry transactionb refused
        // ok I can call my father to do the transaction
        super.withdraw(wd);
    }



and in your CheckIng account class


public void withdraw(double wd) {
        // in saving account I cannot be -0
        if(wd  + 500 > balance)
           return;   // sorry transactionb refused
        // ok I can call my father to do the transaction
        super.withdraw(wd);
    }


Happy coding
Was This Post Helpful? 1
  • +
  • -

#3 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 06-July 11

Re: Overriding Methods

Posted 06 July 2011 - 10:45 PM

Okay so I did the changes but when I try to make the checkings account go negative I still get insufficient funds, and I'm supposed to be able to withdraw until the checkings account hits -500

This post has been edited by shango11: 06 July 2011 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 06-July 11

Re: Overriding Methods

Posted 07 July 2011 - 03:08 AM

I finally have everything working, but I'm not sure if I'm using the savingsAccount class or the checkingsAccount class. Is there any way to check and see if they are being used?

Here is the what I have so far

Test program
import javax.swing.JOptionPane;

public class testAccount {
	public static void main(String[] args) {
        boolean done = true;
        Account account = null;
        Account account2 = null;
        String accountTypeString = JOptionPane.showInputDialog(null, "           Choose an account type: \n" + "\n" + "1- Saving Account \n" + "\n" +
        		"2- Checking Account \n" + "\n");
        int accountType = Integer.parseInt(accountTypeString);
        
        if(accountType == 1 || accountType == 2){
        	Account oneAccount = new Account(1, 100, .045);
        	account = oneAccount;
        	Account twoAccount = new Account(2, 100, .045);
        	account2 = twoAccount;
        }
        else if(accountType < 1 || accountType > 2){
        	JOptionPane.showMessageDialog(null, "Invalid input");
        }
        
        if(accountType == 1)
        	while(done){
        		String savingsAccountActionsString = JOptionPane.showInputDialog( null, "           Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" + 
        				"2- Deposit \n" + "\n");
        		int savingsAccountActions = Integer.parseInt(savingsAccountActionsString);
        		if(savingsAccountActions == 1){
        			String savingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
        			double savingsWithdrawAmount = Double.parseDouble(savingsWithdrawAmountString);
        			if(savingsWithdrawAmount > account.getBalance()){
        				JOptionPane.showMessageDialog(null, "Insufficient funds");
        			}
        			else {
        				account.withdraw(savingsWithdrawAmount);
        				JOptionPane.showMessageDialog(null, "The balance is " + account.getBalance());
        				done = false;
        			}
        		}
        		
        }
        
        if(accountType == 2)
        	while(done){
        		String checkingsAccountActionsString = JOptionPane.showInputDialog( null, "           Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" + 
        				"2- Deposit \n" + "\n");
        		int checkingsAccountActions = Integer.parseInt(checkingsAccountActionsString);
        		if(checkingsAccountActions == 1){
        			String checkingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
        			double checkingsWithdrawAmount = Double.parseDouble(checkingsWithdrawAmountString);
        			account2.withdraw(checkingsWithdrawAmount);
        			if(account2.getBalance() < -500){
        				JOptionPane.showMessageDialog(null, "Insufficient funds");
        			}
        			else {
        				JOptionPane.showMessageDialog(null, "The balance is " + account2.getBalance());
        				done = false;
        			}
        		}
        	}	
        }
	}



Account class
import java.util.Date;

//Create the class and define the variables needed
public class Account {
	private int id;
	protected static double balance;
	private double annualInterestRate;
	private Date dateCreated;

	//Constructor with no arguments that creates a default account
	Account(){
		id = 0;
		balance = 0;
		annualInterestRate = 0;
		dateCreated = new Date();
	}
	
	//A constructor that takes in parameters and does a couple of operations.
	Account(int newId, double newBalance, double newAnnualInterestRate) {
		id = newId;
		balance = newBalance;
		annualInterestRate = newAnnualInterestRate;
		dateCreated = new Date();
	}
	//All the accesors used for the different variables
	public int getId() {
		return id;
	}
	public double getBalance() {
		return balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public Date getDateCreated() {
		return dateCreated;
	}
	
	//All the mutators used for the different variables
	public void setNewDate(Date newDate) {
		dateCreated = newDate;
	}
	public void setId(int newId) {
		id = newId;
	}
	public void setBalance(double newBalance) {
		balance = newBalance;
	}
	public void setAnnualInterestRate(double newAnnualInterestRate){
		annualInterestRate = newAnnualInterestRate;
	}
	
	//Method used to return the monthly rate and the monthly balance accordingly
	public static double getMonthlyInterestRate(double annualInterestRate) {
		double monthlyRate = 0;
		monthlyRate = annualInterestRate / 12;
		monthlyRate = monthlyRate * balance;
		return monthlyRate;
	}
	
	//Method used to withdraw money
	public void withdraw(double wd) {
		balance = balance - wd;
	}
	//Method used to deposit money
	public void deposit(double dp) {
		balance = balance + dp;
	}
}



savingsAccount class
public class savingsAccount extends Account {
	
	public void withdraw(double wd) {
		if(wd > balance)
			return;
		super.withdraw(wd);
	}
}



checkingsAccount class
public class checkingAccounts extends Account {
	
	public void withdraw(double wd){
		if(wd > 600)
			return;
		super.withdraw(wd);
		
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Overriding Methods

Posted 07 July 2011 - 04:01 AM

Should be more like
import javax.swing.JOptionPane;

public class testAccount {
	public static void main(String[] args) {
        boolean done = true;
        Account account = null;
///        Account account2 = null;
        String accountTypeString = JOptionPane.showInputDialog(null, "           Choose an account type: \n" + "\n" + "1- Saving Account \n" + "\n" +
        		"2- Checking Account \n" + "\n");
        int accountType = Integer.parseInt(accountTypeString);
        
        if(accountType == 1) {
        	account = new SavingAccount(1, 100, .045);
        }
        else if(accountType == 2) {
        	account = new CheckingAccount(1, 100, .045);
        }
        else{
        	JOptionPane.showMessageDialog(null, "Invalid input");
        }
        
        if(accountType == 1 || accountType == 2)
...
//        the validation for the balance should be done by the class that you will call
//      account.withDraw() should do the job


Was This Post Helpful? 1
  • +
  • -

#6 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Overriding Methods

Posted 07 July 2011 - 04:20 AM

View Postshango11, on 07 July 2011 - 11:08 PM, said:

checkingsAccount class
public class checkingAccounts extends Account {
	
	public void withdraw(double wd){
		if(wd > 600)
			return;
		super.withdraw(wd);
		
	}
}



Shouldn't you be checking something like this (if the check account has a $500 OD?):
if(wd > balance + 500)


Otherwise your code will allow you to withdraw up to (but not including) $600, regardless of the balance... If I'm reading the code right, anyway...

Cheers

Steve
Was This Post Helpful? 1
  • +
  • -

#7 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 06-July 11

Re: Overriding Methods

Posted 07 July 2011 - 02:07 PM

View Postpbl, on 07 July 2011 - 04:01 AM, said:

Should be more like
import javax.swing.JOptionPane;

public class testAccount {
	public static void main(String[] args) {
        boolean done = true;
        Account account = null;
///        Account account2 = null;
        String accountTypeString = JOptionPane.showInputDialog(null, "           Choose an account type: \n" + "\n" + "1- Saving Account \n" + "\n" +
        		"2- Checking Account \n" + "\n");
        int accountType = Integer.parseInt(accountTypeString);
        
        if(accountType == 1) {
        	account = new SavingAccount(1, 100, .045);
        }
        else if(accountType == 2) {
        	account = new CheckingAccount(1, 100, .045);
        }
        else{
        	JOptionPane.showMessageDialog(null, "Invalid input");
        }
        
        if(accountType == 1 || accountType == 2)
...
//        the validation for the balance should be done by the class that you will call
//      account.withDraw() should do the job



I'm a bit confused about the validation of the input, would I need to do all the validation in the account class? If so would I need to pass the user input (amount to withdraw) to the class so I can do the validation?
Also wouldnt I need to change the withdraw method from void to something else so I can get the validation back?

This post has been edited by shango11: 07 July 2011 - 02:08 PM

Was This Post Helpful? 0
  • +
  • -

#8 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 06-July 11

Re: Overriding Methods

Posted 07 July 2011 - 02:23 PM

Okay so after some trial and error this is what I came up with, I still dont know if the overriding is working.

Test Program
import javax.swing.JOptionPane;

public class testAccount {
	public static void main(String[] args) {
        boolean done = true;
        Account account = null;
        //Account account2 = null;
        String accountTypeString = JOptionPane.showInputDialog(null, "           Choose an account type: \n" + "\n" + "1- Saving Account \n" + "\n" +
        		"2- Checking Account \n" + "\n");
        int accountType = Integer.parseInt(accountTypeString);
        
        if(accountType == 1){
        	account = new Account(1, 100, .045);
        }
        else if(accountType == 2){
        	account = new Account(1, 100, .45);
        }
		else{
        	JOptionPane.showMessageDialog(null, "Invalid input");
        }
        
        
        if(accountType == 1 || accountType == 2)
        	while(done){
        		String savingsAccountActionsString = JOptionPane.showInputDialog( null, "           Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" + 
        				"2- Deposit \n" + "\n");
        		int savingsAccountActions = Integer.parseInt(savingsAccountActionsString);
        		if(savingsAccountActions == 1){
        			String savingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
        			double savingsWithdrawAmount = Double.parseDouble(savingsWithdrawAmountString);
        			account.withdraw(savingsWithdrawAmount);
               			done = false;
        			}
        		}
        		
        }
	}



Account class
import java.util.Date;

import javax.swing.JOptionPane;

//Create the class and define the variables needed
public class Account {
	private int id;
	protected static double balance;
	private double annualInterestRate;
	private Date dateCreated;

	//Constructor with no arguments that creates a default account
	Account(){
		id = 0;
		balance = 0;
		annualInterestRate = 0;
		dateCreated = new Date();
	}
	
	//A constructor that takes in parameters and does a couple of operations.
	Account(int newId, double newBalance, double newAnnualInterestRate) {
		id = newId;
		balance = newBalance;
		annualInterestRate = newAnnualInterestRate;
		dateCreated = new Date();
	}
	//All the accesors used for the different variables
	public int getId() {
		return id;
	}
	public double getBalance() {
		return balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public Date getDateCreated() {
		return dateCreated;
	}
	
	//All the mutators used for the different variables
	public void setNewDate(Date newDate) {
		dateCreated = newDate;
	}
	public void setId(int newId) {
		id = newId;
	}
	public void setBalance(double newBalance) {
		balance = newBalance;
	}
	public void setAnnualInterestRate(double newAnnualInterestRate){
		annualInterestRate = newAnnualInterestRate;
	}
	
	//Method used to return the monthly rate and the monthly balance accordingly
	public static double getMonthlyInterestRate(double annualInterestRate) {
		double monthlyRate = 0;
		monthlyRate = annualInterestRate / 12;
		monthlyRate = monthlyRate * balance;
		return monthlyRate;
	}
	
	//Method used to withdraw money
	public void withdraw(double wd) {
		if(wd > balance){
			JOptionPane.showMessageDialog(null, "Insufficient funds");
		}
		else{
		balance = balance - wd;
		JOptionPane.showMessageDialog(null, "The balance is" + balance);
		}
	}
	//Method used to deposit money
	public void deposit(double dp) {
		balance = balance + dp;
	}
}



savingsAccount class
public class savingsAccount extends Account {
	
	public void withdraw(double wd) {
		if(wd > balance)
			return;
		super.withdraw(wd);
	}
}



checkingAccounts class
public class checkingAccounts extends Account {
	
	public void withdraw(double wd){
		if(wd > balance + 500)
			return;
		super.withdraw(wd);
		
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 06-July 11

Re: Overriding Methods

Posted 07 July 2011 - 04:06 PM

Update
I added some more functions/code that are part of the requirements of the assignment.
I added a second account, one for savings and the second one for checkings.
I added a deposit option for both account.
I also added toString() for each class.
The problems I'm having now is implementing the overriding and how to use/display the toString

Updated Code

Test class
import javax.swing.JOptionPane;

public class testAccount {
	public static void main(String[] args) {
        boolean done = true;
        Account account = null;
        Account account2 = null;
        String accountTypeString = JOptionPane.showInputDialog(null, "           Choose an account type: \n" + "\n" + "1- Saving Account \n" + "\n" +
        		"2- Checking Account \n" + "\n");
        int accountType = Integer.parseInt(accountTypeString);
        
        if(accountType == 1){
        	account = new Account(1, 100, .045);
        }
        else if(accountType == 2){
        	account2 = new Account(1, 100, .45);
        }
		else{
        	JOptionPane.showMessageDialog(null, "Invalid input");
        }
        
        
        if(accountType == 1)
        	while(done){
        		JOptionPane.showMessageDialog(null, "Your balance is: " + account.getBalance());
        		String savingsAccountActionsString = JOptionPane.showInputDialog( null, "           Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" + 
        				"2- Deposit \n" + "\n");
        		int savingsAccountActions = Integer.parseInt(savingsAccountActionsString);
        		if(savingsAccountActions == 1){
        			String savingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
        			double savingsWithdrawAmount = Double.parseDouble(savingsWithdrawAmountString);
        			account.withdraw(savingsWithdrawAmount);
        			done = false;
        		}
        		else if(savingsAccountActions == 2){
        			String depositAmountString = JOptionPane.showInputDialog(null, "Enter an amount to deposit: ");
        			double depositAmount = Double.parseDouble(depositAmountString);
        			account.deposit(depositAmount);
        			done = false;
        		}
        	}
        else if(accountType == 2)
        	while(done){
        		JOptionPane.showMessageDialog(null, "Your balance is: " + account2.getBalance());
        		String checkingsAccountActionString = JOptionPane.showInputDialog( null, "           Choose an option: \n" + "\n" + "1- Withdraw \n" + "\n" + 
        				"2- Deposit \n" + "\n");
        		int checkingsAccountAction = Integer.parseInt(checkingsAccountActionString);
        		if(checkingsAccountAction == 1){
        			String checkingsWithdrawAmountString = JOptionPane.showInputDialog(null, "Enter an amount to withdraw: ");
        			double checkingsWithdrawAmount = Double.parseDouble(checkingsWithdrawAmountString);
        			account2.withdraw(checkingsWithdrawAmount);
        			done = false;
        		}
        		else if(checkingsAccountAction == 2){
        			String checkingsDepositAmountString = JOptionPane.showInputDialog(null, "Enter an amount to deposit: ");
        			double checkingsDepositAmount = Double.parseDouble(checkingsDepositAmountString);
        			account2.deposit(checkingsDepositAmount);
        			done = false;
        		}
        	}
        else{
        	JOptionPane.showMessageDialog(null, "Invalid choice");
        }
 }
}



Account class
import java.util.Date;

import javax.swing.JOptionPane;

//Create the class and define the variables needed
public class Account {
	private int id;
	protected static double balance;
	private double annualInterestRate;
	private Date dateCreated;

	//Constructor with no arguments that creates a default account
	Account(){
		id = 0;
		balance = 0;
		annualInterestRate = 0;
		dateCreated = new Date();
	}
	
	//A constructor that takes in parameters and does a couple of operations.
	Account(int newId, double newBalance, double newAnnualInterestRate) {
		id = newId;
		balance = newBalance;
		annualInterestRate = newAnnualInterestRate;
		dateCreated = new Date();
	}
	//All the accesors used for the different variables
	public int getId() {
		return id;
	}
	public double getBalance() {
		return balance;
	}
	public double getAnnualInterestRate() {
		return annualInterestRate;
	}
	public Date getDateCreated() {
		return dateCreated;
	}
	
	//All the mutators used for the different variables
	public void setNewDate(Date newDate) {
		dateCreated = newDate;
	}
	public void setId(int newId) {
		id = newId;
	}
	public void setBalance(double newBalance) {
		balance = newBalance;
	}
	public void setAnnualInterestRate(double newAnnualInterestRate){
		annualInterestRate = newAnnualInterestRate;
	}
	
	//Method used to return the monthly rate and the monthly balance accordingly
	public static double getMonthlyInterestRate(double annualInterestRate) {
		double monthlyRate = 0;
		monthlyRate = annualInterestRate / 12;
		monthlyRate = monthlyRate * balance;
		return monthlyRate;
	}
	
	//Method used to withdraw money
	public void withdraw(double wd) {
		if(wd > balance){
			JOptionPane.showMessageDialog(null, "Insufficient funds");
		}
		else{
		balance = balance - wd;
		JOptionPane.showMessageDialog(null, "The balance is" + balance);
		}
	}
	//Method used to deposit money
	public void deposit(double dp) {
		if(dp < 0){
			JOptionPane.showMessageDialog(null, "Invalid amount");
		}
		else{
		balance = balance + dp;
		JOptionPane.showMessageDialog(null, "The balance is" + balance);
		}
	}
	
	public String toString(){
		return "Account";
	}
}



savingsAccount
public class savingsAccount extends Account {
	
	public void withdraw(double wd) {
		if(wd > balance)
			return;
		super.withdraw(wd);
	}
	
	public String toString(){
		return "Savings Account";
	}
}



checkingsAccount
public class checkingAccounts extends Account {
	
	public void withdraw(double wd){
		if(wd > balance + 500)
			return;
		super.withdraw(wd);	
	}
	
	public String toString(){
		return "Checkings Account";
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#10 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Overriding Methods

Posted 07 July 2011 - 06:21 PM

toString() is simply used as a method of the specified class. ie:

Account account = new SavingsAccount();
System.out.println(account.toString());


will output (as per your code above):

Savings Account


As an exercise, try commenting out your over-ridden toString method, and then try the System.out.println line from above. Now make a couple of accounts and print them both. What do you notice?

Cheers

Steve
Was This Post Helpful? 2
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10185
  • View blog
  • Posts: 37,603
  • Joined: 27-December 08

Re: Overriding Methods

Posted 07 July 2011 - 08:11 PM

Why is balance a static variable? All the Account objects share that variable. It doesn't make sense. Static variables are associated with the class, not the objects. It should be an instance variable.
Was This Post Helpful? 0
  • +
  • -

#12 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Overriding Methods

Posted 07 July 2011 - 08:34 PM

View Postmacosxnerd101, on 08 July 2011 - 04:11 PM, said:

Why is balance a static variable?


On top of that, there are a few other design issues with this code, but its functional... its probably a good idea to get it working as per the required spec, then look at the glaring problems with the way its written and improve its OO-yness. If you're interested in improving your code and programming skills that is.

Cheers

Steve
Was This Post Helpful? 0
  • +
  • -

#13 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 77
  • Joined: 06-July 11

Re: Overriding Methods

Posted 07 July 2011 - 08:50 PM

View Postkiwi_steve, on 07 July 2011 - 06:21 PM, said:

toString() is simply used as a method of the specified class. ie:

Account account = new SavingsAccount();
System.out.println(account.toString());


will output (as per your code above):

Savings Account


As an exercise, try commenting out your over-ridden toString method, and then try the System.out.println line from above. Now make a couple of accounts and print them both. What do you notice?

Cheers

Steve


WOW!!! That literally just blew my mind, I now got a better understanding of how the overriding works!
But wait, does that mean I need to do something along those lines for the withdraw method?
Was This Post Helpful? 1
  • +
  • -

#14 kiwi_steve  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 109
  • Joined: 26-September 09

Re: Overriding Methods

Posted 07 July 2011 - 09:14 PM

View Postshango11, on 08 July 2011 - 04:50 PM, said:

View Postkiwi_steve, on 07 July 2011 - 06:21 PM, said:

toString() is simply used as a method of the specified class. ie:

Account account = new SavingsAccount();
System.out.println(account.toString());


will output (as per your code above):

Savings Account


As an exercise, try commenting out your over-ridden toString method, and then try the System.out.println line from above. Now make a couple of accounts and print them both. What do you notice?

Cheers

Steve


WOW!!! That literally just blew my mind, I now got a better understanding of how the overriding works!
But wait, does that mean I need to do something along those lines for the withdraw method?


Its awesome when the lightbulbs come on :bananaman:

You already override the withdraw method, although not particularly elegantly. To explain a little: You're using something called inheritance when you create the Account class, then the savings and cheque 'subclasses'. These subclasses 'inherit' (hence the name) all the methods from the superclass (Account) - and this means any method that Account has can be used with any savings or a cheque object that you might create. You can override the ones that need to be specifically modified for a particular type of account (ie: the $500 OD for the cheque account).

On a more global scale, toString() is a method of the Object class, which is the granddaddy of all classes in Java - so all classes you create inherit it automatically. You generally override it to make it useful for the class (if its required).
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Overriding Methods

Posted 07 July 2011 - 09:19 PM

View Postshango11, on 07 July 2011 - 05:07 PM, said:

I'm a bit confused about the validation of the input, would I need to do all the validation in the account class? If so would I need to pass the user input (amount to withdraw) to the class so I can do the validation?
Also wouldnt I need to change the withdraw method from void to something else so I can get the validation back?

Yes the validation should be done in CheckingAccount and SavingAccount
If your main driver does the validation... no need to have 2 separate classes
And Yes you can modify withdraw() to be a boolean to return true or false depending if the transaction was accepted or refused
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2