2017-10-16 112 views
-1

我很難搞清楚爲什麼這個代碼不會在「總成本」的最後加起來。在線購物車java

兩類:

ShoppingCartPrinter.java和ItemToPurchase.java

我不斷收到的結果都是0在 「總成本」。任何幫助將不勝感激。謝謝。

ShoppingCartPrinter.java代碼:

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

ItemToPurchase.java

public class ItemToPurchase { 
    private String itemName; 
    private int itemPrice; 
    private int itemQuantity; 

public ItemToPurchase() { 
    itemName = "none"; 
    itemPrice = 0; 
    itemQuantity = 0; 
    return; 
} 

public void setName(String name) { 
    itemName = name; 
    return; 
} 

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

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

public String getName() { 
    return itemName; 
} 

public int getPrice() { 
    return itemPrice; 
} 

public int getQuantity() { 
    return itemQuantity; 
} 

public void printItemPurchase() { 
    System.out.println(itemQuantity + " " + itemName + " $" + itemPrice + " = $" + (itemPrice * itemQuantity)); 
} 
} 
+1

在您的問題中添加代碼而不是鏈接。 – skw

+0

'public void setPrice(int price){ itemPrice = 0;/*這裏你應該使用「價格」*/ return; } '我不明白爲什麼你對你的0 – 2017-10-16 11:16:34

回答

1

的問題是在你的setters。您正在爲項目分配0(零)。

itemPrice = price; 

itemQuantity = quantity; 

而且,你並不需要在該return聲明:

itemPrice = 0; 

itemQuantity = 0; 

這可以通過做這樣的事情被固定每種方法的結束od具有void返回類型,特別是在ItemToPurchase()構造函數中。你可以安全地刪除它們。