Question

In: Computer Science

Make a class called CashRegister that has the method public static double getTotalCostOfOfferings(ArrayList<Offering> o) Make this...

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

Solutions

Expert Solution

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;
    }
}

Related Solutions

Using maps in Java. Make a public class called ExampleOne that provides a single class (static)...
Using maps in Java. Make a public class called ExampleOne that provides a single class (static) method named firstOne. firstOne accepts a String array and returns a map from Strings to Integer. The map must count the number of passed Strings based on the FIRST letter. For example, with the String array {“banana”, “apples”, “blueberry”, “orange”}, the map should return {“b”:2, “a”:1, “o”:1}. Disregard empty Strings and zero counts. Retrieve first character of String as a char using charAt, or,...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes...
Make a public class Creater that provides a single class (static) method named subtractor. subtractor takes a single int argument and returns a method that implements the following Create interface: public interface Create { int change(int something); --------------------------------------------------------------------- The returned “function” should implement Create so that it subtracts the value passed to subtractor. For example Create one = Creater.substractor(5); Create two = Creater.subtractor(-3); System.out.println(one.change(5)); // should print 0 System.out.println(second.change(3); // should print -6 The answer should be is a single...
for java!! Make a public class Value that provides a static method named test. test takes...
for java!! Make a public class Value that provides a static method named test. test takes a single int argument and returns an anonymous object that implements the following Absolute interface: public interface Absolute { int same(); int opposite(); } -------------------------------------------------------------------------- The returned object should implement same so that it returns the passed int as it is, and opposite so that it returns the passed int as the int * -1. For example Absolute first = Value.test(-90) Absolute second =...
public class Assignment3 { public static Queue> makeQueue(double[] a){ // Each element of the given array...
public class Assignment3 { public static Queue> makeQueue(double[] a){ // Each element of the given array a must be inserted into a BTNode, // this method returns a queue of BTNodes, each node will contain a dataNode // the dataNode will have the value equal to the element of the array // count equal to the number of times that the element repeats // min and max must be equal to value. // the BTNode created must have its parent,...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass;...
import java.util.Scanner; public class Lab9Q3 { public static void main (String [] atgs) { double mass; double velocity; double totalkineticEnergy;    Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the objects mass in kilograms"); mass = keyboard.nextDouble();    System.out.println ("Please enter the objects velocity in meters per second: "); velocity = keyboard.nextDouble();    double actualTotal = kineticEnergy (mass, velocity); System.out.println ("Objects Kinetic Energy: " + actualTotal); } public static double kineticEnergy (double mass, double velocity) { double totalkineticEnergy =...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
Error: Main method is not static in class ArrayReview, please define the main method as: public...
Error: Main method is not static in class ArrayReview, please define the main method as: public static void main(String[] args) please help me fast: import java.util. Random; import java.util.Scanner; //ArrayReview class class ArrayReview { int array[];    //constructor ArrayReview (int n) { array = new int[n]; //populating array Random r = new Random(); for (int i=0;i<n; i++) array[i] = r.nextInt (); } //getter method return integer at given index int getElement (int i) { return array[i]; }    //method to...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate =...
public class sales_receipt { public static void main(String[] args) { //declare varables final double tax_rate = 0.05; //tax rate String cashier_name = "xxx"; //sales person String article1, article2; //item name for each purchased int quantity1, quantity2; //number of quantities for each item double unit_cost1, unit_cost2; //unit price for each item double price1, price2; //calculates amounts of money for each item. double sub_total; //total price of two items without tax double tax; //amount of sales tax double total; //total price of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT