これが私の主な方法です。何らかの理由で、ショッピングカート クラスが見つからず、エラーが発生します。
package goshopping;
import java.util.Scanner;
public class Shopping
{
static final Scanner scanner = new Scanner(System.in);
public static void main (String[] args)
{
ShoppingCart cart=new ShoppingCart();
String name;
double price;
int quantity;
String shopMore;
do
{
System.out.print("Please enter the name of item: ");
name=scanner.nextLine();
System.out.print("Please enter the price of the item: ");
price=scanner.nextDouble();
scanner.nextLine();
System.out.print("Please enter the quantity of the item: ");
quantity=scanner.nextInt();
scanner.nextLine();
cart.addToCart(name, price, quantity);
System.out.println(cart.toString());
System.out.print("Shop some more? Enter Y for yes or N for no ");
shopMore=scanner.nextLine();
}
while(shopMore.charAt(0)=='Y'||shopMore.charAt(0)=='y');
}
}
これは私の 2 番目のクラス ShoppingCart.java で、変数が見つからないと言っていますが、既にインスタンス化しています....
package shoppingcart;
// **********************************************************************
// ShoppingCart.java
//
// Represents a shopping cart as an array of items
// **********************************************************************
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
private Item[] cart;
// -----------------------------------------------------------
// 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)
{
cart[itemCount]=new Item(itemName,price,quantity);
totalPrice+=price;
itemCount++;
if(itemCount==capacity)
{
increaseSize();
}
}
// -------------------------------------------------------
// 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()
{
Item[] temp=new Item[capacity+3];
System.arraycopy(cart, 0, temp, 0, capacity);
cart=temp;
}
}
これは、Item.java という名前の 3 番目のクラスです。このクラスは問題なく、ここにエラーはありません。
package item;
// ***************************************************************
// Item.java
//
// Represents an item in a shopping cart.
// ***************************************************************
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;
}
}