Write a Java program to define a class Tender containing data members cost and company name. Accept data for five objects and display company name for which cost is minimum.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Tender {
String name; // Data members declaration
int cost;
Tender() // Default constructor
{
name = null;
cost = 0;
}
Tender(String s,int a) // Parametrized constructor
{
name = s;
cost = a;
}
void disp() {
System.out.println("Company name="+name+" cost="+cost);
}
public static void main(String args[])throws IOException
{
Tender T[] = new Tender[5]; // Creating array of objects
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
String s;
int c,temp = 0,min=0;
for(int i=0;i<5;i++)
{
System.out.println("Enter company name :: "); // accept Company name
s = b.readLine();
System.out.println("Enter the cost :: "); // accept the cost
c = Integer.parseInt(b.readLine()); // Converting string to int
T[i] = new Tender(s,c); // assign values to data members by constructor
}
for(int i=0;i<5;i++)
{
if(T[i].cost<min)
{
min = T[i].cost; // store minimum value of cost
temp = i; // store index value of object having minimum cost
}
}
System.out.println("Company having minimum cost=");
T[temp].disp();
}
}
C:\>javac Tender.java
C:\>java Tender
Enter company name ::
TechInfo
Enter the cost ::
120365
Enter company name ::
ITInfoTech
Enter the cost ::
154231
Enter company name ::
ITSystems
Enter the cost ::
100235
Enter company name ::
CompTech
Enter the cost ::
90452
Enter company name ::
iSoftSys
Enter the cost ::
135421
Company having minimum cost=
Company name=CompTech cost=90452