Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inheritance Question(desperate for help)

Status
Not open for further replies.

javahopeful321

Programmer
Feb 7, 2003
2
US
Need someone to point me in the right direction. I'm trying to do simple inheritance with a menu interface. I don't really understand what I'm doing wrong? My code does not compile correctly.

Here is the code
Code:
public class Account extends Object
{

    // Fields
    private String customerName;
    private String acctID;
    private double balance;


   // Three-parameter constructor
   	 public Account(String name, String id, double bal)
    	{
		setFields( name, id, bal );
	}

	
   	 public void setFields( String s, String ss, double b )
   	 {
		setCustomerName( s );
		setAccountID( ss );
		setBalance( b );
	}


    	public void setCustomerName(String s)
    	{
		customerName = s;
	}


	public void setAccountID(String s)
	{
		acctID = s;
	}


   	public void setBalance(double bal)
	{
		balance = bal;
   	 }


    	public String getCustomerName()
    	{
        return customerName;
    	}
	
    	public String getAccountID()
    	{
        return acctID;
    	}

   	 public double getBalance()
    	{
        return balance;
    	}

   	 public void printBalance()
    	{ 
	System.out.println("\nBalance: " + balance ); 	
	}

   	 public String toString()
   	 {
	  return "\nCustomer: " + customerName + "  AcctID: " + acctID + "  Balance: " + balance;
  	 }


    public void withdraw(double balance)
    {
	balance=balance-balance;
		 
    }


    public void deposit(double balance)
    {
		balance=balance +balance;
    }



    // Display a menu and read selection from console
    private int accountMenu()
    {
		String s =  "\n1 Deposit" +
					"\n2 Withdraw" +
					"\n3 Balance" +
					"\n4 Print toString" +
					"\n5 Quit\n\nSelect from menu:";

         return Console.readInt( s );
    }

    // manipulate the account
    public void manipulate()
    {
		int choice = accountMenu();
		while( choice != 5 )
		{
			switch( choice )  {

				case 1: deposit(balance);
				String prompt=Console.readString("Choose 1 for savings and 2 for checking");
					{if(prompt=="1")
						SavingsAccount.deposit(balance);
					else
						CheckAccount.deposit(balance);
					}
						
				break;

				case 2: withdraw(balance);
				String prompt=Console.readString("Choose 1 for savings and 2 for checking");
					{if(prompt=="1")
						SavingsAccount.withdraw(balance);
					else
						CheckAccount.withdraw(balance);
					}
				break;

				case 3: printBalance();
				break;

				case 4: System.out.println( toString() );
				break;

				case 5: return;

				default: System.out.println("Select from menu.");
				break;
			}
			choice = accountMenu();
		}
	}


} // END OF CLASS

public SavingsAccount extends Account
{

	private double intRate;
	private int interest;
	

	public SavingsAccount(String a,String b,int c)
	 {	
		super(a,b,c);
		
	 }
	
  
  	public void deposit(double balance)
	{
		double intDeposit=Console.readInt("Enter amount to deposit");	
		balance=balance+intDeposit;
		balance=balance+interest;
	}
	
  
  	public void addInterest()
  	{
	intRate=Console.readDouble("Enter interest rate:);
    	double interest = super.getBalance() * intRate/100.0;
   	 super.deposit(interest);
  	}



	
}//end of savings
class CheckAccount extends Account 
    { 
        
          private int transCount=0;
	  private static final int Free_Transactions=5;
	  private static final double Transaction_Fee=5.0;

	

	public CheckAccount(String a, String b, int c)
	{
		super(a,b,c);
	}

	public  void deposit(double amt)
	{
	  	transCount++;
		super.deposit(amt);
	}
	public void withdraw(double amt)
	{
		transCount++;
		super.withdraw(amt);
		deductFees();
	}
	
	public void deductFees()
	{
		if (transCount>Free_Transactions)
		   {
			double fees=Transaction_Fee*(transCount-Free_Transactions);
			super.withdraw(fees);
		   }

		   transCount=0;
	}
}//end of check account

 public class TestAccount
{
	public static void main( String[] args )
	{
		Account [] accts = new Account [ 6 ];

		accts[0] = new Account( "Smith", "SMI00189", 1000 );
		accts[1] = new Account( "Brooks", "BRO00524", 2000 );
		accts[2]=new SavingsAccount("Millner","MIL5679", 3000);
		accts[3]=new SavingsAccount("Millner","MIL5699", 6000);
		accts[4]=new CheckAccount("Webber","WEb9612", 3000);
		accts[5]=new CheckAccount("Bibby","Bib1489", 5000);

		for( int i = 0; i < accts.length; i++ )

			accts[i].manipulate();

	}

}	// end of class TestAccount

Any help will be greatly appreciated.

 
What would really help me is this bit of code
Code:
                case 1: deposit(balance);
                String prompt=Console.readString(&quot;Choose 1 for savings and 2 for checking&quot;);
                    {if(prompt==&quot;1&quot;)
                        SavingsAccount.deposit(balance);
                    else
                        CheckAccount.deposit(balance);
                    }
                        
                break;

                case 2: withdraw(balance);
                String prompt=Console.readString(&quot;Choose 1 for savings and 2 for checking&quot;);
                    {if(prompt==&quot;1&quot;)
                        SavingsAccount.withdraw(balance);
                    else
                        CheckAccount.withdraw(balance);
                    }
                break;

Where I'm trying to move the user response to either checking or savings. Whenever I do this i get the can't make static reference to void method errors. Then when I try to make my methods static i get even more errors about it being illegal to hide instance methods.
aargh!!!
 
It looks to me like you have classes here, but no objects. You need to instantiate a SavingsAccount object and a CheckingAccount object, like this:

CheckingAccount chkacc = new CheckingAccount(&quot;Name&quot;, &quot;ID&quot;,balance);

Then you can call the methods on that object...

chkacc.deposit(amount);

Hope this gets you moving again...
 
Hi javahopeful321:

Can you post the compiler errors yue are getting? Your main code doesn't seem to have any problems with static data as you create the objects and then call manipulate().

I have two additional tips:

Code:
    public void withdraw(double balance)
    {
    balance=balance-balance;
    }

This method called my attention because it will have the same effect to do: balance=0;

I will explain you why:
Any variables in Java have a scope, that could be global or local. If you declare a variable in the Class it would be visible in all methods, but if you declare it inside a method it would be visible just in that method. This way for while, for, try, etc.
But, if you have a global variable and then you declare another variable inside a method with the same name, the method will work with the local variable. So, in your withdraw method, you declare (as you are receiving the parameter) a local variable balance that is different from the global variable balance. So your line will put a 0 in the local variable and will left the global balance variable intact.

You can avoid this in two ways:

Declare the local variable with a different name:

Code:
    public void withdraw(double toWithDraw)
    {
    balance=balance-toWithDraw; //balance-=toWithDraw; is the same
    }

Or access the global variable explicitly by telling the compiler which class owns the variable, in this case &quot;this&quot;:

Code:
    public void withdraw(double balance)
    {
    this.balance=this.balance-balance;
    }

I hope I made myself clear.

The other tip is about this line:

Code:
      if(prompt==&quot;1&quot;)

AS prompt is an object of type String, you cannot compare it with another String using the operator ==, because it will compare the object itself, not the content of the object. It means that:

String one=&quot;text&quot;;
String two=&quot;text&quot;;

(one==two) -> false

because one and two are different objects, no matter its content. To compare two Strings you can use the equals method of the String class (in fact, this will work for any Object):

one.equals(two) -> true

In your case:
Code:
if(prompt.equals(&quot;1&quot;))

You must note that the line

&quot;1&quot;

its creating a new object of the class String, that is why it is a different object.

Please post the compiler errors to see what can be happening with the static data.

Hope it helps. Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top