My code: public class ItemToPurchase { private String itemName; private int itemPrice; private int itemQuanity; public ItemToPurchase()
Question:
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())));
}
}
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates