Hi again,
I still might be a little in the dark with the issue, there being a language barrier of sorts but ill try my best
First of all, welcome to the forums. Just follow the mod's instructions and you'll fit in fine, i am sure.
I think you should pay a little more attention to your naming conventions. I assume G[] is an array of garments. In Java, it is *very* advisable to use the camel case and other agreed-upon naming practices! If only to be able to clearly and unambiguously rely your code to other Java -programmers. You can google "java naming conventions" and you shall find tons of pages on the issue. Here's one:
Using Java Naming Conventions.
Anyway, to the point. If it as you said, that Garment and Appliance inherit Product, then HomeAppliance and Gadget inherit Appliance, that means all of your objects are Products as well. You can use Product as the type of your collection and add all objects to it. One thing you should read about is ArrayList and other Collections in Java. They are way more flexible to use that raw arrays. You could also override the toString -method of each class to print each instance as you'd like. Ill write a small example here, hopefully i have understood your issue even remotely correct
.
import java.util.ArrayList;
public class ProductExample {
public static void main(String[] args) {
//You mentioned that you have diffirent array for diffirent objects, trying to copy that here:
Appliance[] gadgets = new Appliance[3];
Appliance[] homeAppliances = new Appliance[3];
Garment[] garments = new Garment[3];
// Now creating some example test data. Im using the raw lists instead of ArrayLists because in your posted code
// you didnt either. But you probably should :)
for ( int i = 0; i < 3; i++ ) {
gadgets[i] = new Gadget("gadget_" + i, 20d+i);
homeAppliances[i] = new HomeAppliance("homeApp_" + i, 30d+i);
garments[i] = new Garment("garment_" + i, + 24d+i);
}
//Now creating the *Product* ArrayList that can take all of the above:
ArrayList<Product> allProductsList = new ArrayList<Product>();
//There are better ways of filling the products list and iterating thru the raw lists but for the sake of the example
//im using the long way to illuminate the point
//So, going thru all the diffirent arrays and adding them to the products list:
for ( int i = 0; i < gadgets.length; i++) {
allProductsList.add(gadgets[i]);
}
for ( int i = 0; i < homeAppliances.length; i++ ) {
allProductsList.add(homeAppliances[i]);
}
for ( int i = 0; i < garments.length; i++ ) {
allProductsList.add( garments[i] );
}
//At this point we have all the different products in a single array and we can just go through it and use the overwritten toString.
//Using for-each here.
for ( Product currentProduct : allProductsList ) {
System.out.println(currentProduct.toString());
}
}
}
//The classes
/**
* The mother class of all products.
*/
class Product {
//Some instance variables for the example
String name;
Double price;
//Constructor to set the variables
public Product(String name, Double price) {
this.name = name;
this.price = price;
}
//Overriding the toString method for custom printing
@Override
public String toString() {
return "I am a " + this.getClass().getName() + ". My name is " + name + " and i cost " + price;
}
}
//Appliance that extends Product and then Gadget and HomeAppliance that extend the Appliance class.
class Appliance extends Product {
public Appliance(String name, Double price) {
super(name, price);
}
}
class Gadget extends Appliance {
public Gadget(String name, Double price) {
super(name, price);
}
}
class HomeAppliance extends Appliance {
public HomeAppliance(String name, Double price) {
super(name, price);
}
}
// The garment class that isnt Appliance but is a product.
class Garment extends Product{
public Garment(String name, Double price) {
super(name, price);
}
}
The output should be:
I am a main.Gadget and my name is gadget_0 and i cost 20.0
I am a main.Gadget and my name is gadget_1 and i cost 21.0
I am a main.Gadget and my name is gadget_2 and i cost 22.0
I am a main.HomeAppliance and my name is homeApp_0 and i cost 30.0
I am a main.HomeAppliance and my name is homeApp_1 and i cost 31.0
I am a main.HomeAppliance and my name is homeApp_2 and i cost 32.0
I am a main.Garment and my name is garment_0 and i cost 24.0
I am a main.Garment and my name is garment_1 and i cost 25.0
I am a main.Garment and my name is garment_2 and i cost 26.0
Hope you got something out of this
. Good luck!