FIX ERROR JAVA CODE: (Issues seem to be with spacing and output, not syntax) My
ID: 3725362 • Letter: F
Question
FIX ERROR JAVA CODE: (Issues seem to be with spacing and output, not syntax)
My code:
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enter the item name:");
String name1 = scan.nextLine();
System.out.println("Enter the item price:");
int price1 = scan.nextInt();
System.out.println("Enter the item quantity:");
int quantity1 = scan.nextInt();
item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scan.nextLine();
System.out.println("Item 2");
System.out.println("Enter the item name: ");
String name2 = scan.nextLine();
System.out.println("Enter the item price: ");
int price2 = scan.nextInt();
System.out.println("Enter the item quantity: ");
int quantity2 = scan.nextInt();
item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);
System.out.println();
System.out.println("TOTAL COST");
int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();
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));
}
}
: Compare output 0/2 Output is nearly correct; but whitespace differs. See yellow highlights below. Chocolate Chips Input Bottled Water 10 Item 1 Enter the item name: Enter the item price Your output startsEnter the item quantity: with Item 2 Enter the item name: Enter the item price Enter the item quantity: Item 1 Enter the item name: Enter the item price Enter the item quantity: Expected output starts with Item 2 Enter the item name: Enter the item price Enter the item quantity: Special character legendExplanation / Answer
public class ItemToPurchase {
private String name;
private int price;
private int quantity;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the price
*/
public int getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(int price) {
this.price = price;
}
/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}
/**
* @param quantity the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
import java.util.Scanner;
public class ShoppingCartPrinter {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ItemToPurchase item1 = new ItemToPurchase();
ItemToPurchase item2 = new ItemToPurchase();
System.out.println("Item 1");
System.out.println("Enter the item name:");
String name1 = scan.nextLine();
System.out.println("Enter the item price:");
int price1 = scan.nextInt();
System.out.println("Enter the item quantity:");
int quantity1 = scan.nextInt();
item1.setName(name1);
item1.setPrice(price1);
item1.setQuantity(quantity1);
scan.nextLine();
scan.nextLine();
System.out.println("Item 2");
System.out.println("Enter the item name:");
String name2 = scan.nextLine();
System.out.println("Enter the item price:");
int price2 = scan.nextInt();
System.out.println("Enter the item quantity:");
int quantity2 = scan.nextInt();
item2.setName(name2);
item2.setPrice(price2);
item2.setQuantity(quantity2);
System.out.println();
System.out.println("TOTAL COST");
int item1Total = item1.getPrice() * item1.getQuantity();
int item2Total = item2.getPrice() * item2.getQuantity();
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));
}
}
Ssample Output;
Item 1
Enter the item name:
Choclate 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
TOTAL COST
Choclate chips 1 @ $3 = $3
Bottled water 10 @ $1 = $10
Total: $13
Please Provide FeedBack or Thumbs up.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.