この練習では、ショッピングカートをアイテムの配列として実装するクラスを完成させます。 Item.javaファイルには、購入するアイテムをモデル化したItemという名前のクラスの定義が含まれています。アイテムには名前、価格、数量(購入数量)があります。ファイルShoppingCart.javaはショッピングカートをItemオブジェクトの配列として実装します。私が受け取ったエラーの処理方法が分かりません。これは私のショッピングカートプロジェクトです
次のようにしてのShoppingCartクラスを完了します。インスタンス変数cartをItemの配列に宣言し、コンストラクタ内でカートをインスタンス化してcapacity項目を保持する配列にします。 b。 increaseSizeメソッドのコードを入力します。コードはリスト7.8のテキストと似ているはずですが、サイズを2倍にする代わりに、3つの要素だけ増やしてください。 c。 addToCartメソッドのコードを入力します。このメソッドは、アイテムをカートに追加し、totalPriceインスタンス変数を更新する必要があります(この変数は量を考慮しています)。 d。クラスをコンパイルします。
ショッピングをシミュレートするプログラムを作成します。プログラムは、ユーザーが買い物をしたい限り継続するループを持つ必要があります。ループのたびに、ユーザーがカートに追加したいアイテムの名前、価格、数量を読み込みます。アイテムをカートに追加した後、カートの内容を印刷する必要があります。ループの後にカート内の品目の合計金額を「お支払いください...」というメッセージが表示されます。スレッド内
package Shopping; import java.text.NumberFormat; public class Item { private String name; private double price; private int quantity; // ----------------------------------------------------- -- // Create a new item with the given attributes. // ----------------------------------------------------- -- public Item (String itemName, double itemPrice, int numPurchased) { name = itemName; price = itemPrice; quantity = numPurchased; } // ----------------------------------------------------- -- // Return a string with the information about the item // ----------------------------------------------------- -- public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t" + fmt.format(price*quantity)); } // ----------------------------------------------- // Returns the unit price of the item // ----------------------------------------------- public double getPrice() { return price; } // ----------------------------------------------- // Returns the name of the item // ----------------------------------------------- public String getName() { return name; } // ----------------------------------------------- // Returns the quantity of the item // ----------------------------------------------- public int getQuantity() { return quantity; } } package Shopping; import Shopping.Item; import java.text.NumberFormat; public class ShoppingCart { private int itemCount; // total number of items in the cart private double totalPrice; // total price of items in the cart private int capacity; // current cart capacity Item[] cart; // declare an instance variable cart for an array of Item // --------------------------------------------------------- // Creates an empty shopping cart with a capacity of 5 items. // --------------------------------------------------------- public ShoppingCart() { capacity = 5; itemCount = 0; totalPrice = 0.0; cart = new Item[capacity]; } // ----------------------------------------------------- // Adds an item to the shopping cart. // ----------------------------------------------------- public void addToCart(String itemName, double price, int quantity) { if (itemCount > 5) { System.out.println("Now the shopping cart is full."); } else { addToCart(itemName, price, quantity); totalPrice = totalPrice + (price * quantity); } itemCount = itemCount+1; } // ----------------------------------------------------- // Returns the contents of the cart together with // summary information. // ----------------------------------------------------- public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String contents = "\nShopping Cart\n"; contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n"; for (int i = 0; i < itemCount; i++) contents += cart[i].toString() + "\n"; contents += "\nTotal Price: " + fmt.format(totalPrice); contents += "\n"; return contents; } // ----------------------------------------------------- // Increases the capacity of the shopping cart by 3 // ----------------------------------------------------- private void increaseSize() { capacity = capacity + 3; } }
例外Shopping.ShoppingCart.addToCartで "メイン" によってjava.lang.StackOverflowError (ShoppingCart.java:39) は、私はあなたが呼び出し続ける
をご 'addToCart'方法を見てみましょう、あなたは再帰的にstackOverflowErrがどこから来ているでは何も変更せずに(他のブロックから)同じメソッドを呼び出します。おそらくaddToCartか何かを呼び出す前にitemCountをインクリメントすることを意味します。 – user1875195
Heh ... 'StackOverflowError' ... StackOverflow ...これは面白くないです。 – XavCo7