Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this programming assignment, the goal is to create a smart shopping cart in J

ID: 3601198 • Letter: I

Question

In this programming assignment, the goal is to create a smart shopping cart in JAVA Think of the self-checkout option in many stores these days.

Design a ShoppingCart class that contains Items. This uses composition since there is an array of Item objects inside a ShoppingCart. Several interesting methods are

addItem to insert an item to the cart. This is a void method since it modifies the state of the array in the ShoppingCart object. There are different ways to implement this method. One was is to have a single parameter that is an already constructed Item. Another way is to have a set of parameters that are the parameters to the constructor of the Item class.

cartTotal computes the total price of the cart. This method returns a double but does not need any parameters since it works with data members of the ShoppingCart object.

cartTaxAmount receives the tax rate and computes the total tax to charge for the items currently in the cart. Only Taxable items should be considered for this calculation. This method also returns a double but does not need any parameters since it works with data members of the ShoppingCart object.

For simplicity, you can assume that the capacity of a ShoppingCart is fixed at 10 items.

Keep in mind:

Each class should have at least one constructor that receives arguments.

Make data members private and create accessor and mutator methods whenever necessary.

Each class should have a toString method that you use to display the contents of the class.

Explanation / Answer

Item.java

public class Item {

//Declaring instance variables
private int itemNo;
private String itemName;
private double price;
boolean isTaxable;

//Parameterized constructor
public Item(int itemNo, String itemName, double price, boolean isTaxable) {
super();
this.itemNo = itemNo;
this.itemName = itemName;
this.price = price;
this.isTaxable = isTaxable;
}


//getters and setters
public int getItemNo() {
return itemNo;
}

public void setItemNo(int itemNo) {
this.itemNo = itemNo;
}

public String getItemName() {
return itemName;
}

public void setItemName(String itemName) {
this.itemName = itemName;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public boolean isTaxable() {
return isTaxable;
}

public void setTaxable(boolean isTaxable) {
this.isTaxable = isTaxable;
}

//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return "Item No=" + itemNo + ", Item Name=" + itemName + ", Price=$" + price + ", Is Taxable=" + isTaxable;
}

}

__________________

ShoppingCart.java

import java.util.Arrays;

public class ShoppingCart {
//Declaring constant
final double taxRate = 5.5;
//Declaring static variable
static int count;
Item sc[] = null;

//Zero argumented constructor
public ShoppingCart() {
sc = new Item[10];
count = 0;
}

//This method will add the Item to the ShoppingCart
public void addItem(Item itm) {
if (count < 10) {
sc[count] = itm;
count++;
} else
System.out.println("** Shopping Cart is Full **");
}

//This method will calculate the Total cost of all items in the Cart
public double cartTotal() {
double total = 0.0;
for (int i = 0; i < count; i++) {
total += sc[i].getPrice();
}
return total;
}

//This method will calculate the Total tax of all items in the Cart
public double cartTaxAmount() {
double totalTax = 0.0;
for (int i = 0; i < count; i++) {
if (sc[i].isTaxable) {
totalTax += sc[i].getPrice() * (taxRate / 100);
}
}
return totalTax;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
System.out.println("__ShoppingCart __");
for (int i = 0; i < count; i++) {
System.out.println(sc[i]);
}

return " ";
}


}

________________

Test.java

import java.text.DecimalFormat;

public class Test {

public static void main(String[] args) {
// DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat(".00");

//Creating an Instances of Item class objects
Item item1 = new Item(111, "Book", 5.00, false);
Item item2 = new Item(222, "Pen", 2.00, false);
Item item3 = new Item(333, "Umbrella", 10.00, true);
Item item4 = new Item(444, "Mobile", 500.00, true);
Item item5 = new Item(555, "Tablet", 400.00, true);

//Creating an instance of ShoppingCart class
ShoppingCart sc = new ShoppingCart();

//Adding Items to the Shoppingcart
sc.addItem(item1);
sc.addItem(item2);
sc.addItem(item3);
sc.addItem(item4);
sc.addItem(item5);

//Displaying the Items added to the ShoppingCart
System.out.println(sc);

//Displaying the tottal cost of all the Items in the ShoppingCart and the Total Tax of all the Items
System.out.println("Total Amount :$" + df.format(sc.cartTotal()));
System.out.println("Total Tax :$" + df.format(sc.cartTaxAmount()));


}

}

______________________

Output:

__ShoppingCart __
Item No=111, Item Name=Book, Price=$5.0, Is Taxable=false
Item No=222, Item Name=Pen, Price=$2.0, Is Taxable=false
Item No=333, Item Name=Umbrella, Price=$10.0, Is Taxable=true
Item No=444, Item Name=Mobile, Price=$500.0, Is Taxable=true
Item No=555, Item Name=Tablet, Price=$400.0, Is Taxable=true

Total Amount :$917.00
Total Tax :$50.05

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote