Chapter 10 Object-Oriented Programming: Polymorphism
Problem Description An employee is either a salaried employee or a hourly employee or a commission employee A base-salaried commission employee is a commission employee with a base salary Every employee has a first name, a last name, and a social security number In addition to the info above –The salaried employee has a weekly salary –The hourly employee has a wage per hour and number of hours worked –The commission employee has a commission rate and gross weekly sales –The base-salaried commission employee has a base salary, a commission rate, and gross weekly sales Develop a method to calculate the earnings for all kinds of employees
Example of a Salaried Employee First name: John Last name: Smith SSN: Weekly salary: 800 USD Earnings = 800 USD
Example of a Hourly Employee First name: Karen Last name: Price SSN: Wage per hour: USD If the number of hours worked is 40, the earnings will be 40 = 670 USD If the number of hours worked is 45, the earnings will be (45 – 40) 1.5 = = USD
Example of a Commission Employee First name: Sue Last name: Jones SSN: Gross weekly sales: 10,000 USD Commission rate:.06 Earnings = 600 USD If the gross weekly sales is 500 USD and the commission rate is.1, the earnings will be 50 USD
Example of a Base Salaried Commission Employee First name: Bob Last name: Lewis SSN: Commission rate:.04 Base salary: 300 USD If the gross weekly sales is 5,000 USD, the earnings will be 500 USD
Case Study: Creating and Using Interfaces
Problem Description Develop a simple invoice that contains billing information for only one kind of part Billing information includes –The part number –The part description –The quantity –The price per item The invoice object should be able to calculate the amount of payment
Examples Example 1 –Part number: –Part description: Seat –Quantity: 2 –Price per item: Payment amount = Example 2 –Part number: –Part description: Tire –Quantity: 4 –Price per item: Payment amount = 319.8
Question Q: What both classes Invoice and Employee have in common? A: Classes Invoice and Employee both represent things for which the company must be able to calculate a payment amount –But the way of calculation is different We can use an interface called Payable to combine the two designs into one