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

In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
Create a class called FileSumWrapper with a method that has the signature public static void handle(String...
Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound). Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. I'm unsure at how to incorporate the FileSum.read() method to even start anything. I also don't know...
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...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList:   public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol"
(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void...
(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void sort(ArrayList<E> list) Write a program to test the method with three different arrays: Integers: 2, 4, and 3; Doubles: 3.4, 1.2, and -12.3; Strings: "Bob", "Alice", "Ted", and "Carol" This program is similar to the Case Study in the book: Sorting an Array of Objects the language is in java
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...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[]...
////Fixme(1) add a statement to import ArrayList class public class ListManipulation { public static void main(String[] args) { //Fixme(2) create an ArrayList of integers and name the ArrayList list. //Fixme(3) add the following numbers to the list: 10, 15, 7, -5, 73, -11, 100, 20, 5, -1    displayList(list); System.out.println(); displayListBackwards(list);       }    public static void displayList(ArrayList<Integer> list) { for(Integer i: list) System.out.print(i + " ");    } //Fixme(4) define a method displayListBackwards, which takes an ArrayList as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT