I have three classes Client,Product and Main.How can I call the client and the product from the main ?

In java, all you need to do is reference them from main.
...just like you would reference almost any other class.
...unless they are buried in some other package.

Are they in separate files?

Edited 4 Years Ago by thines01: n/a

import java.util.*;
import java.text.*;
class Client
{

public static void main(String []args){

Scanner in=new Scanner(System.in);

double total=0;


System.out.println("PLACE YOUR ORDER!");
System.out.println ();
System.out.print("NAME:");
String a=in.next();
System.out.print ("SURNAME:");
String b=in.next();
System.out.print("ADDRESS-1:");
String c=in.next();
System.out.print("ADDRESS-2:");
String d=in.next();

System.out.print(" :");
String e=in.next();

System.out.print("POST CODE:");
String f=in.next();

System.out.println("ENTER CODE (XX to stop)" );
System.out.print("Code:" );
String code=in.next();
System.out.print("Quantity: ");
int quantity=in.nextInt();
while(!code.equals("XX")){
System.out.print("Code:" );
code=in.next();
if(code.equals("XX")){
break;
}
System.out.print("Quantity: ");
quantity=in.nextInt();



}
}

THIS IS CLIENT CLASS

------------------------------------------------------------------------

import java.util.*;
import java.text.*;
class Product
{
String code;
String description;
double price;

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


}
}

THIS IS MY PRODUCT CLASS

WHAT CAN I INPUT IN MAIN CLASS TO CALL THE BOTH CLASSES?

Edited 4 Years Ago by Ezzaral: Added code tags. Use them to format any code that you post.

A couple of things you should know:
1) When you post code, please use the [ CODE ] button at the top edge of the post and put your code in the tags so it displays in a better format.
2) Only ONE of your classes can have a main() method in it, so you will need to decide which. You can even make a third module with the main() in it.

And
3) Learn to indent.

my problem is how to call them!

my problem is how to call them!

in the main class what should i code to get those two classes in it? i want the output of the main class will be with other two too!

Thanks

We're getting there.
Let's say you remove the main from your Product class and arrange it like this:

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;
   }
}

You could then create another class (let's call it Business) that has the main()

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

public class Business
{
   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 ()) );
      }
   }
}

When you compile the Business.java, it will compile the Product.java and make it available to main().

You will need to do also remove the main() from Client.java and make it hold fields, properties and methods JUST LIKE PRODUCT but with Client-appropriate elements.

When you also reference it from the main() INSIDE Business.java, it will all work together.

You need to re-construct Client.java, however.

Only one module can contain main() and there can only be ONE main().

Edited 4 Years Ago by thines01: n/a

We're getting there.
Let's say you remove the main from your Product class and arrange it like this:

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;
   }
}

You could then create another class (let's call it Business) that has the main()

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

public class Business
{
   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 ()) );
      }
   }
}

When you compile the Business.java, it will compile the Product.java and make it available to main().

You will need to do also remove the main() from Client.java and make it hold fields, properties and methods JUST LIKE PRODUCT but with Client-appropriate elements.

When you also reference it from the main() INSIDE Business.java, it will all work together.

You need to re-construct Client.java, however.

Only one module can contain main() and there can only be ONE main().

in product class i have an error when i run it :Error: Main method not found in class Product, please define the main method as:
public static void main(String[] args)

in product class i have an error when i run it :Error: Main method not found in class Product, please define the main method as:
public static void main(String[] args)

so i made it.. now what about the class of client ?

so i made it.. now what about the class of client ?

Can i use the same thing for the Client class and than i do the same in main class u add up the classses in it??

REALLY THANKS

You can do something like that, yes.

thanks really alot for your help, appreciate it :)

Just remember to keep Product and Client simple.
Do the processing in main().
That might change in other exercises.

Only one module can contain main() and there can only be ONE main().

Just for the record: any or every class in your program can have a public static void main(String[] args) method. It's up to you which one you run when you start the program.
Like many other people I often code a main in each class that just runs some basic tests for that class which I can use for testing during development and also for regression testing during maintenance, and/or which illustrate its intended use.

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