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

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

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

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

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

Edited 4 Years Ago by Tuz: n/a

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.

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();

Edited 3 Years Ago by Dani: Formatting fixed

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.

Edited 4 Years Ago by rushikesh jadha: n/a

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());

Edited 3 Years Ago by diafol: fixed formatting

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();

}
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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?

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