6.20 Warm up: Online shopping cart (Java) Create a program using classes that do
ID: 3868605 • Letter: 6
Question
6.20 Warm up: Online shopping cart (Java)
Create a program using classes that does the following using your NetBeans IDE and upload it here:
(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:
(3) Add the costs of the two items together and output the total cost. (2 pts)
Ex:
This should have two components to be submitted called>>>
ItemToPurchase.java
ShoppingCartPrinter.java
Explanation / Answer
ItemToPurchase.java:
public class ItemToPurchase
{
//declaring the private variables
private String itemName;
private int itemPrice;
private int itemQuantity;
//Default constructor with fields
public ItemToPurchase(String itemName, int itemPrice, int itemQuantity) {
super();
//declared the default constructor values as asked
this.itemName = "none";
this.itemPrice = 0;
this.itemQuantity = 0;
}
//Access mutators for the variables
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) {
//initiating the variables
String itemName="";
int price = 0;
int quantity = 0;
//Initiating the scanner object
Scanner sc = new Scanner(System.in);
//Creating 2 objects of ItemToPurchase.java for two items to be entered
ItemToPurchase item1 = new ItemToPurchase(itemName, price, quantity);
ItemToPurchase item2 = new ItemToPurchase(itemName, price, quantity);
//Prompting the user to enter the details of item1
System.out.println("Item1");
System.out.println("Enter the item name:");
itemName= sc.nextLine();
//storing the item name in the ItemToPurchase.java object item1 using setters(mutators)
item1.setItemName(itemName);
System.out.println("Enter the item price:");
price = sc.nextInt();
//storing the item price in the ItemToPurchase.java object item1 using setters(mutators)
item1.setItemPrice(price);
System.out.println("Enter the item quantity");
quantity = sc.nextInt();
//storing the item quantity in the ItemToPurchase.java object item1 using setters(mutators)
item1.setItemQuantity(quantity);
// calling sc.nextLine() to allow the user to input a new string
sc.nextLine();
//Prompting the user to enter the details of item2
System.out.println("Item2");
System.out.println("Enter the item name:");
itemName= sc.nextLine();
//storing the item name in the ItemToPurchase.java object item1 using setters(mutators)
item2.setItemName(itemName);
System.out.println("Enter the item price:");
price = sc.nextInt();
//storing the item price in the ItemToPurchase.java object item1 using setters(mutators)
item2.setItemPrice(price);
System.out.println("Enter the item quantity");
quantity = sc.nextInt();
//storing the item quantity in the ItemToPurchase.java object item1 using setters(mutators)
item2.setItemQuantity(quantity);
//calculating the total value of item1 and item2
//getting the value of itemprice and itemquantity from ItemToPurchase.java using accessors
int item1total = item1.getItemPrice()*item1.getItemQuantity();
int item2total = item2.getItemPrice()*item2.getItemQuantity();
int total = item1total+item2total;
//Printing the Output with total cost occured
System.out.println("TOTAL COST");
System.out.println(item1.getItemName()+" "+item1.getItemQuantity()+" @ $"+item1.getItemPrice()+" = $"+item1total);
System.out.println(item2.getItemName()+" "+item2.getItemQuantity()+" @ $"+item2.getItemPrice()+" = $"+item2total);
//to create an extra line space as shown in question
System.out.println("");
System.out.println("Total: $"+total);
}
}
Output:
Item1
Enter the item name:
itemA
Enter the item price:
4
Enter the item quantity
9
item2
Enter the item name:
itemB
Enter the item price:
3
Enter the item quantity
7
TOTAL COST
itemA 9 @ $4 = $36
itemB 7 @ $3 = $21
Total: $57
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.