Link to home
Start Free TrialLog in
Avatar of Mickeys
MickeysFlag for Sweden

asked on

Inheritance and Abstract

I got help from you guys in here for a couple of month ago and now I got my feedback from it

I have got this answer ofmy program:

Hmm ... I can not accept your inheritance hierarchy and your abstract class when much of it is completely useless. It seems entirely possible to completely delete all the methods in the "account" class, ie. no inheritance of these appear to be? In addition, you should use the abstract classes wherever possible.

What is wrong? I cant see it. Look at my code and please explain
Account.java
----------------------------------------------
package p3;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public abstract class Account 
{	
	//Declaration
	public UUID konto_namn;
	public String namn;
	public double balance;
	public static final int MAX_UNITS = 10;
	public List<Trans> trans = new ArrayList<Trans>();

	/**
	 * getbalance - Get balance
	 * @return balance 
	 */
	public double getbalance()
	{
		return balance;
	} //end getbalance

	
	/**
	 * setKontoNamn - Sätter Konto namnet
	 * 
	 * @param namn - UUID namnet
	 */
	public void setKontoNamn(UUID namn) 
	{} //end setKontoNamn

	/**
	 * deposit - Put in money
	 * @param amount - How much
	 */
	public void deposit( double amount ) 
	{} //end deposit


	/**
	 * withdraw - Take out money
	 * @param amount - how much
	 * @return 0
	 */
	public double withdraw( double amount ) 
	{
		return 0;
	} //end withdraw
}


----------------------------------------------
SavingsAccount.java
----------------------------------------------
package p3;

import java.util.List;
import java.util.UUID;


public class SavingsAccount extends Account{

	//Declaration
	private double ranta = 0.5;
	
	/**
	 * SavingsAccount Constructor
	 * @param amount the amount
	 */
	public SavingsAccount( double amount )
	{		
		konto_namn = UUID.randomUUID();
		balance = amount;
		namn = "SparKonto";
	} //SavingAccount

	
	/**
	 * Overloading SavingAccount constructor
	 */ 
	public SavingsAccount()
	{
		konto_namn = UUID.randomUUID();
		balance = 0.0;
		namn = "SparKonto";
	} //SavingAccount

	
	/**
	 * deposit - Put in somemoney
	 * 
	 * @param amount
	 */
	public void deposit( double amount )
	{
		System.out.println("Insättning utfört");
		balance += amount;
	} //deposit

	
	/**
	 * withdraw - Withdraw money
	 * 
	 * @param amount
	 * 
	 * @return 0
	 */
	public double withdraw( double amount )
	{
		// See if amount can be withdrawn
		if (balance >= amount)
		{
			balance -= amount;
			System.out.println("Uttag utfört");
			return amount;
		} //end if
		else
		{
			// Withdrawal not allowed
			System.out.println("Du får ej ta ut några pengar");
			return 0.0;
		} //end else
	} //end withdraw

	
	/**
	 * getbalance - Get the money
	 * 
	 * @return balance
	 */
	public double getbalance()
	{
		return balance;
	} //end getbalance
	
	
	
	
	/**
	 * getTrans - Get the list
	 * 
	 * @return balance
	 */
	public List<Trans> getTrans()
	{
		return trans;
	} //end getbalance
	
	
	
	/**
	 * setTrans - Get the list
	 * 
	 * @param o - object Trans
	 */
	public void setTrans(Trans o)
	{
		this.trans.add(o);
	} //end setTrans
		
	
	/**
	 * getName - Get name
	 * 
	 * @return namn
	 */
	public String getName()
	{
		return namn;
	} //end getName
	
	
	
	/**
	 * setKontoNamn - Set the kontoname
	 */
	public void setKontoNamn(UUID namn)
	{
		this.konto_namn = namn;
	} //end setKontoNamn

	
	/**
	 * getKontoNamn
	 * 
	 * @return konto_namn
	 */
	public UUID getKontoNamn() 
	{
		return konto_namn;
	} //end getKontoNamn

	
	
	/**
	 * getRanta
	 * 
	 * @return ranta
	 */
	public double getRanta() 
	{
		return ranta;
	} //end getRanta
	
	
	
	/**
	 * setRanta
	 * 
	 * @param amount
	 */
	public void setRanta(double amount) 
	{
		this.ranta = amount;
	} //end setRanta
	
	
	
    /**
     * Skriv ut
     * @return string of kontonamn and balance
     */
    public String toString() 
    {      
        return "Kontonamn: " +namn +"\nKontoNummer: " +konto_namn + "\nBalance: " +balance +" kronor\n------------------------\n";
    } //end toString
    
}  //end class

-------------------------------------------------------
CreditAccount.java
------------------------------------------------------
package p3;

import java.util.List;
import java.util.UUID;


/**
 * 
 * Inlämnings uppgift 2
 * 
 * 
 * @author 		Mikael Floberg
 * @version 	1.0
 * @since 		2009-09-16
 * @see			Customer
 * @see			SavingsAccount
 * @see			BankHandler
 * @see			CivicRegistrationNumber
 * @see			CreditAccount
 * @see			Account
 * @see			Trans
 */


public class CreditAccount extends Account{

	//Declaration
	private double ranta = 0.5;
	private final static double MAX_CREDIT = 1000;
	private double skuldRanta = 5.5;
	
	
	/**
	 * CreditAccount Constructor
	 * @param amount the amount
	 */
	public CreditAccount( double amount )
	{		
		konto_namn = UUID.randomUUID();
		balance = amount;	
		namn = "KreditKonto";
	} //CreditAccount

	/**
	 * Overloading CreditAccount constructor
	 */ 
	public CreditAccount()
	{
		konto_namn = UUID.randomUUID();
		balance = 0.0;
		namn = "KreditKonto";
	} //CreditAccount
	

	/**
	 * deposit - Put in somemoney
	 * @param amount - the amount
	 */
	public void deposit( double amount )
	{
		balance += amount;
		System.out.println("Insättning utfört");
	} //deposit

	
	
	/**
	 * withdraw - Withdraw money
	 * 
	 * @param amount
	 * 
	 * @return 0
	 */
	public double withdraw( double amount )
	{
		// See if amount can be withdrawn
		if (balance+MAX_CREDIT >= amount)
		{
			balance -= amount;
			System.out.println("Uttag utfört");
			return amount;
		} //end if
		
		else
		{
			// Withdrawal not allowed
			System.out.println("Du får ej ta ut några pengar.");
			return 0.0;
		} //end else
	} //end withdraw

	
	/**
	 * getName - Get name
	 * @return namn
	 */
	public String getName()
	{
		return namn;
	} //end getName
	
	
	/**
	 * getbalance - Get the money
	 * @return balance
	 */
	public double getbalance()
	{
		return balance;
	} //end getbalance
	
	
	/**
	 * getTrans - Get the list
	 * 
	 * @return balance
	 */
	public List<Trans> getTrans()
	{
		return trans;
	} //end getbalance
	
	
	
	/**
	 * setTrans - Get the list
	 * 
	 * @param o - Object Trans
	 */
	public void setTrans(Trans o)
	{
		this.trans.add(o);
	} //end setTrans
		
	
	
	/**
	 * setKontoNamn - Set the kontoname
	 * 
	 * @param namn
	 */
	public void setKontoNamn(UUID namn)
	{
		this.konto_namn = namn;
	} //end setKontoNamn

	
	/**
	 * getKontoNamn
	 * @return konto_namn
	 */
	public UUID getKontoNamn() {
		return konto_namn;
	} //end getKontoNamn

	
	
	/**
	 * getRanta
	 * @return ranta
	 */
	public double getRanta() {
		return ranta;
	} //end getRanta
	
	
	
	/**
	 * getSkuldRanta
	 * @return skuldRanta
	 */
	public double getSkuldRanta() {
		return skuldRanta;
	} //end getSkuldRanta
	
	
	/**
	 * setRanta
	 * 
	 * @param amount
	 */
	public void setRanta(double amount) {
		this.ranta = amount;
	} //end setRanta
	
	
	
    /**
     * Skriv ut
     * @return string of kontonamn and balance
     */
    public String toString() {      
        return "KontoNummer: " +konto_namn + "\nBalance: " +balance +" kronor\n------------------------\n";
    } //end toString
    
}  //end class

Open in new window

Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

@Mickeys: I can't see any abstract method in Account Class, so why have you named it Abstract class.

Where are you using/over-riding the inherited methods from Account Class in the derived classes?

Avatar of Mickeys

ASKER

So how should it be? This is what I got told in here. ????
SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mickeys

ASKER

So all that really is wrong is that it should be like this instead?
public abstract class Account 
{	
	//Declaration
	public UUID konto_namn;
	public String namn;
	public double balance;
	public static final int MAX_UNITS = 10;
	public List<Trans> trans = new ArrayList<Trans>();

	/**
	 * getbalance - Get balance
	 * @return balance 
	 */
	public abstract double getbalance(); //end getbalance

	
	/**
	 * setKontoNamn - Sätter Konto namnet
	 * 
	 * @param namn - UUID namnet
	 */
	public abstract void setKontoNamn(UUID namn); //end setKontoNamn

	/**
	 * deposit - Put in money
	 * @param amount - How much
	 */
	public abstract void deposit( double amount );



	/**
	 * withdraw - Take out money
	 * @param amount - how much
	 * @return 0
	 */
	public abstract double withdraw( double amount ); 

}

Open in new window

@Mickeys: this is good for a start, but i think you need to do all three steps that i have mentioned in my previous reply. This is just the half of the first step.

Thanks

Avatar of Mickeys

ASKER

so the nr 2 should be

/**
       * CreditAccount Constructor
       * @param amount the amount
       */
      public CreditAccount( double amount )
      {      
                            super();
            konto_namn = UUID.randomUUID();
            balance = amount;      
            namn = "KreditKonto";
      } //CreditAccount
Avatar of Mickeys

ASKER

or should it be


      public Account(UUID konto_namn, String namn, double balance)
      {
            this.konto_namn  = konto_namn;
            this.namn = namn;
            this.balance = balance;
      }



and then in the derrived

public CreditAccount( double amount )
      {      
                            super(UUID.randomUUID(), amount, "KreditKonto");
      } //CreditAccount
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ur never writing  any return type in ur pgm !!

check that !!

<<<<<<Both are fine.

what gurvinder372: is there any return type in this method ??????