[JAVA]: Hello! I need some help with a java program. Thanks. Part 1: Part 2: 20.
ID: 3757139 • Letter: #
Question
[JAVA]:
Hello! I need some help with a java program.
Thanks.
Part 1:
Part 2:
20.7 Program 3a: Online shopping cart (1) Create two files to submit ItemToPurchase.java Class definition ShoppingCartPrinterjava- 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 o Default constructor Public member methods (mutators & accessors) setName) & getName0 (2 pts) setPrice)&getPrice0 (2 pts) setQuantity0& getQuantity0 (2 pts) toStringo (1 pt) (2) In main0. (ShoppingCartPrinterjava) prompt the user for two items and create two objects of the ItemToPurchase class. Before prompting for the second item, call scnr.nextLine0; to allow the user to input a new string. (2 pts) Ex: Item 1 Enter the item name Chocolate Chips Enter the item price: Enter the item quantity: Item 2 Enter the item name: Bottled Water Enter the item price: Enter the item quantity: 10Explanation / Answer
PROGRAM:
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.