我一直在研究我的購物車計劃,但我一直在輸入itemTax時遇到問題,並且將新項目添加到購物車中,我不知道我做了什麼錯誤。購物車計劃
類項目:
public class Item {
private int id;
private String name;
private double price;
private String description;
private int quantity;
private double tax;
public Item (int itemID, String itemName, double itemPrice, String itemDescription, int itemQuantity, double itemTax){
id = itemID;
name = itemName;
price = itemPrice;
description = itemDescription;
quantity = itemQuantity;
tax = itemTax;
}
public int getID(){
return id;
}
public String getName(){
return name;
}
public double getPrice(){
return price;
}
public String getDescription(){
return description;
}
public int getQuantity(){
return quantity;
}
public double getTax(){
return tax;
}
}
類車:
import java.util.Scanner;
public class Cart {
private int itemCount;
private double totalPrice;
private static int capacity;
private static Item[] cart = new Item[capacity];
public Cart(){
itemCount = 10;
totalPrice = 0.0;
capacity = 0;
}
public void add(int itemID, String itemName, double itemPrice, String itemDescription, int itemQuantity, double itemTax){
Item item = new Item(itemID, itemName, itemPrice, itemDescription, itemQuantity, itemTax);
totalPrice += (itemPrice * itemQuantity);
cart[itemCount] = item;
itemCount += 1;
if(itemCount==capacity)
{
increaseSize();
}
}
public static void remove(String itemName){
Scanner s = new Scanner(System.in);
for (int i = 0; i < cart.length; i++) {
Item remove = (Item) cart.get(i);
if (itemName.equals(remove.getName())) {
cart.remove(i);
}
}
System.out.println("\n" + "Item " + itemName + " wasn't found.");
}
private void increaseSize()
{
Item[] item = new Item[capacity+5];
for(int i=0; i < capacity; i++)
{
item[i] = cart[i];
}
cart = item;
item = null;
capacity = cart.length;
}
public static void prLine (int itemID, String itemName, int itemQuantity, double itemPrice, double total, double itemTax) {
System.out.printf("\n%-10.10d %30s %10.2f %10d %10.2f", itemID, itemName, itemPrice, itemQuantity, itemTax, total);
}
public static void prTitles() {
System.out.printf("\n%-10s 30% %10s %10s %10s %10s", "ID", "Item", "Price", "Quantity", "Tax", "Total");
}
}
類店:
import java.util.ArrayList;
import java.util.Scanner;
public class Shop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Item> cart = new ArrayList<Item>();
Item item;
int itemID;
String itemName;
double itemPrice;
String itemDescription;
int itemQuantity;
double itemTax;
int ch;
String choice;
Cart shoppingCart = new Cart();
while (true) {
System.out.println("Menu:");
System.out.println("0) Exit " + "\n"
+ "1) Add item in shopping cart" + "\n"
+ "2) Remove item from shpping cart");
ch = sc.nextInt();
switch (ch) {
case 0:
System.out.println("\n" + "Good bye!");
System.exit(0);
case 1:
System.out.println("Enter item ID: ");
itemID = sc.nextInt();
System.out.println("Enter item name: ");
itemName = sc.next();
System.out.println("Enter item price: ");
itemPrice = sc.nextDouble();
System.out.println("Enter short description of item: ");
itemDescription = sc.next();
System.out.println("Enter quantity: ");
itemQuantity = sc.nextInt();
System.out.println("Enter tax rate:");
itemTax = sc.nextDouble();
shoppingCart.add(itemID, itemName, itemPrice, itemDescription, itemQuantity, itemTax);
break;
case 2:
System.out.println("Enter name of the item that you would like to remove: ");
choice = sc.next();
shoppingCart.remove(choice);
break;
}
}
}
}
您可否詳細說明pblm。 – Kick
當我輸入itemTax的十進制數時,我得到:線程「main」中的異常java.util.InputMismatchException \t at java.util.Scanner.throwFor(Unknown Source) \t at java.util.Scanner.next(Unknown Source) \t在java.util.Scanner.nextDouble(來源不明) \t在en.Shop.main(Shop.java:52),但是當我進入尋常號我得到:異常線程「main」 java.lang.ArrayIndexOutOfBoundsException :10 at en.Cart.add(Cart.java:21) at en.Shop.main(Shop.java:55) – user3023250
最好是[編輯]問題以添加新的信息。另外,請檢查如何製作[最小示例](http://stackoverflow.com/help/mcve)。 – brasofilo