Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A few problems with Inheritance

inheritance

  • Please log in to reply
5 replies to this topic

#1 brutetal

brutetal

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 22 May 2010 - 02:04 PM

For my java class, we're doing a project based on inheritance.

I can get my code to update the balanceFloat and display.
But it won't output the string objects. @.@

This is just a modified version of the full code.

Design class:
JTextArea transactionsTextArea = new JTextArea(10,10);
//Instance Variable
int accountInteger = 0;
//Array
//BankAccount("First Name", "Last Name", pinCodeInteger, balanceFloat, typeAccountInteger);
BankAccount[] accounts = new BankAccount[10];
public void intializeTextArea(){
//Example account
accounts[indexInteger] = new BankChecking("name","last", "0000", 100.0f, 1); 

		String accountTypeString = null;
		DecimalFormat currencyFormat = new DecimalFormat("$#0.00");
		transactionsTextArea.setText("");
		if(accounts[accountInteger].getAccountType() == 1){
			accountTypeString = "Checking";
		}else if(accounts[accountInteger].getAccountType() == 2){
			accountTypeString = "Savings";
		}
		transactionsTextArea.append("Account Type: " + accountTypeString + "   Balance: " + currencyFormat.format(accounts[accountInteger].getBalance()) + "\n");
		transactionsTextArea.append("First Name: " + accounts[accountInteger].getFirstName() + "Last Name:" + accounts[accountInteger].getLastName() + "Pin Code:" + accounts[accountInteger].getPinCode() + "\n");
		transactionsTextArea.append("----------------------------------------------------------\n");
		
		
	}
BankAccount class
public abstract class BankAccount {
	protected static String firstNameString, lastNameString, pinCodeString;
	protected int typeAccountInteger;
	protected static float balanceFloat;
	
	protected BankAccount(String firstNameString, String lastNameString, String pinCodeString,float balanceNewFloat, int typeNewInteger){
		setFirstName(firstNameString);
		setLastName(lastNameString);
		setAccountType(typeNewInteger);
		setPinCode(pinCodeString);
		setBalance(balanceNewFloat);
		
	}
	protected BankAccount(float balanceNewFloat){
		setBalance(balanceNewFloat);
	}
	protected void setFirstName(String firstNameString){
		this.firstNameString = firstNameString;
	}
	protected void setLastName(String lastNameString){
		this.lastNameString = lastNameString;
	}
	protected void setPinCode(String pinCodeString){
		this.pinCodeString = pinCodeString;
	}
	protected void setBalance(float balanceNewFloat){
		this.balanceFloat = balanceNewFloat;
	}
	protected void setAccountType(int typeNewInteger){
		this.typeAccountInteger = typeNewInteger;
	}
	protected String getFirstName(){
		return firstNameString;
	}
	protected String getLastName(){
		return lastNameString;
	}
	protected String getPinCode(){
		return pinCodeString;
	}
	protected float getBalance(){
		return balanceFloat;
	}
	protected int getAccountType(){
		return typeAccountInteger;
	}
}

BankChecking subClass

public class BankChecking extends BankAccount {
	
	public BankChecking(String firstNameString, String lastNameString,
			String pinCodeString, float balanceNewFloat, int typeAccountInteger) {
		super(firstNameString, lastNameString, pinCodeString, balanceNewFloat, typeAccountInteger);
		
	}
	public void withdraw(float balanceNewFloat){
		float feeFloat = 0.50f;
		if(balanceNewFloat > 750.0f){
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat - feeFloat;
		}else{
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat;
		}
	}
	public void deposit(float balanceNewFloat){
		BankAccount.balanceFloat = BankAccount.balanceFloat + balanceNewFloat;
	}
	public String getFirstName(){
		return firstNameString;
	}
	public String getLastName(){
		return lastNameString;
	}
	public String getPinCode(){
		return pinCodeString;
	}
	public float getBalance(){
		return balanceFloat;
	}
	public int getAccountType(){
		return typeAccountInteger;
	}

}

BankSavings subClass
public class BankSaving extends BankAccount {

	public BankSaving(String firstNameString, String lastNameString,
			String pinCodeString, float balanceNewFloat, int typeAccountInteger) {
		super(firstNameString, lastNameString, pinCodeString, balanceNewFloat, typeAccountInteger);
		
	}
	public void withdraw(float balanceNewFloat){
		float feeFloat = 0.75f;
		if(balanceNewFloat > 2000.0f){
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat - feeFloat;
		}else{
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat;
		}
	}
	public void deposit(float balanceNewFloat){
		BankAccount.balanceFloat = BankAccount.balanceFloat + balanceNewFloat;
	}
	public String getPinCode(){
		return pinCodeString;
	}

}


Edited by brutetal, 22 May 2010 - 04:09 PM.

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 23 May 2010 - 01:15 AM

ehm...what exactly isn't showing? If i run the code it shows:

Account Type: Checking Balance: $100,00
First Name: nameLast Name:lastPin Code:0000
----------------------------------------------------------

Which seems correct..?
(i did change the following line:
accounts[indexInteger] = new BankChecking("name","last", "0000", 100.0f, 1);
into
accounts[accountInteger] = new BankChecking("name","last", "0000", 100.0f, 1);
but i suppose indexInteger is declared somehwere else.)

Anyway, about inheritance:
If a method exists in the parent class, it isn't required to put it in the chid class aswell. If you call getFirstName() From a BankChecking object it will automatically use the method from the BankAccount class if BankChecking doesn't have that method.
So all the red methods aren't really usefull here (suggested action: Delete):

BankChecking
public class BankChecking extends BankAccount {
	
	public BankChecking(String firstNameString, String lastNameString,
			String pinCodeString, float balanceNewFloat, int typeAccountInteger) {
		super(firstNameString, lastNameString, pinCodeString, balanceNewFloat, typeAccountInteger);
		
	}
	public void withdraw(float balanceNewFloat){
		float feeFloat = 0.50f;
		if(balanceNewFloat > 750.0f){
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat - feeFloat;
		}else{
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat;
		}
	}
	public void deposit(float balanceNewFloat){
		BankAccount.balanceFloat = BankAccount.balanceFloat + balanceNewFloat;
	}
	[COLOR="red"][B]public String getFirstName(){
		return firstNameString;
	}
	public String getLastName(){
		return lastNameString;
	}
	public String getPinCode(){
		return pinCodeString;
	}
	public float getBalance(){
		return balanceFloat;
	}
	public int getAccountType(){
		return typeAccountInteger;
	}[/B][/COLOR]

}

BankSavings
public class BankSaving extends BankAccount {

	public BankSaving(String firstNameString, String lastNameString,
			String pinCodeString, float balanceNewFloat, int typeAccountInteger) {
		super(firstNameString, lastNameString, pinCodeString, balanceNewFloat, typeAccountInteger);
		
	}
	public void withdraw(float balanceNewFloat){
		float feeFloat = 0.75f;
		if(balanceNewFloat > 2000.0f){
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat - feeFloat;
		}else{
			BankAccount.balanceFloat =  BankAccount.balanceFloat - balanceNewFloat;
		}
	}
	public void deposit(float balanceNewFloat){
		BankAccount.balanceFloat = BankAccount.balanceFloat + balanceNewFloat;
[COLOR="red"][B]	}
	public String getPinCode(){
		return pinCodeString;
	}[/B][/COLOR]

}

Also, if both child classes have the same withdraw and deposit methods, it's better to put that method at the BankAccount class, and remove it from BankChecking and BankSaving.



Oh yea, i didn't actually create a gui i just did System.out.println(transactionsTextArea.getText()); to see if it worked.
  • 0

#3 brutetal

brutetal

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 23 May 2010 - 01:46 AM

Thanks anyways. I found out what was wrong.
I called the method before creating the object

Yeah, I got rid of them. And changed int typeInteger to String typeString.

But I have another problem
It doesn't let me add more than one account into the array.
If the first name in the first key in the array is filled, then it just counts the numbers until it hits the end.

	public void registerAccount(){
		boolean foundBoolean = false;
		String firstNameString = firstNameTextField.getText(), lastNameString = lastNameTextField.getText(), pinCodeString = pinRegTextField.getText();
		int indexInteger = 0, selectedInteger = typeComboBox.getSelectedIndex();
		float balanceNewFloat = 0.0f;
		while(foundBoolean == false && indexInteger < accounts.length){
			if(accounts[indexInteger].getFirstName().equals("")){
				foundBoolean = true;
				this.accountInteger = indexInteger;
				JOptionPane.showMessageDialog(null, "Found User.  " + accountInteger);
			}else{
				indexInteger++;
                                JOptionPane.showMessageDialog(null, "Count " + indexInteger);
			}
		}
		if(foundBoolean){
			if(selectedInteger == 0){
				balanceNewFloat = 100.0f;
				accounts[indexInteger] = new BankChecking(firstNameString, lastNameString, pinCodeString, balanceNewFloat, "Checking");
			}else if(selectedInteger == 1){
				balanceNewFloat = 500.0f;
				accounts[indexInteger] = new BankSaving(firstNameString, lastNameString, pinCodeString, balanceNewFloat, "Savings");
			}
		}
	}

So what do I need to change for the condition in the IF statement?
I've tried other things but still the same result.
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 23 May 2010 - 01:53 AM

Unless you allready filled the array with empty bankAccounts try this:
if(accounts[indexInteger]==null)
  • 0

#5 brutetal

brutetal

    CC Lurker

  • Just Joined
  • Pip
  • 6 posts

Posted 23 May 2010 - 01:57 AM

Yeah, I already filled the array with empty accounts but I'll try it that way.

Edit:
Okay, I left the array empty and tried your advice.
It works just the way I need it to.
Thanks very much, I was stuck on that for a few hours.
  • 0

#6 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 23 May 2010 - 02:03 AM

You're welcome, it's better anyway with less memory consumption ;)
  • 0





Also tagged with one or more of these keywords: inheritance