In: Computer Science
Find a receipt from any purchase you made, and try to represent it in a Java program. Make sure to use the most appropriate data type for each of these items
import java.util.ArrayList;
class Item {
private String name;
private double price;
private double tax;
public Item(String aName, double aPrice, double
aTax) {
super();
name = aName;
price = aPrice;
tax = aTax;
}
public String getName() {
return name;
}
public void setName(String aName) {
name = aName;
}
public double getPrice() {
return price;
}
public void setPrice(double aPrice) {
price = aPrice;
}
public double getTax() {
return tax;
}
public void setTax(double aTax) {
tax = aTax;
}
public String toString() {
return name + " :" + price;
}
}
public class Receipt {
public static void main(String[] args) {
ArrayList<Item> list = new
ArrayList<Item>();
list.add(new Item("Soap", 10,
0.05));
list.add(new Item("Brush", 16,
0.05));
list.add(new Item("Tv", 100,
0.1));
double total = 0;
double tax = 0;
for (Item i : list) {
total +=
i.getPrice();
tax +=
i.getPrice() * i.getTax();
}
System.out.println("No of Items : "
+ list);
System.out.println("Sub total : " +
total);
System.out.println("Tax : " +
tax);
System.out.println("Total : " +
(tax + total));
}
}