Java ArrayList和类

时间:2016-10-26 18:04:37

标签: java arrays arraylist java.util.scanner

我正试图通过printMenu(cart, scnr).

但是,例如'a'>代码打印
    输入项目名称:     输入项目描述:

在取项目名称之前!!!!

提前谢谢。

这是主要课程:

    import java.util.Scanner;

    public class ShoppingCartManager {
        public static void main(String[] args) {

            Scanner scnr = new Scanner(System.in);

            String customerName = "none";
            String currentDate = "January 1, 2016";

            System.out.println("Enter Customer's Name: ");
            customerName = scnr.nextLine();

            System.out.println("Enter Today's Date: ");
            currentDate = scnr.nextLine();

            ShoppingCart cart = new ShoppingCart(customerName, currentDate);

            System.out.println("Customer Name: " + cart.getCustomerName());
            System.out.println("Today's Date: " + cart.getDate());
            printMenu(cart, scnr);

        }

        public static void printMenu(ShoppingCart cart, Scanner scnr) {
            ItemToPurchase cartItems = new ItemToPurchase();

            char userChoice = '?';
                System.out.println("");

                System.out.print("MENU" + "\na - Add item to cart" + "\nd - Remove item from cart"
                                    + "\nc - Change item quantity" + "\ni - Output items' descriptions"
                                    + "\no - Output shopping cart" + "\nq - Quit");

                System.out.println("");
                System.out.println("");
                System.out.println("");

                System.out.println("Choose an option: ");

                userChoice = scnr.next().charAt(0);


                boolean rePrintMenu = true;

                do {

                switch (userChoice) {

                // Add item to cart.
                            case 'a':

                                System.out.println("ADD ITEM TO CART");

                                System.out.println("Enter the item name: ");
                                cartItems.setName(scnr.nextLine());

                            System.out.println("\n\nEnter the item description: ");
                                cartItems.setDescription(scnr.nextLine());

                                System.out.println("\n\nEnter the item price: ");
                                cartItems.setPrice(scnr.nextInt());

                                System.out.println("Enter the item quantity: ");
                                cartItems.setQuantity(scnr.nextInt());

                                break;


                // Remove item from cart                
                            case 'd':

                                System.out.println("REMOVE ITEM FROM CART");
                                System.out.println("Enter name of item to remove: ");

                                cart.removeItem(scnr.nextLine());

                                break;


                // Change item quantity         
                            case 'c':

                                //ItemToPurchase newQuantity = new ItemToPurchase();

                                System.out.println("CHANGE ITEM QUANTITY");
                                System.out.println("Enter the item name: ");
                                // cart.modifyItem(scnr.nextLine());

                                break;


                // Output items' descriptions       
                            case 'i':

                                System.out.println("OUTPUT ITEMS' DESCRIPTIONS");
                                System.out.println(cart.getCustomerName() + "'s Shopping Cart - " + cart.getDate());

                                System.out.println("");
                                System.out.println("");


                                System.out.println("Item Descriptions");

                                System.out.println(cart.printDescriptions());

                                System.out.println();

                                break;

                            case 'o':
                                System.out.println("OUTPUT SHOPPING CART");
System.out.println(cart.getCustomerName() + "'s Shopping Cart - " + cart.getDate());
                                System.out.println( cart.getNumItemsInCart() );

                                System.out.println("");
                                System.out.println("");

                                System.out.println(cart.printTotal());

                                break;

                            case 'q':
                                rePrintMenu = false;
                                System.exit(0);

                                break;

                            }
                        } while (rePrintMenu);      
                        return;
            }

    }
enter code here

这是我的ShoppingCart!

但我不知道为什么它不适用于main。另外,如何按名称删除项目?

我尝试了这种逻辑,但没有奏效:

有关(){ 如果()

否则{ }

我应该有布尔标志吗?还算?我迷路了。

import java.util.ArrayList;

public class ShoppingCart {

    private String customerName = "none";
    private String currentDate = "January 1, 2016";
    private ArrayList<ItemToPurchase> cartItems = new ArrayList<ItemToPurchase>();


    public ShoppingCart() {

    }


    public ShoppingCart(String userName, String userDate) {
        customerName = userName;
        currentDate = userDate;
        return;
    }


    public String getCustomerName() {
        return customerName;
    }


    public String getDate() {
        return currentDate;
    }


    public void addItem(ItemToPurchase item) {
        cartItems.add(item);
        return;
    }

//fixMe     
    public void removeItem(String itemName) {
        return;
    }
// fixMe
       public void modifyItem(ItemToPurchase item) {
           return;
       } 

    public int getNumItemsInCart() {
        int totalNumOfItem = 0;
        for (int i = 0; i < cartItems.size(); ++i) {

            totalNumOfItem += cartItems.get(i).getQuantity();
        }
        return totalNumOfItem;
    }


    public int getCostOfCart() {
        int cartCost = 0;

        for (int i = 0; i < cartItems.size(); ++i) {
            cartCost += cartItems.get(i).getQuantity();
            cartCost *= cartItems.get(i).getPrice();
        }

        return cartCost;
    }


    public int printTotal() {
        int total= 0;

            for (int i = 0; i < cartItems.size(); ++i) {
                if (cartItems.size()> 0) {

                cartItems.get(i).printItemCost();

                  total = (cartItems.get(i).getQuantity()) * (cartItems.get(i).getPrice());
                 System.out.println("Total: $" + total);
            }


        else {
            System.out.println("SHOPPING CART IS EMPTY");



    }}
            return total;
}

    public String printDescriptions() {
        String descriptions = "";
        for (int i = 0; i < cartItems.size(); ++i) {
            descriptions = cartItems.get(i).printItemDescription();
        }

            return descriptions;
    }

}

这是ItemToPurchase类。

public class ItemToPurchase {

    private String itemName = "none";
    private String itemDescription = "none";

    private int itemPrice=0; 
    private int itemQuantity=0;


    public ItemToPurchase() {

     }


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


    public void setDescription(String userItemDescription) {
        itemDescription = userItemDescription;
        return;
    }


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


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


    public String getName() {
        return itemName;
    }


    public String getDescription() {
        return itemDescription;
    }


    public int getPrice() {
        return itemPrice;
    }


    public int getQuantity() {
        return itemQuantity;
    }


    public void printItemCost() {
        System.out.print(itemName);
        System.out.print(" " + itemQuantity + " @ $" + itemPrice);
        System.out.println(" = $" + (itemQuantity * itemPrice));

        return ;
    }


    public String printItemDescription() {
        String descriptionForItem = itemName + ": " + itemDescription;
        System.out.println(descriptionForItem);
        return descriptionForItem;
    }

}

2 个答案:

答案 0 :(得分:0)

问题是nextInt()不使用'\ n',所以下一次调用nextLine()会消耗它,然后等待读取输入。

在调用nextLine();

之前,您需要使用'\ n'

更多详情:

Java Scanner doesn't wait for user input

答案 1 :(得分:0)

代码:

String userChoiceString = scnr.nextLine();
userChoice = userChoiceString.charAt(0);

next()部分不会读取所有标记。如果您调试代码,您将看到输入项目名称变为空字符串“” - scnr.next()的一部分。

改为使用:

{{1}}

请阅读https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#next()了解更多信息。