0

It's my first time writing a class and I am getting all these errors:

C:\Users\Eamon\programming\java>javac -Xlint:unchecked Shop1.java
Shop1.java:20: error: cannot find symbol
                if (cart.itemsInCart.products.get(itemIndex).quantity != 0)
                                                            ^
  symbol:   variable quantity
  location: class Object
Shop1.java:21: error: cannot find symbol
                    System.out.println(cart.itemsInCart.products.get(itemIndex).quantity
                                                                               ^
  symbol:   variable quantity
  location: class Object
Shop1.java:22: error: cannot find symbol
                        + " " + cart.itemsInCart.products.get(itemIndex).name
                                                                        ^
  symbol:   variable name
  location: class Object
Shop1.java:23: error: cannot find symbol
                        + " $"+ df.format(cart.itemsInCart.products.get(itemIndex).price)
                                                                                  ^
  symbol:   variable price
  location: class Object
Shop1.java:25: error: cannot find symbol
                        ((cart.itemsInCart.products.get(itemIndex).quantity
                                                                  ^
  symbol:   variable quantity
  location: class Object
Shop1.java:26: error: cannot find symbol
                        * cart.itemsInCart.products.get(itemIndex).price)));
                                                                  ^
  symbol:   variable price
  location: class Object
Shop1.java:35: error: cannot find symbol
                subtotal += cart.itemsInCart.products.get(itemIndex).quantity
                                                                    ^
  symbol:   variable quantity
  location: class Object
Shop1.java:36: error: cannot find symbol
                * cart.itemsInCart.products.get(itemIndex).price;
                                                          ^
  symbol:   variable price
  location: class Object
Shop1.java:126: error: cannot find symbol
                    if (codeInput.equals(this.itemsInCart.products.get(itemIndex).code))
                                                                                 ^
  symbol:   variable code
  location: class Object
Shop1.java:138: error: cannot find symbol
            this.itemsInCart.products.get(itemIndex).quantity = scanner.nextInt();
                                                    ^
  symbol:   variable quantity
  location: class Object
Shop1.java:140: error: cannot find symbol
            if (this.itemsInCart.products.get(itemIndex).quantity > 100)
                                                        ^
  symbol:   variable quantity
  location: class Object
Shop1.java:143: error: cannot find symbol
                    + this.itemsInCart.products.get(itemIndex).quantity
                                                              ^
  symbol:   variable quantity
  location: class Object
Shop1.java:145: error: cannot find symbol
                    + this.itemsInCart.products.get(itemIndex).quantity
                                                              ^
  symbol:   variable quantity
  location: class Object
Shop1.java:147: error: cannot find symbol
                if (scanner.nextInt() != this.itemsInCart.products.get(itemIndex).quantity)
                                                                                 ^
  symbol:   variable quantity
  location: class Object
Shop1.java:148: error: cannot find symbol
                    this.item[itemIndex].quantity = 0;
                        ^
  symbol: variable item
Shop1.java:150: error: cannot find symbol
            if (this.itemsInCart.products.get(itemIndex).quantity < 0)
                                                        ^
  symbol:   variable quantity
  location: class Object
Shop1.java:151: error: cannot find symbol
                this.itemsInCart.products.get(itemIndex).quantity = 0;
                                                        ^
  symbol:   variable quantity
  location: class Object
Shop1.java:50: error: cannot find symbol
        ArrayList products = new Arraylist<Product>(3);
                                 ^
  symbol:   class Arraylist
  location: class Catalogue
Shop1.java:54: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
            products.add(new Product("Condensed Powdered water", "P3487", 2.50
                        ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
Shop1.java:56: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
            products.add(new Product("Distilled Moonbeams", "K3876", 3.00
                        ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
Shop1.java:58: warning: [unchecked] unchecked call to add(E) as a member of the raw type ArrayList
            products.add(new Product("Anti-Gravity Pills", "Z9983", 12.75
                        ^
  where E is a type-variable:
    E extends Object declared in class ArrayList
Shop1.java:80: error: Illegal static declaration in inner class Catalogue.Product
            static final Pattern productCodeRegex =
                                 ^
  modifier 'static' is only allowed in constant variable declarations
Shop1.java:83: error: Illegal static declaration in inner class Catalogue.Product
            public static boolean isValidCode(String codeInput)
                                  ^
  modifier 'static' is only allowed in constant variable declarations
Shop1.java:92: error: cannot find symbol
                    + this.products.get(itemIndex).name
                                                  ^
  symbol:   variable name
  location: class Object
Shop1.java:93: error: cannot find symbol
                    + " [" + this.products.get(itemIndex).code + "], $"
                                                         ^
  symbol:   variable code
  location: class Object
Shop1.java:94: error: cannot find symbol
                    + df.format(this.products.get(itemIndex).price) + " "
                                                            ^
  symbol:   variable price
  location: class Object
Shop1.java:95: error: cannot find symbol
                    + this.products.get(itemIndex).rate + ".");
                                                  ^
  symbol:   variable rate
  location: class Object
24 errors
3 warnings

I'm working my way back, and I can find those variables just fine; what gives?

import java.util.Scanner;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.text.DecimalFormat;

public class Shop1
{  
    public static void main(String args[])
    {  
        Cart cart = new Cart(new Catalogue());
        printOrder(cart);
    }

    public static void printOrder(Cart cart)
    {
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println("Your order:");
        for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size(); 
            itemIndex++)
            if (cart.itemsInCart.products.get(itemIndex).quantity != 0)
                System.out.println(cart.itemsInCart.products.get(itemIndex).quantity 
                    + " " + cart.itemsInCart.products.get(itemIndex).name 
                    + " $"+ df.format(cart.itemsInCart.products.get(itemIndex).price) 
                    + " = $" + df.format
                    ((cart.itemsInCart.products.get(itemIndex).quantity 
                    * cart.itemsInCart.products.get(itemIndex).price)));

        double subtotal = 0;
        int taxPercent = 20;
        double tax;
        double total;

        for(int itemIndex = 0; itemIndex < cart.itemsInCart.products.size(); 
            itemIndex++)
            subtotal += cart.itemsInCart.products.get(itemIndex).quantity 
            * cart.itemsInCart.products.get(itemIndex).price;
        tax = subtotal * taxPercent / 100;
        total = subtotal + tax;


        System.out.print("Subtotal: $" + df.format(subtotal) 
            + " Tax @ " + taxPercent + "%: $" + df.format(tax) 
            + " Grand Total: $" + df.format(total));
    }  
}

class Catalogue
{
    DecimalFormat df = new DecimalFormat("0.00");
    ArrayList products = new Arraylist<Product>(3);

    public Catalogue()
    {
        products.add(new Product("Condensed Powdered water", "P3487", 2.50
            , "per packet"));
        products.add(new Product("Distilled Moonbeams", "K3876", 3.00
            , "a dozen")); 
        products.add(new Product("Anti-Gravity Pills", "Z9983", 12.75
            , "for 60"));
    }

    class Product
    {
        String name;
        double price;
        String code;
        String rate;
        int quantity;

        public Product(String startName, String startCode, double startPrice
            , String startRate) 
        {
            name = startName;
            code = startCode;
            price = startPrice;
            rate = startRate;
            quantity = 0;
        }

        static final Pattern productCodeRegex = 
            Pattern.compile("^[a-zA-Z][0-9]{4}$"); 

        public static boolean isValidCode(String codeInput)
            {return productCodeRegex.matcher(codeInput).matches();}
    }

    public void printCatalogue()
    {
        System.out.println("Our catalogue (product codes in brackets):");
        for(int itemIndex = 0; itemIndex < this.products.size(); itemIndex++)
            System.out.println("(" + (itemIndex + 1) + ") " 
                + this.products.get(itemIndex).name 
                + " [" + this.products.get(itemIndex).code + "], $"
                + df.format(this.products.get(itemIndex).price) + " " 
                + this.products.get(itemIndex).rate + ".");
        System.out.println("Buy something!");
    }
}

class Cart
{
    Scanner scanner = new Scanner(System.in); //

    int size = 3; //
    String codeInput = "";
    Catalogue itemsInCart;

    public Cart(Catalogue catalogue) 
    {
        itemsInCart = catalogue;
        catalogue.printCatalogue();
        this.selectProducts();
    }

    public void selectProducts()
    {
        while (true)
        {
            System.out.print("Enter product code (0 to check out): ");
            codeInput = scanner.next();
            scanner.nextLine();
            if (codeInput.equals("0")) return;

            for (int itemIndex = 0; itemIndex < this.itemsInCart.products.size(); 
                itemIndex++)
                if (codeInput.equals(this.itemsInCart.products.get(itemIndex).code))
                    this.addToCart(itemIndex);       
            if (Product.isValidCode(codeInput))
                System.out.println("This product code is not on record.");      
            else System.out.println
                ("Sorry, I don't understand! Use product codes only.");
        }         
    }

    public void addToCart(int itemIndex)
    {
        System.out.print("Enter quantity: ");
        this.itemsInCart.products.get(itemIndex).quantity = scanner.nextInt();
        scanner.nextLine();
        if (this.itemsInCart.products.get(itemIndex).quantity > 100)
        {
            System.out.print("That is a large order, " 
                + this.itemsInCart.products.get(itemIndex).quantity 
                + " counts. Is this correct? Enter \"" 
                + this.itemsInCart.products.get(itemIndex).quantity 
                + "\" to confirm, or, enter any other integer to cancel: ");
            if (scanner.nextInt() != this.itemsInCart.products.get(itemIndex).quantity)
                this.item[itemIndex].quantity = 0;
        }
        if (this.itemsInCart.products.get(itemIndex).quantity < 0)
            this.itemsInCart.products.get(itemIndex).quantity = 0;
    }

}
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
Eamon Moloney
  • 1,853
  • 4
  • 19
  • 22

2 Answers2

4

In Catalogue, you need

ArrayList<Product> products = new ArrayList<Product>(3);

instead of

ArrayList products = new Arraylist<Product>(3);

or else the compiler does not know which type will be returned by

cart.itemsInCart.products.get(itemIndex)

Only if the compiler knows the returned type here is Product, he knows you can access the quantity field. If any type could be returned, it could be types were .quantity is not present or not accessible.

For future reference, please provide the code you are actually using, because yours wouldn't even compile. You have Arraylist where it should be ArrayList (Java is case sensitive). Also it is good practice to program against the interface, so it would be even better to write List<Product> products = new ArrayList<Product>(3);

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • I don't know what this means. « programming against the interface » – Eamon Moloney Jan 03 '13 at 11:15
  • I tried to make an example. When specifying the type, only use the interface, when instantiating use the concrete class: `List products = new ArrayList(3);` – jlordo Jan 03 '13 at 11:19
1

From what I can see, some symbols you're trying to use could be inaccessible. Try defining each class in its own file, then making the symbols you want to use public and see if that resolves the issue. After that, you can structure your code in a better manner (that is, with getters and setters instead of direct access).

EDIT: My mistake. jlordo's answer is the correct one.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104