Shopping.java:35: cannot find symbol
symbol : method ShoppingCart()
location: class Shopping
System.out.println(ShoppingCart());
I have ShoppingCart instantiated within this class so I am not sure why I am getting this error. Please help!!
// ***************************************************************
// 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;
}
}
// **********************************************************************
// 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
// -----------------------------------------------------------
// Creates an empty shopping cart with a capacity of 5 items.
// -----------------------------------------------------------
private Item[] cart = new Item[5];
public ShoppingCart()
{
capacity = 5;
itemCount = 0;
totalPrice = 0.0;
}
// -------------------------------------------------------
// Adds an item to the shopping cart.
// -------------------------------------------------------
public void addToCart(String itemName, double price, int quantity)
{
if(itemCount==cart.length)
{
increaseSize();
capacity += 3;
}
cart[itemCount] = new Item(itemName, price, quantity);
itemCount++;
totalPrice += price*quantity;
capacity--;
}
// -------------------------------------------------------
// 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[cart.length+3];
for(int i=0; i<cart.length; i++)
temp[i] = cart[i];
cart=temp;
}
// ---------------------------------------------------------
// Returns the total price
// ---------------------------------------------------------
public double getTotalPrice()
{
return totalPrice;
}
}
import java.util.Scanner;
public class Shopping
{
public static void main (String[] args)
{
String ans;
int quantity;
double price;
String itemName;
Scanner scan = new Scanner(System.in);
ShoppingCart ShopCart = new ShoppingCart();
System.out.print("Do you wish to shop?");
ans = scan.nextLine();
while (ans.equalsIgnoreCase("y"))
{
System.out.print("Enter item: ");
itemName = scan.nextLine();
System.out.print("Enter price: ");
price = scan.nextFloat();
System.out.print("Enter quantity: ");
quantity = scan.nextInt();
scan.nextLine();
//add item to cart
ShopCart.addToCart(itemName, price, quantity);
System.out.println(ShoppingCart());
System.out.println("Please pay " + ShopCart.getTotalPrice());
} //end of while loop
}
}

New Topic/Question
Reply




MultiQuote




|