Question
My code: public class ItemToPurchase { private String itemName; private int itemPrice; private int itemQuanity; public ItemToPurchase() { itemName = none; itemPrice = 0; itemQuantity
My code:
public class ItemToPurchase {
private String itemName;
private int itemPrice;
private int itemQuanity;
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
public ItemToPurchase(String itemName, int itemPrice, int itemQuanity){
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemQuantity = itemQuantity;
}
public void setName(String Name){
itemName = Name;
}
public String getName() {
return itemName;
}
public void setPrice(int Price){
itemPrice = Price;
}
public int getPrice() {
return itemPrice;
}
public void setQuanity(int Quanity) {
itemQuanity = Quanity;
}
public int getQuanity() {
return itemQuanity;
}
public void printItemPurchase() {
System.out.println(itemQuantity + " " + itemName + " $" + itemPrice +
" = $" + (Price * Quantity));
}
}
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int i = 0;
String productName;
int productPrice = 0;
int productQuantity = 0;
int cartTotal = 0;
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
ItemToPurchase1.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextLine();
ItemToPurchase1.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuanity = scnr.nextLine();
ItemToPurchase1.setQuanity(productQuanity);
System.out.println();
scnr.nextLine();
System.out.println("Item 2");
System.out.println("Enter the item name: ");
productName = scnr.nextLine();
ItemToPurchase2.setName(productName);
System.out.println("Enter the item price: ");
productPrice = scnr.nextLine();
itemToPurchase2.setPrice(productPrice);
System.out.println("Enter the item quantity: ");
productQuanity = scnr.nextLine();
ItemToPurchase2.setQuanity(productQuanity);
System.out.println();
cartTotal = (item1.getPrice() * item1.getQuantity()) + (item2.getPrice() * item2.getQuantity());
System.out.println("Total Cost");
ItemToPurchase1.printItemPurchase();
ItemToPurchase2.printItemPurchase();
System.out.println("Total: $" + ((ItemToPurchase1.getPrice() * ItemToPurchase1.getQuantity()) + (ItemToPurchase2.getPrice() * ItemToPurchase2.getQuantity())));
}
}
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: Item ToPurchase.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 Item ToPurchase 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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started