// Definition of class Company
// Class Company represents a company that produces several products
package TestCompany;
public class Company {
private int [][] hours = {{20, 10, 15, 13},
{18, 11, 11, 10},
{28, 0, 16, 17 }};
private double [] cost = {140.0, 295.5, 225.0, 95.5};
private int [] total_hours;
// constructor
public Company()
{
total_hours = new int [cost.length];
}
// find the total cost of processing all the products
public double total()
{
double total_cost = 0;
// go over all the departments
for ( int j=0; j < hours[0].length; j++)
{
int sum_hours =0;
// find the number of hours each deaprtment spends on all products
// and store in the array
for ( int i=0; i < hours.length; i++ )
sum_hours = sum_hours + hours[i][j] ;
total_hours [j] = sum_hours;
total_cost = total_cost + sum_hours * cost[j];
}
return total_cost;
}
// find for which department the cost of producing a certain product was the
// most expensive
public int maximal ( int product )
{
double max = 0;
int dept = 0;
// go over all the departments
for ( int i=0; i < cost.length; i++)
{
// compute the cost of producing the product by a certain department
double total = hours [product] [i] * cost [i];
if ( total > max )
{
max = total;
dept = i;
}
}
return dept;
}
}
// Test class Company
package TestCompany;
import javax.swing.JOptionPane; // import class JOptionPane
public class TestCompany {
public static void main( String args[] )
{
// initialize the Company object
Company c = new Company();
//send message to object to find the total cost of processing all the products
double cost = c.total();
JOptionPane.showMessageDialog(
null, "the total cost of processing all the products is " + cost );
// send message to object to find for which department the cost of processing a
// certain product was the most expensive
int product = 1;
int dept = c.maximal (product);
JOptionPane.showMessageDialog(
null,
"the cost of processing the product was the most expensive for department "
+ dept );
System.exit( 0 ); // terminate the program
}
}