5.12 Online shopping cart (Java) Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class files. To switch files, look for where it says "Current File" at the top of the developer window. Click the current file name, then select the file you need. (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 Here's the code I put in for ItemToPurchase.java public class ItemToPurchase { //Private fields - itemName, itemPrice, and itemQuanity private String itemName; private int itemPrice; private int itemQuantity; //Default Constructor public ItemToPurchase() { this.itemName = "none"; this.itemPrice = 0; this.itemQuantity = 0; } //public ItemToPurchase(String itemName, int itemPrice, int itemQuantity) { //this.itemName = itemName; //this.itemPrice = itemPrice; //this.itemQuantity = itemQuantity; //public member methods (mutators & accessors) //setName() & getName() public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } //setPrice() & getPrice() public int getItemPrice() { return itemPrice; } public void setItemPrice(int itemPrice) { this.itemPrice = itemPrice; } //setQuantity() & getQuantity() public int getItemQuantity() { return itemQuantity; } public void setItemQuantity(int itemQuantity) { this.itemQuantity = itemQuantity; } } Heres the code I used for ShoppingCartPrinter.java import java.util.Scanner; public class ShoppingCartPrinter { public static void main(String[] args) { ItemToPurchase item1 = new ItemToPurchase(); ItemToPurchase item2 = new ItemToPurchase(); Scanner scnr = new Scanner(System.in); // Get item 1 details from user, create itemToPurchase object System.out.println("Item 1"); System.out.println("Enter the item name: "); item1.setItemName(scnr.nextLine()); System.out.println("Enter the item price: "); item1.setItemPrice(scnr.nextInt()); System.out.println("Enter the item quantity: "); item1.setItemQuantity(scnr.nextInt()); System.out.println(); scnr.nextLine(); // Get item 2 details from user, create itemToPurchase object System.out.println("Item 2"); System.out.println("Enter the item name: "); item2.setItemName(scnr.nextLine()); System.out.println("Enter the item price: "); item2.setItemPrice(scnr.nextInt()); System.out.println("Enter the item quantity: "); System.out.println(); item2.setItemQuantity(scnr.nextInt()); // Add costs of two items and print total // cartTotal = item one price + item two price // Total Cost // item one information // item two information // Total output int totalItem1 = item1.getItemPrice() * item1.getItemQuantity(); int totalItem2 = item2.getItemPrice() * item2.getItemQuantity(); int total = totalItem1 + totalItem2; System.out.println("TOTAL COST"); System.out.println(item1.getItemName() + " " + item1.getItemQuantity() + " @ $" + item1.getItemPrice() + " = $" + totalItem1); System.out.println(item2.getItemName() + " " + item2.getItemQuantity() + " @ $" + item2.getItemPrice() + " = $" + totalItem2); System.out.println(); System.out.println("Total: $" + total); } } When I used this code, I got some points but some tests I didn't pass and I need to know how to fix my code to pass those tests. Here are two of the tests I did not pass.
5.12 Online shopping cart (Java)
Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class files. To switch files, look for where it says "Current File" at the top of the developer window. Click the current file name, then select the file you need.
(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:
//Private fields - itemName, itemPrice, and itemQuanity
private String itemName;
private int itemPrice;
private int itemQuantity;
//Default Constructor
public ItemToPurchase() {
this.itemName = "none";
this.itemPrice = 0;
this.itemQuantity = 0;
}
//public ItemToPurchase(String itemName, int itemPrice, int itemQuantity) {
//this.itemName = itemName;
//this.itemPrice = itemPrice;
//this.itemQuantity = itemQuantity;
//public member methods (mutators & accessors)
//setName() & getName()
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
//setPrice() & getPrice()
public int getItemPrice() {
return itemPrice;
}
public void setItemPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
//setQuantity() & getQuantity()
public int getItemQuantity() {
return itemQuantity;
}
public void setItemQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
}
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
Scanner scnr = new Scanner(System.in);
// Get item 1 details from user, create itemToPurchase object
System.out.println("Item 1");
System.out.println("Enter the item name: ");
item1.setItemName(scnr.nextLine());
System.out.println("Enter the item price: ");
item1.setItemPrice(scnr.nextInt());
System.out.println("Enter the item quantity: ");
item1.setItemQuantity(scnr.nextInt());
System.out.println();
scnr.nextLine();
// Get item 2 details from user, create itemToPurchase object
System.out.println("Item 2");
System.out.println("Enter the item name: ");
item2.setItemName(scnr.nextLine());
System.out.println("Enter the item price: ");
item2.setItemPrice(scnr.nextInt());
System.out.println("Enter the item quantity: ");
System.out.println();
item2.setItemQuantity(scnr.nextInt());
// Add costs of two items and print total
// cartTotal = item one price + item two price
// Total Cost
// item one information
// item two information
// Total output
int totalItem1 = item1.getItemPrice() * item1.getItemQuantity();
int totalItem2 = item2.getItemPrice() * item2.getItemQuantity();
int total = totalItem1 + totalItem2;
System.out.println("TOTAL COST");
System.out.println(item1.getItemName() + " " + item1.getItemQuantity() + " @ $" + item1.getItemPrice() + " = $" + totalItem1);
System.out.println(item2.getItemName() + " " + item2.getItemQuantity() + " @ $" + item2.getItemPrice() + " = $" + totalItem2);
System.out.println();
System.out.println("Total: $" + total);
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images