2017-06-28 19 views
0

計算された出力の代わりに、私が探している実際の合計印刷出力なしで「null」を取得します。私はItemが適切に定義されていないからだと思っています。無視してshop.txtは、それは私の問題には影響しませんコンパイルされますが、合計が計算されません。

package Shopping; 
import java.util.ArrayList; 
import java.util.Scanner; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.text.NumberFormat; 
public class Shop 
{ 
    public static void main(String[] args) 
    { 
     //I/O stream 

     String fileName = "shop.txt"; 
     Scanner inputStream = null; 
     System.out.println("The file " + fileName + 
       "\ncontains the following lines:\n"); 
     try 
     { 
      inputStream = new Scanner(new File(fileName)); 
     } 
     catch (FileNotFoundException e) 
     { 
      System.out.println("This is a shopping list " + 
        fileName); 
      System.exit(0); 
     } 
     while (inputStream.hasNextLine()) 
     { 
      String line = inputStream.nextLine(); 
      System.out.println(line); 
     } 
     inputStream.close(); 


     ArrayList<Item> Cart = new ArrayList<Item>(); 

     Item item; 
     String itemName; 
     double itemPrice; 
     int quantity; 
     double totalPrice = 0.0; 
     double sum = 0.0; 
     int priority; 

     Scanner scan = new Scanner(System.in); 
     String continueShopping = "y"; 
     do 
     { 
      System.out.print("Enter the name of the item: "); 
      itemName = scan.nextLine(); 
      System.out.print("Enter the unit price: "); 
      itemPrice = scan.nextDouble(); 
      System.out.print("Enter the quantity: "); 
      quantity = scan.nextInt(); 


      // create a new item and add it to the cart 

      item = new Item(itemName, itemPrice, quantity); 
      Cart.add(item); 

      for (int i = 0; i < Cart.size(); i++) 
      { 
       Item itm = Cart.get(i); 
       System.out.println(itm); 
      } 
      // Print out the results 

      System.out.print("Continue shopping (y/n)? "); 
      scan.nextLine(); 
      continueShopping = scan.nextLine(); 
     } 
     while (continueShopping.equals("y")); 
     for (int i = 0; i < Cart.size(); i++) 
     { 
      Item itm = Cart.get(i); 
      System.out.println(itm); 
      totalPrice = itm.getQuantity() * itm.getPrice(); 
      sum += totalPrice; 
     } 
     NumberFormat type = NumberFormat.getCurrencyInstance(); 
     System.out.println("The total price is: " + type.format(sum)); 
    } 
} 

Itemクラス

package Shopping; 
import java.text.NumberFormat; 

public class Item 
{ 

    private String name; 
    private double price; 
    private int quantity; 


    public Item(String itemName, double itemPrice, int quantity2) 
    { 
    } 

    public void Item(String itemName, double itemPrice, int numPurchased) 
    { 
     name = itemName; 
     price = itemPrice; 
     quantity = numPurchased; 
    } 

    //Info about the item 

    public String toString() 
    { 
     NumberFormat type = NumberFormat.getCurrencyInstance(); 

     return (name + "\t" + type.format(price) + "\t" + quantity + "\t" 
       + type.format(price * quantity)); 
    } 

    //Retrieve the item price 

    public double getPrice() 
    { 
     return price; 
    } 

    //Retrieve item name 

    public String getName() 
    { 
     return name; 
    } 

    //Retrieve quantity 

    public int getQuantity() 
    { 
     return quantity; 
    } 
} 
+0

"shop.txt"からいくつかのタプルを与えることはできますか? –

+0

私はそれを取り除くことができ、同じ方法で出力します。それは必要ではない、ちょうどそこに - 無視して –

答えて

2

あなたItemクラスがあるが、1つの空のコンストラクタ

public Item(String itemName, double itemPrice, int quantity2) { 
} 

と呼ばれる方法Item

public void Item (String itemName, double itemPrice, int numPurchased) 
{ 
    name = itemName; 
    price = itemPrice; 
    quantity = numPurchased; 
} 

空のコンストラクタを削除し、戻り値の型voidをコンストラクタに変換するメソッドから削除します。

+0

ありがとう!良いキャッチ –

+0

はい、それは問題でした –

+0

それはちょっとしたことです! @Matthew H:あなたのIDEフラグには、メソッドの名前 'Item(String itemName、double itemPrice、int numPurchased)'が_against java coding standard_または "このメソッドにコンストラクタ名を持っていますか?ところで、コンストラクタでのみ設定した変数を最終的に作ることは、そのような問題を検出するのに役立ちます。 – sruetti

関連する問題