Shopping cart program

32,157

This block of code looks...suspect...

private static Item[] cart = new Item[capacity];

public Cart(){
    itemCount = 10;
    totalPrice = 0.0;
    capacity = 0;
}

You provide no other way to instantiate the Cart, so every time you new one up, you have a cart of size 0. Nothing could be added to that.

I'm willing to bet that, semantically, you mean to do something like this:

private Item[] cart;

public Cart(){
    itemCount = 0;
    totalPrice = 0.0;
    capacity = 10;
    cart = new Item[capacity];
}

You have a capacity of ten items now, and itemCount should be used in place of capacity in your add method to move the elements into their appropriate spot.

Share:
32,157
user3023250
Author by

user3023250

Updated on November 25, 2022

Comments

  • user3023250
    user3023250 over 1 year

    I've been working on my shopping cart program, but I keep having problems with entering itemTax in and with adding new items into cart and I don't know what I've done wrong.

    class Item:

    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;
    }
    
    }
    

    class Cart:

    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");
    }
    
    
    
    }
    

    class Shop:

    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;
            }
    
        }
    }
    
    }
    
    • Kick
      Kick over 10 years
      Can you elaborate the pblm .
    • user3023250
      user3023250 over 10 years
      When I enter decimal number for itemTax I get: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextDouble(Unknown Source) at en.Shop.main(Shop.java:52) , but when I enter ordinary number I get: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at en.Cart.add(Cart.java:21) at en.Shop.main(Shop.java:55)
    • brasofilo
      brasofilo over 10 years
      It's better to edit the Question to add new info. Also, check how to make a minimal example.