can someone tell me how can i do arrylist(of three classes) in one class ??

please explain what it is you're trying to do?

What do you mean by ArrayList of three classes?

You could create an ArrayList which hold Objects. Or maybe an interface class that the three classes have incommon.

public interface GeneralType {
   public void someMethod();
}

// Use Object as the generic type and cast to something else later.
List<Object> theList = new ArrayList<Object>();

// Use an Interface as the generic type which the three classes inherit from.
List<GeneralType> the List = new ArrayList<GeneralType>();

in which case the first option is the best, since you don't always have access to the classes you need to put in there.

i have three classes .. client ,product,customer and i want to do them in one class

??? you want to do 'what' with them in one class?

i want to call them from one class...which method i can use?

this has nothing at all to do with an ArrayList
just instantiate them and use their methods.

can you give me an example pls?

You should explain what it is your trying to do. This makes no sense.

i have this class :

import java.util.*;
import java.text.*;

public class Product
{
private String code;
private String description;
private double price;

public Product()
{
code="";
description="";
price=0.0;
}

public Product(Product copy)
{
this.code=copy.code;
this.description=copy.description;
this.price=copy.price;
}

public Product(String code, String description, double price)
{
this.code=code;
this.description=description;
this.price=price;
}

public String getCode()
{
return code;
}

public String getDescription()
{
return description;
}

public double getPrice()
{
return price;
}


public static void main(String []args)

{


Scanner in=new Scanner(System.in);

double total=0;

ArrayList<Product> list=new ArrayList<Product>();
list.add(new Product("K16" ,"Wood screws,brass,20mm", 7.75));
list.add(new Product("D24","Wood glue,clear,1 liter",5.50));
list.add(new Product("M93","Sandpaper,fine grade",10.25));
list.add(new Product("M94","Sandpaper,fine grade",14.75));
DecimalFormat df=new DecimalFormat("$ ##.##");

System.out.println("Hardware Items");
System.out.println();
System.out.println("Code " + " " +" Description " + " " +" Unit Price ");

for(Product e : list){

System.out.println(e.getCode () + "\t" + e.getDescription() + "\t" + df.format (e.getPrice ()) );

}
}
}

AND I WANT CALL IT FROM ANOTHER CLASS

Call what from which other class?

You should explain what it is your trying to do. This makes no sense.

I HAVE THIS CLASS :


import java.util.*;
import java.text.*;

public class Product
{
private String code;
private String description;
private double price;

public Product()
{
code="";
description="";
price=0.0;
}

public Product(Product copy)
{
this.code=copy.code;
this.description=copy.description;
this.price=copy.price;
}

public Product(String code, String description, double price)
{
this.code=code;
this.description=description;
this.price=price;
}

public String getCode()
{
return code;
}

public String getDescription()
{
return description;
}

public double getPrice()
{
return price;
}


public static void main(String []args)

{


Scanner in=new Scanner(System.in);

double total=0;

ArrayList<Product> list=new ArrayList<Product>();
list.add(new Product("K16" ,"Wood screws,brass,20mm", 7.75));
list.add(new Product("D24","Wood glue,clear,1 liter",5.50));
list.add(new Product("M93","Sandpaper,fine grade",10.25));
list.add(new Product("M94","Sandpaper,fine grade",14.75));
DecimalFormat df=new DecimalFormat("$ ##.##");

System.out.println("Hardware Items");
System.out.println();
System.out.println("Code " + " " +" Description " + " " +" Unit Price ");

for(Product e : list){

System.out.println(e.getCode () + "\t" + e.getDescription() + "\t" + df.format (e.getPrice ()) );

}
}
}

ABD I WANT TO CALL IT FROM ANTOTHER CLASS

then just put your main method in another class.

Call what from which other class?

i want the ouput of classA to bean output in classB

then just put your main method in another class.

i can do that?

read my previous post

Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75)

you can do this (instantiating a Product in each method you have, in no matter what class, as long as the Product class is in it's scope.
the same for your other classes.

i can do that?

yup.

yup.

thanks really alot :)

yup.

i did this :

import java.util.*;
import java.text.*;

public class Business{
    public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.Product();

    }
}

and the output is tellin me that cannot find symbol in firstProd.Product();

put code inside main method to another method of class product and replace

import java.util.*;
import java.text.*;

public class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.Product();

}
}

to

import java.util.*;
import java.text.*;

public class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.newMethod();

}
}

look at line number 10.

i did this :

 import java.util.*;
 import java.text.*;

 public class Business{
     public static void main (String[] args) {


 Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

 firstProd.Product();

     }
 }

and the output is tellin me that cannot find symbol in firstProd.Product();

don't confuse methods and constructors.

you are trying to call your constructor for an instance.
replace firstProd.Product();
with
System.out.println(firstProd.getDescription());

put code inside main method to another method of class product and replace

import java.util.*;
import java.text.*;

public class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.Product();

}
}

to

import java.util.*;
import java.text.*;

public class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.newMethod();

}
}

look at line number 10.

why newMethod();?
it would land him exactly the same error, because there is no 'newMethod()' method.

why newMethod();?
it would land him exactly the same error, because there is no 'newMethod()' method.

i mean the new method(whichever) in which he has to put code of main method of class product.

put code inside main method to another method of class product and replace

import java.util.*;
import java.text.*;

public class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.Product();

}
}

to

import java.util.*;
import java.text.*;

public class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.newMethod();

}
}

look at line number 10.

do i have to do them in both different class or in business class(one class)?

just in your main method.
do NOT change the public Product() in your Product class.
this is NOT just a method, it's a constructor. change that name, and your code will no longer compile.

here is improved code.
take a look at it

import java.util.*;
//Product class

import java.text.*;

class Product
{
private String code;
private String description;
private double price;

public Product()
{
code="";
description="";
price=0.0;
}

public Product(Product copy)
{
this.code=copy.code;
this.description=copy.description;
this.price=copy.price;
}

public Product(String code, String description, double price)
{
this.code=code;
this.description=description;
this.price=price;
}

public String getCode()
{
return code;
}

public String getDescription()
{
return description;
}

public double getPrice()
{
return price;
}




public void printArrayList()

{


Scanner in=new Scanner(System.in);

double total=0;

ArrayList<Product> list=new ArrayList<Product>();
list.add(new Product("K16" ,"Wood screws,brass,20mm", 7.75));
list.add(new Product("D24","Wood glue,clear,1 liter",5.50));
list.add(new Product("M93","Sandpaper,fine grade",10.25));
list.add(new Product("M94","Sandpaper,fine grade",14.75));
DecimalFormat df=new DecimalFormat("$ ##.##");

System.out.println("Hardware Items");
System.out.println();
System.out.println("Code " + " " +" Description " + " " +" Unit Price ");



for(Product e : list){

System.out.println(e.getCode () + "\t" + e.getDescription() + "\t" + df.format (e.getPrice ()) );

}
}
public static void main (String[] args) 
{


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.printArrayList();

}
}

//Business class
import java.util.*;
import java.text.*;

class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.printArrayList();

}
}

public Product(Product copy)
{
this.code=copy.code;
this.description=copy.description;
this.price=copy.price;
}

what exactly do you mean by that constructor? creating an instance you already have? :)
I think the clone method would be more suitable when implementing something like this.

not to mention that you still put a lot of business code in the Product class, which should definitely not be there.

here is improved code.
take a look at it

import java.util.*;
//Product class

import java.text.*;

class Product
{
private String code;
private String description;
private double price;

public Product()
{
code="";
description="";
price=0.0;
}

public Product(Product copy)
{
this.code=copy.code;
this.description=copy.description;
this.price=copy.price;
}

public Product(String code, String description, double price)
{
this.code=code;
this.description=description;
this.price=price;
}

public String getCode()
{
return code;
}

public String getDescription()
{
return description;
}

public double getPrice()
{
return price;
}




public void printArrayList()

{


Scanner in=new Scanner(System.in);

double total=0;

ArrayList<Product> list=new ArrayList<Product>();
list.add(new Product("K16" ,"Wood screws,brass,20mm", 7.75));
list.add(new Product("D24","Wood glue,clear,1 liter",5.50));
list.add(new Product("M93","Sandpaper,fine grade",10.25));
list.add(new Product("M94","Sandpaper,fine grade",14.75));
DecimalFormat df=new DecimalFormat("$ ##.##");

System.out.println("Hardware Items");
System.out.println();
System.out.println("Code " + " " +" Description " + " " +" Unit Price ");



for(Product e : list){

System.out.println(e.getCode () + "\t" + e.getDescription() + "\t" + df.format (e.getPrice ()) );

}
}
public static void main (String[] args) 
{


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.printArrayList();

}
}

//Business class
import java.util.*;
import java.text.*;

class Business{
public static void main (String[] args) {


Product firstProd = new Product("K16" ,"Wood screws,brass,20mm", 7.75);

firstProd.printArrayList();

}
}

end-quote

so now i did the saem thing for client and put firstClient.printArrays();
in class Business and its doesnt worked? why?

so now i did the saem thing for client and put firstClient.printArrays();
in class Business and its doesnt worked? why?

there were a lot of design errors in that 'improved code'. could you show the code you have now, from your different classes in separate CODE blocks?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.