Question: How to create the Receipt class with the following information giving the Item class along with the main method in the Receipt class? A cash
How to create the Receipt class with the following information giving the Item class along with the main method in the Receipt class?
A cash register is used in retail stores to help clerks enter a number of items and calculate their subtotal and total. It usually prints a receipt with all the items in some format. Design a Receipt class and Item class. In general, one receipt can contain multiple items. Here is what you should have in the Receipt class:
numberOfItems: this variable will keep track of the number of items added to this receipt. This is int value.
items: this is an array of Item class. The size of the array will be set in the constructor.
numberOfSales: this variable should keep track of all the Receipts.
Constructor: this constructor should accept an int value. This value will be used to set the size of the items array.
add(Item): this method accepts an item object and adds it to the items array.
add(name, price, qty, id): this is an overloaded add method. It works the same way as the add method above.
printReceipt(): this method displays a receipt with all the items. Including each items name, price, qty and subtotal. In addition, it displays the sales tax collected as well as the total after tax.
Input validation:
Dont accept any id less than 1000.
Dont accept any negative input for price or qty.
Dont accept an empty or null string for the name.
When your program gets an invalid input, it should print an error message and exit.
public class Item {
private int itemId;
private String name;
private double price;
private int qty;
public Item(String name, double price, int qty, int id) {
setItemId(id);
setName(name);
setPrice(price);
setQty(qty);
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public double getSubTotal() {
return qty * price;
}
@Override
public String toString() {
String itemInfo = this.itemId + "\t" + this.name + "\t$" + this.price
+ "\t" + this. qty + "\t$" + getSubTotal();
return itemInfo;
}
}
public static void main(String[] args) {
int maxNumberOfItems = 4;
// create an instance of CashRegister class
Receipt receipt = new Receipt(maxNumberOfItems);
// create an instance of Item class
Item item1 = new Item("Milk", 3.99, 4, 2222);
// create an instance of Item class
Item item2 = new Item("Eggs", 2.99, 3, 1222);
// add an item to the cash register
receipt.add(item1);
// add an item to the cash register
receipt.add(item2);
// add an item to the cash register using the overloaded add method
receipt.add("OJ", 5.99, 1, 1000);
// add an item to the cash register using the overloaded add method
receipt.add("Butter", 1.49, 10, 1223);
// display a receipt with all the items
receipt.printReceipt();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
