Answer on Question #59442, Programming & Computer Science, Java, JSP, JSF
Condition
You will also need to create a Cashier.java file in package qa.edu.qu.cmps251.hw3 (not in the .model subpackage) that implements this interface. The cashier must have an ArrayList of fruits, a method called getTotalCost() will return the cost of all the fruit objects in the list, and getTotalTax() will return only the total taxes of the fruits in the list. You must use the constant TAX_RATE in the Consts.java file to get that rate and calculate the taxes properly. The constructor of this class must take the shop name. This shop name will be printed in the receipt eventually.
Pay attention that you must round your prices to the nearest 2 digit decimal points in both of these methods.
You must also override toString() method and return a user-friendly receipt of all the items in the basket in this cashier.
Code
Shop.java
package qa.edu.qu.cmps251.hw3;
import qa.edu.qu.cmps251.hw3.model.*;
import java.util.ArrayList;
public class Task {
public static void main(String[] args) {
ArrayList<Fruit> fruits = new ArrayList<Fruit>();
Apple apple = new Apple(3);
Banana banana = new Banana(3, 12.5);
Lemon lemon = new Lemon(6, 1.5);
OrangeLime orangeLime = new OrangeLime(4, 0.4, 5);
Cashier cashier = new Cashier("Fruit shop", fruits);
cashier.addItem(apple);
cashier.addItem(banana);
cashier.addItem(lemon);
cashier.addItem(orangeLime);
System.out.println(cashier);
}
}Fruit.java
package qa.edu.qu.cmps251.hw3.model;
public abstract class Fruit {
public abstract double getCost();
}Consts.java
package qa.edu.qu.cmps251.hw3;
public class Consts {
public static final String PRICE_FORMAT = "#0.00";
public static final double APPLE_PRICE = 24.0; // per dozen
public static final int LEMONS_IN_A_JUICE = 6;
public static final int TAX_RATE = 20;
}Cashier.java
package qa.edu.qu.cmps251.hw3;
import qa.edu.qu.cmps251.hw3.model.Fruit;
import java.util.ArrayList;
public class Cashier {
private String shopName;
private ArrayList<Fruit> fruits;
public Cashier(String shop, ArrayList<Fruit> fruits) {
this.shopName = shop;
this.fruits = fruits;
}
public double getTotalCost() {
double cost = 0;
for (Fruit fruit : fruits) {
cost = cost + fruit.getCost();
}
return Math.floor(cost * 100) / 100.0;
}
public double getTotalTax() {
return Math.floor((getTotalCost() * Consts.TAX_RATE / 100) * 100) / 100.0;
}
public void addItem(Fruit fruit) {
fruits.add(fruit);
}
public int getNumberOfItems() {
return fruits.size();
}
public void clear() {
fruits.clear();
}
@Override
public String toString() {
return "Shop: " + shopName + "\n" +
"1. Apple: " + fruits.get(0).getCost() + "$" + "\n" +
"2. Banana: " + fruits.get(1).getCost() + "$" + "\n" +
"3. Lemon: " + fruits.get(2).getCost() + "$" + "\n" +
"4. Orange lime: " + fruits.get(3).getCost() + "$" + "\n" +
"---" + "\n" +
"Items: " + getNumberOfItems() + "\n" +
"Total cost: " + getTotalCost() + "$" + "\n" +
"Total tax: " + getTotalTax() + "$";
}
}Apple.java
package qa.edu.qu.cmps251.hw3.model;
import qa.edu.qu.cmps251.hw3.Consts;
public class Apple extends Fruit {
private int applesNumber;
public Apple(int number) {
this.applesNumber = number;
}
@Override
public double getCost() {
return Math.floor(((Consts.APPLE_PRICE / 12.0) * applesNumber) * 100) / 100.0;
}
}Banana.java
package qa.edu.qu.cmps251.hw3.model;
public class Banana extends Fruit {
private double bananasWeight;
private double bananaPrice;
public Banana(double weight, double price) {
this.bananasWeight = weight;
this.bananaPrice = price;
}
@Override
public double getCost() {
return Math.floor((bananasWeight * bananaPrice) * 100) / 100.0;
}
}Lemon.java
package qa.edu.qu.cmps251.hw3.model;
public class Lemon extends Fruit {
private int lemonsNumber;
private double lemonPrice;
public Lemon(int number, double price) {
this.lemonsNumber = number;
this.lemonPrice = price;
}
@Override
public double getCost() {
return Math.floor((lemonPrice * lemonsNumber) * 100) / 100.0;
}
}OrangeLime.java
package qa.edu.qu.cmps251.hw3.model;
import qa.edu.qu.cmps251.hw3.Consts;
public class OrangeLime extends Lemon {
private int numberOfJuices;
private double pricePerLemon;
private double laborCost;
public OrangeLime(int number, double price, double labor) {
super(number, price);
this.numberOfJuices = number;
this.pricePerLemon = price;
this.laborCost = labor;
}
@Override
public double getCost() {
return Math.floor((numberOfJuices * Consts.LEMONS_IN_A_JUICE * pricePerLemon) * 100) / 100.0;
}
}http://www.AssignmentExpert.com/
Output
Shop: Fruit shop
1. Apple: 6.0
3. Lemon: 9.0
Items: 4
Total cost: 62.1