Please, can anyone look at this code and tell me where the mistake is? The first
ID: 3708779 • Letter: P
Question
Please, can anyone look at this code and tell me where the mistake is? The first three answers compute correctly.
ItemToPurchase:
public class ItemToPurchase {
//declaring the private variables
private String itemName;
private int itemPrice;
private int itemQuantity;
//Default constructor with fields
public ItemToPurchase() {
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
}
//Access mutators for the variables
public String getName() {
return itemName;
}
public void setName(String itemName) {
this.itemName = itemName;
}
public int getPrice() {
return itemPrice;
}
public void setPrice(int itemPrice) {
this.itemPrice = itemPrice;
}
public int getQuantity() {
return itemQuantity;
}
public void setQuantity(int itemQuantity) {
this.itemQuantity = itemQuantity;
}
}
ShoppingCartPrinter:
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
// Initialize scan
Scanner scan = new Scanner(System.in);
// Get items from item list
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
// Print out to screen
System.out.println("Item 1");
System.out.print("Enter the item name: ");
String name1 = scan.nextLine();
System.out.print("Enter the item price: ");
// Variable for price
int price1 = scan.nextInt();
System.out.print("Enter the item quantity: ");
// Variable for quantity
int quantity1 = scan.nextInt();
item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scan.nextLine();
// Print to screen
System.out.println("Item 2");
System.out.print("Enter the item name: ");
String name2 = scan.nextLine();
System.out.print("Enter the item price: ");
// Store price 2
int price2 = scan.nextInt();
System.out.print("Enter the item quantity: ");
// Store quantity 2
int quantity2 = scan.nextInt();
item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);
System.out.println();
int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();
// Print to screen
System.out.println(item1.getName() + " " + item1.getQuantity() + " for $" + item1.getPrice() + " = $" + item1Total);
System.out.println(item2.getName() + " " + item2.getQuantity() + " for $" + item2.getPrice() + " = $" + item2Total);
System.out.println();
System.out.println("Total: $" + (item1Total + item2Total));
}
}
1. Unit test 2/2 Tests item.setName("Chocolate Chips) and item.GetName0"Chocolate Chips Your output Item name "Chocolate Chips" correctly set and is accessible 2. Unit test 2/2 Tests item.setPrice(3) and item.getPrice3 Your output Item price $3 correctly set and is accessible. 3. Unit test 2/2 Tests item.setQuantity(4) and item.getQuantity04Explanation / Answer
import java.util.Scanner; public class ShoppingCartPrinter { public static void main(String[] args) { // Initialize scan Scanner scan = new Scanner(System.in); // Get items from item list ItemToPurchase item1 = new ItemToPurchase(); ItemToPurchase item2 = new ItemToPurchase(); // Print out to screen System.out.println("Item 1"); System.out.println("Enter the item name: "); String name1 = scan.nextLine(); System.out.println("Enter the item price: "); // Variable for price int price1 = scan.nextInt(); // Variable for quantity System.out.println("Enter the item quantity: "); int quantity1 = scan.nextInt(); item1.setName(name1); item1.setPrice(price1); item1.setQuantity(quantity1); scan.nextLine(); // Print to screen System.out.println(" Item 2"); System.out.println("Enter the item name: "); String name2 = scan.nextLine(); System.out.println("Enter the item price: "); // Variable for price int price2 = scan.nextInt(); System.out.println("Enter the item quantity: "); // Variable for quantity int quantity2 = scan.nextInt(); item2.setName(name2); item2.setPrice(price2); item2.setQuantity(quantity2); System.out.println(); int item1Total = item1.getPrice() * item1.getQuantity(); int item2Total = item2.getPrice() * item2.getQuantity(); // Print to screen System.out.println(item1.getName() + " " + item1.getQuantity() + " @ $" + item1.getPrice() + " = $" + item1Total); System.out.println(item2.getName() + " " + item2.getQuantity() + " @ $" + item2.getPrice() + " = $" + item2Total); System.out.println(); System.out.println("Total: $" + (item1Total + item2Total)); } }