Question #59471

You must also override toString() method and return a user-friendly receipt of all the items in the basket in this cashier. Once you run the TestSystem.java file that is already supplied, you must see something similar to the following:

Number of items is: 4
Total cost is: 157.69
Total tax is: 3.94
Eat Healthy Fruit Shop
---------------------
12 @ 45.00 /dz
Gala Apples 45.00
2.30 kg @ 4.30 /kg
African bananas 9.89
23 @ 3.20 /pc
Qatari Lemons 73.60
1 @ 19.20 /cp
+ 10.00 Labor
Lemon mix juice 29.20
Tax: 3.94
Total cost: 161.63
Eat Healthy Fruit Shop
---------------------
23.00 kg @ 33.20 /kg
Thailand Bananas 763.60
4 @ 19.20 /cp
+ 10.00 Labor
Juice Mix 86.80
Tax: 21.26
Total cost: 871.66

Expert's answer

Answer on Question #59471, Programming & Computer Science, Java, JSP, JSF

Condition

You must also override to String() method and return a user-friendly receipt of all the items in the basket in this cashier. Once you run the TestSystem.java file that is already supplied, you must see something similar to the following:

Number of items is: 4

Total cost is: 157.69

Total tax is: 3.94

Eat Healthy Fruit Shop

12 @ 45.00 /dz

Gala Apples 45.00

2.30 kg @ 4.30 /kg

African bananas 9.89

23 @ 3.20 /pc

Qatari Lemons 73.60

1 @ 19.20 /cp

+ 10.00 Labor

Lemon mix juice 29.20

Tax: 3.94

Total cost: 161.63

Eat Healthy Fruit Shop

23.00 kg @ 33.20 /kg

Thailand Bananas 763.60

4 @ 19.20 /cp

+ 10.00 Labor

Juice Mix 86.80

Tax: 21.26

Total cost: 871.66

Code

TestSystem.java


package qa.edu.qu.cmps251.hw3;
import qa.edu.qu.cmps251.hw3.model.*;
import java.util.ArrayList;
public class TestSystem {
    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(1, 19.20, 5);
        Cashier cashier = new Cashier("Eat Healthy 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();
    public abstract double getAmount();
    public abstract double getPrice();
}


Consts.java


package qa.edu.qu.cmps251.hw3;
public class Consts {
    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 qa.edu.qu.cmps251.hw3.model.OrangeLime;
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 shopName + "\n" +
                "Number of items is: " + getNumberOfItems() + "\n" +
                "Total cost is: " + getTotalCost() + "$" + "\n" +
                "Total tax is: " + getTotalTax() + "$" + "\n" +
                fruits.get(0).getAmount() + " @ " + fruits.get(0).getPrice() + "/dz" + "\n" +
                "Gala apples " + fruits.get(0).getCost() + "$" + "\n" +
                fruits.get(1).getAmount() + "kg" + "@" + fruits.get(1).getPrice() + "/kg" + "\n" +
                "African bananas " + fruits.get(1).getCost() + "$" + "\n" +
                fruits.get(2).getAmount() + "@" + fruits.get(2).getPrice() + "/pc" + "\n" +
                "Qatari Lemons " + fruits.get(2).getCost() + "$" + "\n" +
                fruits.get(3).getAmount() + "@" + fruits.get(3).getPrice() + "/cp" + "\n" +
                "+ " + ((OrangeLime)fruits.get(3)).getLaborCost() + "Labor" + "\n" +
                "Orange lime mix juice: " + fruits.get(3).getCost() + "$";
    }
}


Apple.java


package qa.edu.qu.cmps251.hw3.model;
import qa.edu.qu.cmps251.hw3.Consts;
public class Apple extends Fruit {
    private double applesNumber;
    public Apple(int number) {
        this.applesNumber = number;
    }
    @Override
    public double getCost() {
        return Math.floor(((Consts.APPLE_PRICE / 12.0) * applesNumber) * 100) / 100.0;
    }
    public double getAmount() {
        return applesNumber;
    }
    public double getPrice() {
        return Consts.APPLE_PRICE;
    }
}


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;
    }
    public double getAmount() {
        return bananasWeight;
    }
    public double getPrice() {
        return bananaPrice;
    }
}


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;
    }
    public double getAmount() {
        return lemonsNumber;
    }
    public double getPrice() {
        return lemonPrice;
    }
}


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;
    }
    public double getAmount() {
        return numberOfJuices;
    }
    public double getLaborCost() {
        return laborCost;
    }
    public double getPrice() {
        return Math.floor((Consts.LEMONS_IN_A_JUICE * pricePerLemon) * 100) / 100.0;
    }
}


Output

Eat Healthy Fruit Shop

Number of items is: 4

Total cost is: 167.69Totaltaxis:33.53Total tax is: 33.53

3.0 @ 24.0/dz

Gala apples 6.03.0kg@12.5/kgAfricanbananas37.53.0 kg @ 12.5/kg African bananas 37.5

6.0 @ 1.5/pc

Qatari Lemons 9.01.0@115.19/cp+5.0LaborOrangelimemixjuice:115.191.0 @ 115.19/cp + 5.0 Labor Orange lime mix juice: 115.19

http://www.AssignmentExpert.com/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS