1 Replies - 468 Views - Last Post: 10 March 2013 - 02:11 AM Rate Topic: -----

#1 latuya123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-February 13

How to use Composition instead of Inheritance?

Posted 10 March 2013 - 12:04 AM

// Hello everybody: I'm a little stuck on how to demonstrate composition of (as opposed to inheritance from) a CommissionEmployee object for "BaseSalary"
// How do I throw an IllegalArgumentException if the salary argument is negative ("setBaseSalary")
// Note: Right now I DONT need a Main()
// Thanks :bigsmile:/>

public class BasePlusCommissionEmployeeComposition {
	
         private double baseSalary; [b]// base salary per week
       // Demonstrate composition of (as opposed to inheritance from) a CommissionEmployee object[/b]

	 private String firstName;
	 private String lastName;
	 private String socialSecurityNumber;
	 private double baseSales;
	 private double baseRate; 
	 
	 
	 public BasePlusCommissionEmployeeComposition( String first, String last, 
	            String ssn, double sales, double rate, double salary) {
	 
		firstName = first;
		lastName = last;
		socialSecurityNumber = ssn;
		setBaseSales( sales ); // validate and store gross sales
		setBaseRate( rate );
		setBaseSalary(salary);
		 
		 }
	 
  
		 public void setBaseSalary(double salary) 
	 {
		  if ( salary >= 0.0 )
		  baseSalary = salary;
		  else
		  throw new IllegalArgumentException(
		  "Base salary must be >= 0.0" );
		  }
	 
	 public double getBaseSalary()
	 {
	 return baseSalary;
	 }
	 //
	 
	 
	 
	 public String getFirstName() {
		 return firstName;
	    }
	 
	 public void setFirstName(String first) {
		 firstName = first;
	    }
	 
	 public String getLastName() {
	    	return lastName;
	    }

	   public void setLastName(String last) {
		   lastName = last;
	    }
	 
	   public String getSocialSecurityNumber() {
	    	return socialSecurityNumber;
	    }

	    public void setSocialSecurityNumber(String ssn) {
	    	socialSecurityNumber = ssn;
	    }
	   
	   
	    public double getSales() {
	    	return baseSales;
	    }

	    public void setBaseSales(double sales) {
	    	baseSalary = sales;
	    }
	   
	    public double getRate() {
	    	return baseRate;
	    }

	    public void setBaseRate(double rate) {
	    	baseRate = rate;
	    }
	    
	    public double earnings() {
	    	return baseSalary + (baseRate * baseSales);
	    }
	    
	    @Override
	    public String toString() {        
	    	
	    	return String.format(
	    	"%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f\n%s: %.2f",
	    	"base-salaried commission employee", firstName, lastName,
	    	 "social security number", socialSecurityNumber,
	    	 "gross sales", baseSales, "commission rate", baseRate,
	    	 "base salary", baseSalary);
	    }
	    
}

Edit: Added code tags. Please use code tags.

This post has been edited by GregBrannon: 10 March 2013 - 02:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to use Composition instead of Inheritance?

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: How to use Composition instead of Inheritance?

Posted 10 March 2013 - 02:11 AM

Here's a simple example of a class that uses composition instead of inheritance. Waitress could have extended Employee (inheritance) but instead incorporates an Employee object into the Waitress constructor(s) to favor composition.
// class Waitress is based on class Employee. Employee has two String fields,
// firstName and lastName and a single constructor that sets both fields.
class Waitress
{
	// shift can be Breakfast, Lunch, or Dinner
	String shift;
	
	// a constructor that builds a Waitress instance from an Employee instance
	// passed as a parameter
	public Waitress( Employee employee, String shift )
	{
		this.shift = shift;
	}
	
	// a constructor that first builds an Employee object and then adds
	// Waitress details
	public Waitress( String firstName, String lastName, String shift )
	{
		this( new Employee( firstName, lastName ), shift );
	}
	
} // end class Waitress

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1