Click here to Skip to main content
15,913,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I have a superclass called product and 3 subclasses called Laptop, Web and Microphone. In each subclass a created a static variable that counts the number of products from each class. I created an Array list that shows the products from each but my problem is that a don't know how to call the static variable assigned to each object.

What I have tried:

The page of subclass laptop:

Java
public class laptop extends Products {
     static int stock=0;
    public laptop(String name, String color, Double price, Double discount) {
        super(name, color, price, discount);
        stock++;

 public static int getStock() {
        return stock;
    }
    }


The class of the Array List that creates the objects:

Java
<pre>ArrayList <Products> catalog = new ArrayList<>();
        catalog.add( new web("web", "red", 145.0, 1.0));
        catalog.add( new microphone("microphone", "red", 145.0, 1.0));
        catalog.add( new laptop("laptop", "red", 145.0, 1.0));


So, If I would call web.getStock everything works fine and I can access the stock. But if I want to display the list using a for loop I don't really know how to access the stock of each product.

Java
<pre>   for(int i=0; i<catalog.size(); i++){
           System.out.println(catalog.get(i).name + " " + (Here! I don't know how to acces the static variable of each catalog.get(i).name);
Posted
Updated 6-Jun-18 10:43am
v2

Why not use separate lists for each product type, so you can just use ArrayList.size()[^] to get the count?
 
Share this answer
 
Comments
Member 11018842 6-Jun-18 16:28pm    
Because I want to create a database for a lot of products, so my code would be very complicated if I would create lists for let's say 100 different products.
Richard MacCutchan 6-Jun-18 17:07pm    
That does not make sense. If you are using a database then you are unlikely to need such a list.
Member 11018842 6-Jun-18 17:22pm    
I expressed myself wrong. I just want to say that if I would have 100 of different products the code will be very complicated.
Richard MacCutchan 7-Jun-18 3:40am    
Not if you do it right.
Define an abstract method in the Product class and override it in the derived ones. For instance
Java
abstract class Product
{
  String name;
  public Product(String name)
  {
    this.name = name;
  }
  public abstract int getStock();
}

class Laptop extends Product
{
  static int stock = 0;
  public Laptop(String name)
  {
    super(name);
    ++stock;
  }
  @Override
  public int getStock()
  {
    return stock;
  }
}
 //...
 // in main method
    ArrayList <Product> catalog = new ArrayList<Product>();
    catalog.add( new Laptop("foo") );
    catalog.add( new Laptop("goo"));
    System.out.println( catalog.get(1).getStock() );
 
Share this answer
 
Comments
Member 11018842 6-Jun-18 19:56pm    
It worked. Thank you very much.
CPallini 7-Jun-18 4:20am    
Did you doubt about? :-D
You are welcome.
Member 11018842 7-Jun-18 4:58am    
If I would have, let's say 5 types of laptops, how can I keep the evidence of the stocks for each of them? Should I create public classes for each kind of laptop or just to create static variables(for each kind of laptop) in the class Laptop? How is used in practice?
CPallini 7-Jun-18 5:29am    
You should make different classes if you need them (e.g. different states or behaviours). Don't make different classes just to differentiate their instances counts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900