In: Computer Science
Using NetBeans, Modify your sales application so that it polymorphically processes any account objects that are created. Complete the following:
Create a 2-item array of type Account.
Store each account object created into the
array.
For each element in this array, call the
calculateSales() method, and use the toString() method to display
the results.
Code should be fully commented.
Program flow should be logical.
Code before revision:
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package inheritance;
/**
*
* @author Aaron Drury
*/
public class Inheritance {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
abstract class Account {
private int accountID;
public Account(int accountID) {
this.accountID = accountID;
}
public int getAccountID() {
return accountID;
}
public void setAccountID(int accountID) {
this.accountID = accountID;
}
public abstract double calculateSales();
@Override
public String toString() {
return "Account [\naccountID: " + accountID + "]";
}
}
class Sales extends Account {
private int numberOfHours;
private double ratePerHour;
public Sales(int accountID, int numberOfHours, double
ratePerHour) {
super(accountID);
this.numberOfHours = numberOfHours;
this.ratePerHour = ratePerHour;
}
public int getHours() {
return numberOfHours;
}
public void setfHours(int numberOfHours) {
this.numberOfHours = numberOfHours;
}
public double getRate() {
return ratePerHour;
}
public void setRate(double ratePerHour) {
this.ratePerHour = ratePerHour;
}
@Override
public double calculateSales() {
return numberOfHours * ratePerHour;
}
@Override
public String toString() {
return "Sales [\naccountID: " + super.getAccountID() +
"\nnumberOfHours: " + numberOfHours + "\nratePerHour: "
+ ratePerHour + "]";
}
}
class Service extends Account {
private int numberOfItems;
private int pricePerItem;
public Service(int accountID, int numberOfItems, int
pricePerItem) {
super(accountID);
this.numberOfItems = numberOfItems;
this.pricePerItem = pricePerItem;
}
public int getNumberOfItems() {
return numberOfItems;
}
public void setNumberOfItems(int numberOfItems) {
this.numberOfItems = numberOfItems;
}
public int getPricePerItem() {
return pricePerItem;
}
public void setPricePerItem(int pricePerItem) {
this.pricePerItem = pricePerItem;
}
@Override
public double calculateSales() {
return numberOfItems * pricePerItem;
}
@Override
public String toString() {
return "Service [\naccountID: " + super.getAccountID() +
"\nnumberOfItems: " + numberOfItems
+ "\npricePerItem: " + pricePerItem + "]";
}
}
}
}
/*The java program that creates an array of Account
type of size,2. Then, instantiate the Sales and Service class
objects and set to to the accounts array. Then call the methods in
a for loop and display the account details and sales amount on the
console output.
* */
//Polymorphism.java
public class Polymorphism
{
public static void main(String[] args)
{
//Create an array of type Account
of size,2
Account[] accounts=new
Account[2];
//Set the objects of the Sales and
services to the Account objects
accounts[0]=new Sales(1234, 40,
10);
//Set the objects of the Sales and
services to the Account objects
accounts[1]=new Service(5678, 50,
10);
//Display the account details and
sales value on console
for(int
index=0;index<accounts.length;index++)
{
//call toString
method on accounts object
System.out.println("Account :"+accounts[index].toString());
//call
calculateSales method
System.out.println("Sales
:"+accounts[index].calculateSales());
}
}//end of main method
}//end of class
------------------------------------------------------------------------------------
//Account.java
abstract class Account
{
private int accountID;
public Account(int accountID)
{
this.accountID = accountID;
}
public int getAccountID()
{
return accountID;
}
public void setAccountID(int accountID)
{
this.accountID = accountID;
}
public abstract double calculateSales();
public String toString()
{
return "Account [\naccountID: " +
accountID + "]";
}
}
------------------------------------------------------------------------------------
//Sales.java
class Sales extends Account
{
private int numberOfHours;
private double ratePerHour;
public Sales(int accountID, int numberOfHours, double
ratePerHour)
{
super(accountID);
this.numberOfHours =
numberOfHours;
this.ratePerHour =
ratePerHour;
}
public int getHours()
{
return numberOfHours;
}
public void setfHours(int numberOfHours)
{
this.numberOfHours =
numberOfHours;
}
public double getRate()
{
return ratePerHour;
}
public void setRate(double ratePerHour)
{
this.ratePerHour =
ratePerHour;
}
public double calculateSales()
{
return numberOfHours *
ratePerHour;
}
public String toString()
{
return "Sales [\naccountID: " +
super.getAccountID() + "\nnumberOfHours: " + numberOfHours +
"\nratePerHour: "
+ ratePerHour + "]";
}
}
------------------------------------------------------------------------------------
//Service.java
class Service extends Account
{
private int numberOfItems;
private int pricePerItem;
public Service(int accountID, int numberOfItems, int
pricePerItem)
{
super(accountID);
this.numberOfItems =
numberOfItems;
this.pricePerItem =
pricePerItem;
}
public int getNumberOfItems()
{
return numberOfItems;
}
public void setNumberOfItems(int numberOfItems)
{
this.numberOfItems =
numberOfItems;
}
public int getPricePerItem()
{
return pricePerItem;
}
public void setPricePerItem(int pricePerItem)
{
this.pricePerItem =
pricePerItem;
}
public double calculateSales()
{
return numberOfItems *
pricePerItem;
}
public String toString()
{
return "Service [\naccountID: " +
super.getAccountID() + "\nnumberOfItems: " + numberOfItems
+ "\npricePerItem: " + pricePerItem + "]";
}
}
------------------------------------------------------------------------------------
Sample Output:
Account :Sales [
accountID: 1234
numberOfHours: 40
ratePerHour: 10.0]
Sales :400.0
Account :Service [
accountID: 5678
numberOfItems: 50
pricePerItem: 10]
Sales :500.0