Find highest earning company from while loop

30 views Asked by At

I need to find the company with the highest earnings.

So far I am able to pull out the highest total Earnings from the loop but have no idea how to get the company name that ties with this highest earnings.

while( count <= noOfComp)

{   
    System.out.print("Enter Company: ");
    companyName = kb.nextLine();

    System.out.print("Number of hires: ");
    noOfHires = kb.nextInt();
    kb.nextLine();


    //calculations
    totalEarnings = noOfHires * 2500 + 10000;
    System.out.println("Total Earnings of company is :" + totalEarnings);
    totalEarned = "" + totalEarnings;



    if(totalEarnings > large )                          //If statement for largest number
    {
     large = totalEarnings;
    }


    allTotalEarnings += totalEarnings;
    count++;


}
1

There are 1 answers

0
Sridhar DD On BEST ANSWER

You can assign the highest earning company name and its earnings in variables after the calculation by comparing with previous highest with calculated one.

String highCompanyName = "";int highCompanyEarning = 0;
while( count <= noOfComp)
{   
    System.out.print("Enter Company: ");
    companyName = kb.nextLine();

    System.out.print("Number of hires: ");
    noOfHires = kb.nextInt();
    kb.nextLine();


    //calculations
    totalEarnings = noOfHires * 2500 + 10000;
    System.out.println("Total Earnings of company is :" + totalEarnings);
    totalEarned = "" + totalEarnings;


    if(totalEarnings> highCompanyEarning )  //If statement for largest number
    {
        highCompanyEarning = totalEarnings;
        highCompanyName = companyName;
    }


    allTotalEarnings += totalEarnings;//the purpose of it is not clear so left as it is.
    count++;
 }
 System.out.println("Highest Earnings company is :" + highCompanyName );
 System.out.println("Earning of that company is :" + highCompanyEarning );