import java.text.NumberFormat; import java.util.Scanner; 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 String end; // ----------------------------------------------------------- // 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) { Item item = new Item(itemName, price, quantity); if (itemCount <= capacity) { cart[itemCount] = item; totalPrice += (price * quantity); itemCount++; } } // ------------------------------------------------------- // 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; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); String end = ""; while (!end.equalsIgnoreCase("quit")) { ShoppingCart shopCart = new ShoppingCart(); System.out.print("Enter the name of the item: "); String name = scan.next(); System.out.print("Enter the price of the item: "); double price = scan.nextDouble(); System.out.print("Enter the quantity of the item: "); int quantity = scan.nextInt(); shopCart.addToCart(name, price, quantity); for (int i=0;i < shopCart.capacity;i++) System.out.println(shopCart.cart[i].toString()); System.out.println("The total price is: $" + shopCart.totalPrice); System.out.println("Type quit to exit, or type anything else to continue. "); end = scan.next(); } } // main } // class ---------------------------------------------------------- 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; } } -------------------------------------------------- C:\$\10>java ShoppingCart Enter the name of the item: a Enter the price of the item: 12 Enter the quantity of the item: 1 a $12.00 1 $12.00 Exception in thread "main" java.lang.NullPointerException at ShoppingCart.main(ShoppingCart.java:84)