In: Computer Science
I need this code in java.
Task
(1) Create two files to submit:
Build the ItemToPurchase class with the following specifications:
(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
This is the given code so far:
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;
// TODO Create new item1 and new item2
// Prompt for item 1 details from user, create itemToPurchase
object
System.out.println("Item 1");
System.out.println("Enter the item name:");
productName = scnr.nextLine();
System.out.println("Enter the item price:");
productPrice = scnr.nextInt();
System.out.println("Enter the item quantity:");
productQuantity = scnr.nextInt();
System.out.println("");
// TODO: Set item1 fields here
// Promptr for item 2 details from user, create itemToPurchase
object
System.out.println("Item 2");
System.out.println("Enter the item name:");
scnr.nextLine(); // DO NOT OMIT THIS LINE
productName = scnr.nextLine();
System.out.println("Enter the item price:");
productPrice = scnr.nextInt();
System.out.println("Enter the item quantity:");
productQuantity = scnr.nextInt();
System.out.println("");
// TODO set item2 here
// Add costs of two items and print total
cartTotal = (item1.getPrice() * item1.getQuantity()) +
(item2.getPrice() * item2.getQuantity());
System.out.println("TOTAL COST");
System.out.println(item1.getName() + " " + item1.getQuantity()
+
" @ $" + item1.getPrice() + " = $" +
(item1.getPrice() * item1.getQuantity()));
System.out.println(item2.getName() + " " + item2.getQuantity()
+
" @ $" + item2.getPrice() + " = $" +
(item2.getPrice() * item2.getQuantity()));
System.out.println("");
System.out.println("Total: $" + cartTotal);
return;
}
}
ItemToPurchase.java :
//Java class
public class ItemToPurchase {
//private variables
private String itemName;
private int itemPrice;
private int itemQuantity;
//Constructor
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
//getter method
public String getName() {
return itemName;
}
//setter method
public void setName(String in) {
this.itemName = in;
}
public int getPrice() {
return itemPrice;
}
public void setPrice(int ip) {
this.itemPrice = ip;
}
public int getQuantity() {
return itemQuantity;
}
public void setQuantity(int iq) {
this.itemQuantity = iq;
}
}
========================
ShoppingCartPrinter.java :
package sample;
import java.util.Scanner;//java class
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;
// TODO Create new item1 and new item2
ItemToPurchase item1 = new
ItemToPurchase();
ItemToPurchase item2 = new
ItemToPurchase();
// Prompt for item 1 details from user, create itemToPurchase
object
System.out.println("Item 1");
System.out.println("Enter the item
name:");
productName = scnr.nextLine();
System.out.println("Enter the
item price:");
productPrice = scnr.nextInt();
System.out.println("Enter the
item quantity:");
productQuantity =
scnr.nextInt();
System.out.println("");
// TODO: Set item1 fields here
item1.setName(productName);
item1.setPrice(productPrice);
item1.setQuantity(productQuantity);
// Promptr for item 2 details from user, create itemToPurchase
object
System.out.println("Item 2");
System.out.println("Enter the item
name:");
scnr.nextLine(); // DO NOT OMIT
THIS LINE
productName = scnr.nextLine();
System.out.println("Enter the
item price:");
productPrice = scnr.nextInt();
System.out.println("Enter the
item quantity:");
productQuantity =
scnr.nextInt();
System.out.println("");
// TODO set item2 here
item2.setName(productName);
item2.setPrice(productPrice);
item2.setQuantity(productQuantity);
// Add costs of two items and print total
cartTotal = (item1.getPrice() *
item1.getQuantity()) + (item2.getPrice() *
item2.getQuantity());
System.out.println("TOTAL
COST");
System.out.println(item1.getName()
+ " " + item1.getQuantity() + " @ $" + item1.getPrice() + " =
$"
+ (item1.getPrice() *
item1.getQuantity()));
System.out.println(item2.getName()
+ " " + item2.getQuantity() + " @ $" + item2.getPrice() + " =
$"
+ (item2.getPrice() *
item2.getQuantity()));
System.out.println("");
System.out.println("Total: $" +
cartTotal);
return;
}
}
**************************************
Output :