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

8.11 Ch 7 Warm up: Online shopping cart (Java) (1) Create two files to submit: I

ID: 3916811 • Letter: 8

Question

8.11 Ch 7 Warm up: Online shopping cart (Java) (1) Create two files to submit: ItemToPurchase.java - Class definition ShoppingCartPrinter.java - Contains main() method Build the ItemToPurchase class with the following specifications: Private fields String itemName - Initialized in default constructor to "none" int itemPrice - Initialized in default constructor to 0 int itemQuantity - Initialized in default constructor to 0 Default constructor Public member methods (mutators & accessors) setName() & getName() (2 pts) setPrice() & getPrice() (2 pts) setQuantity() & getQuantity() (2 pts) (2) In main(), prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine(); to allow the user to input a new string. (2 pts) Ex: Item 1 Enter the item name: Chocolate Chips Enter the item price: 3 Enter the item quantity: 1 Item 2 Enter the item name: Bottled Water Enter the item price: 1 Enter the item quantity: 10 (3) Add the costs of the two items together and output the total cost. (2 pts) Ex: TOTAL COST Chocolate Chips 1 @ $3 = $3 Bottled Water 10 @ $1 = $10 Total: $13

Explanation / Answer

ItemToPurchase.java

public class ItemToPurchase {

//Declaring instance variables

private String itemName;

private int itemPrice;

private int itemQuantity;

  

//Zero argumented constructor

public ItemToPurchase() {

this.itemName = "none";

this.itemPrice = 0;

this.itemQuantity = 0;

}

//getters and setters

public String getItemName() {

return itemName;

}

public void setItemName(String itemName) {

this.itemName = itemName;

}

public int getItemPrice() {

return itemPrice;

}

public void setItemPrice(int itemPrice) {

this.itemPrice = itemPrice;

}

public int getItemQuantity() {

return itemQuantity;

}

public void setItemQuantity(int itemQuantity) {

this.itemQuantity = itemQuantity;

}

}

_________________

ShoppingCartPrinter.java

import java.util.Scanner;

public class ShoppingCartPrinter {

public static void main(String[] args) {

//Declaring variables
String name;
int price, qty, itmTotPrice, tot = 0;

//Scanner object is used to get the inputs entered by the user
Scanner scnr = new Scanner(System.in);

//Creating an Array of ItemPurchase class
ItemToPurchase itp[] = new ItemToPurchase[2];

/* Getting the inputs entered by the user and populate
* the values into each ItemToPurchase class object
*/
for (int i = 0; i < itp.length; i++) {
//Creating an Object
itp[i] = new ItemToPurchase();
System.out.println("Item " + (i + 1));
System.out.println("Enter the item name:");
name = scnr.nextLine();
itp[i].setItemName(name);
System.out.println("Enter the item price:");
price = scnr.nextInt();
itp[i].setItemPrice(price);
System.out.println("Enter the item quantity:");
qty = scnr.nextInt();
itp[i].setItemQuantity(qty);
scnr.nextLine();

}

System.out.println("TOTAL COST");

//Calculating the total cost
for (int i = 0; i < itp.length; i++) {
itmTotPrice = itp[i].getItemQuantity() * itp[i].getItemPrice();
System.out.println(itp[i].getItemName() + " " + itp[i].getItemQuantity() + " @ $" + itp[i].getItemQuantity() + " = $" + (itmTotPrice));
tot += itmTotPrice;
}

System.out.println("Total: $" + tot);

}

}

______________________

Output:

Item 1
Enter the item name:
Chocolate Chips
Enter the item price:
3
Enter the item quantity:
1
Item 2
Enter the item name:
Bottled Water
Enter the item price:
1
Enter the item quantity:
10
TOTAL COST
Chocolate Chips 1 @ $1 = $3
Bottled Water 10 @ $10 = $10
Total: $13

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