import java.util.Arrays;
import java.util.Comparator;

public class CameraInventory2
{

    
    // The main entry
    public static void main(String[] args)
    {
        
        // create the array
        Camera[] camera = new Camera[3];
		Cam cam = new Cam();
        camera[0] = new Camera(1, "Nokia", 20, 30.0, 8);
        camera[1] = new Camera(2, "Polaroid", 10, 21.0, 14.1);
        camera[2] = new Camera(3, "Canon", 15, 32.0, 17.8);
        
        Inventory invent = new Inventory(camera);
        
        // sort the inventory
        invent.sortProd();
        
        for (int i = 0; i < invent.getSize(); i++)
        {
            // get the product
            Camera prod = invent.get(i);
            
            // display the product
            System.out.println("Item Number: " + prod.getItemNumber());
            System.out.println("Product Name: " + prod.getName());
            System.out.println("Number Of Units: " + prod.getUnits());
			System.out.println("Number Of Megapixels: " + cam.megapx());
            System.out.printf("Price Per Unit: $%,.2f\n", prod.getPrice());
            System.out.printf("The value of the inventory: $%,.2f\n\n", prod.inventoryValue());
			System.out.printf("The value of the inventory after restocking: $%,.2f\n\n", cam.getRestockingFee());
        }
        
        // display the total value
        System.out.printf("The total value: $%,.2f\n", invent.totalValue());
       

    }

}


class Camera
{
    // Declare instance variables  
    private int itemNumber; 
    private String name; 
    private int units; 
    private double price;
    Cam cam = new Cam();
	
	    public Camera(Cam[] cam)
    {
        this.cam = cam;
    }
	
    // Default constructor
    public Camera()
    {
        itemNumber = 0;
        name = "";
        units = 0;
        price = 0;        
    }
    
    // Parameters constructor
    public Camera(int itemNumber, String name, int units, double price, double megapx)
    {
        this.itemNumber = itemNumber;
        this.name = name;
        this.units = units;
        this.price = price;
    }
    
    // calculate inventory value
    public double inventoryValue()
    {
        return units * price;
    }

    // accessors and modifiers
    public int getItemNumber()
    {
        return itemNumber;
    }
    
    public void setItemNumber(int itemNumber)
    {
        this.itemNumber = itemNumber;
    }
    
    public String getName()
    {
        return name;
    }
    
    public void setName(String name)
    {
        this.name = name;
    }
    
    public int getUnits()
    {
        return units;
    }
    
    public void setUnits(int units)
    {
        this.units = units;
    }
    
    public double getPrice()
    {
        return price;
    }
    
    public void setPrice(double price)
    {
        this.price = price;
    }    
  
}

class Cam extends Camera
{
    // additional unique feature
    private double megapx;
    
    // Default constructor
    public Cam()
    {
        super();
        double megapx = 14.1;
    }
    
    // Parameters constructor
    public Cam(int itemNumber, String name, int units, double price, double megapx)
    {
        super(itemNumber, name, units, price, megapx);
        this.megapx = megapx;
    }
    
    // calculate 5% restocking fee
    public double getRestockingFee()
    {
        return getPrice() * getUnits() * 0.05;
    }
    
    // calculate inventory value
    public double inventoryValue()
    {
        return getPrice() * getUnits();
    }
    
    // accessors and modifiers
    public double getMegapx()
    {
        return megapx;
    }

    // setter of megapx
    public void setMegapx(double megapx)
    {
        this.megapx = megapx;
    }

}

class Inventory
{
    private Camera[] camera;
    
    // Constructor
    public Inventory(Camera[] camera)
    {
        this.camera = camera;
    }
    
    // Calculate the value of the entire inventory
    public double totalValue()
    {
        double total = 0;       
        for (int i = 0; i < camera.length; i++)
            total += camera[i].inventoryValue();
        return total;
    }
    
    // Sort the array items by the name of the product
    public void sortProd()
    {
        Comparator<Camera> comp = new Comparator<Camera>() {
            public int compare(Camera o1, Camera o2) {
                return o1.getName().compareTo(o2.getName());
            }            
        };
        
        Arrays.sort(camera, comp);
    }
    
    // Get the size of the inventory
    public int getSize()
    {
        return camera.length;
    }
    
    // Get product of the inventory
    public Camera get(int idx)
    {
        return camera[idx];
    }
}

CameraInventory2.java:33: error: cannot find symbol
System.out.println("Number Of Megapixels: " + cam.megapx
());
^
symbol: method megapx()
location: variable cam of type Cam
CameraInventory2.java:59: error: incompatible types
this.cam = cam;
^
required: Cam
found: Cam[]
2 errors

I cannot seem to understand why I am getting these two errors. I have tried to modify the lines of code several times, but I usually wind up with a different set of errors.

Edited 5 Years Ago by b.netana: n/a

The error is because of concept of polymorphism.

In line 33 that you specified , cam is a super class, while megapx() is present in a sub class. Your super class variable can hold the reference to a suclass object but it will not know the functions or variables present in subclass.

A more practical example : Animal class cannot know the specific methods of a Dog class.

For your convenience , give more appropriate names to super classes. Cam and camera are too confusing as to what is what unless a programmer specifically looks into your code.

To simplify...
On line 33, you are calling a method... "cam.megapx()".
You don't have a method called megapx.
You have a variable (a double) called megapx.
Instead, call your get method... "cam.getMegapx()".

This article has been dead for over six months. Start a new discussion instead.