简体   繁体   中英

Online shopping cart java

I'm having a hard time figuring out why this code is not adding up at the end in "TOTAL COST".

Any help would be greatly appreciated. Thanks.

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();                                         // Sets the variable productName for user input

  System.out.println("Enter the item price: ");
  productPrice = scnr.nextInt();                                         // Set the variable productPrice for user input

  System.out.println("Enter the item quantity: ");
  productQuantity = scnr.nextInt();                                      // Set the variable productQuantity for user user input
  System.out.println("");

  item1.setName(productName);
  item1.setPrice(productPrice);
  item1.setQuantity(productQuantity);

  System.out.println("Item 2");
  System.out.println("Enter the item name: ");
  scnr.nextLine();
  productName = scnr.nextLine();                                         // Set the variable productName for user input

  System.out.println("Enter the item price: ");
  productPrice = scnr.nextInt();                                         // Set the variable productPrice for user input

  System.out.println("Enter the item quantity: ");
  productQuantity = scnr.nextInt();                                      // Set the variable productQuantity for user input
  System.out.println("");

  item2.setName(productName);
  item2.setPrice(productPrice);
  item2.setQuantity(productQuantity);

  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;
   }
}

The problem is in your setters . You are assigning 0 (zero) to the items.

itemPrice = 0;

and

itemQuantity = 0;

This can be fixed by doing something like:

itemPrice = price;

and

itemQuantity = quantity;

Also, you do not need the return statements at the end of each method that has a void return type, especially in the ItemToPurchase() constructor. You can safely remove them.

The zybooks assignment applies the setters automatically, it is not by choice. there is only certain parts of code that can be edited. I am currently working on this project and have been running into problems as well. Just wanted to clarify its not the coders fault they are set to 0

@skw is correct, the mutator methods should not be set to 0:

public void setPrice(int price) {
    itemPrice = price;
}

public void setQuantity (int quantity) {
    itemQuantity = quantity;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM