In: Computer Science
Make a class called CashRegister that has the method
public static double getTotalCostOfOfferings(ArrayList<Offering> o)
Make this method calculate the total cost of all Offering objects in the ArrayList.
Submit Product, Service, and CashRegister.
Given Files:
import java.util.ArrayList;
public class Demo3
{
public static void crunch(ArrayList<Offering> o) {
System.out.println("Adding up the following offerings:");
for (Offering current : o) {
System.out.println(current);
}
System.out.printf("Total for all: $%,.2f\n", CashRegister.getTotalCostOfOfferings(o));
System.out.println("---------------------------------\n");
}
public static void main(String[] args)
{
ArrayList<Offering> offeringList = new ArrayList<>();
offeringList.add(new Product("iPhone", 999));
offeringList.add(new Product("Movie ticket", 12));
crunch(offeringList);
offeringList.add(new Product("Backpack", 25));
offeringList.add(new Product("Toyota Corolla", 19000));
crunch(offeringList);
offeringList.add(new Service("Gardening", 15, 10));
offeringList.add(new Service("House keeping", 20, 17));
crunch(offeringList);
offeringList.add(new Service("Data Entry", 10, 25));
offeringList.add(new Service("Driver", 13, 3));
crunch(offeringList);
}
}
And:
public abstract class Offering
{
private String name;
public Offering(String n) {
name = n;
}
public abstract double getTotalCost();
public String toString() {
return name + " costs $" + String.format("%.2f", getTotalCost());
}
}
And:
public class Service extends Offering
{
private String names;
private String hourRate;
private double rate;
private double totalCost;
public Service(String n, double r, double h)
{
super(n);
names = n;
rate = r;
hourRate = " Each hour costs " + String.format("%.2f", r);
totalCost = r * h;
}
@Override
public double getTotalCost()
{
return totalCost;
}
@Override
public String toString()
{
return names + " costs $" + String.format("%.2f", getTotalCost()) + "." + hourRate;
}
}
//////////// Required Output ////////////
Adding up the following offerings:\n iPhone costs $999.00\n Movie ticket costs $12.00\n Total for all: $1,011.00\n ---------------------------------\n \n Adding up the following offerings:\n iPhone costs $999.00\n Movie ticket costs $12.00\n Backpack costs $25.00\n Toyota Corolla costs $19000.00\n Total for all: $20,036.00\n ---------------------------------\n \n Adding up the following offerings:\n iPhone costs $999.00\n Movie ticket costs $12.00\n Backpack costs $25.00\n Toyota Corolla costs $19000.00\n Gardening costs $150.00. Each hour costs 15.00\n House keeping costs $340.00. Each hour costs 20.00\n Total for all: $20,526.00\n ---------------------------------\n \n Adding up the following offerings:\n iPhone costs $999.00\n Movie ticket costs $12.00\n Backpack costs $25.00\n Toyota Corolla costs $19000.00\n Gardening costs $150.00. Each hour costs 15.00\n House keeping costs $340.00. Each hour costs 20.00\n Data Entry costs $250.00. Each hour costs 10.00\n Driver costs $39.00. Each hour costs 13.00\n Total for all: $20,815.00\n ---------------------------------\n \n

public class Product extends Offering {
private double cost;
public Product(String name, double cost) {
super(name);
this.cost = cost;
}
@Override
public double getTotalCost() {
return cost;
}
}

public class Service extends Offering {
private String names;
private String hourRate;
private double rate;
private double totalCost;
public Service(String n, double r, double h) {
super(n);
names = n;
rate = r;
hourRate = " Each hour costs " + String.format("%.2f", r);
totalCost = r * h;
}
@Override
public double getTotalCost() {
return totalCost;
}
@Override
public String toString() {
return names + " costs $" + String.format("%.2f", getTotalCost()) + "." + hourRate;
}
}

import java.util.ArrayList;
public class CashRegister {
public static double getTotalCostOfOfferings(ArrayList<Offering> offerings) {
double total = 0;
for (int i = 0; i < offerings.size(); i++) {
total += offerings.get(i).getTotalCost();
}
return total;
}
}