Sample Code: Break Even Analysis All In Main Method
By Lon Hosford
Here is sample code that uses only the main method with both static input values and dynamically generated numbers. It demonstrations using the Math and DecimalFormat classes from the Java API and expression writing.
 
/*---------------------------------------------
    Required for the DecimalFormat class
---------------------------------------------*/
import java.text.*;
 
public class BreakEvenCalculator
{
 

    /*---------------------------------------------
        Program starts here. No arguments are expected.
    ---------------------------------------------*/
    public static void main(String [] args)
    {
        /*---------------------------------------------
            Decalre variables private
            to the main method
        ---------------------------------------------*/
 
            /*---------------------------------------------
                Price per unit
            ---------------------------------------------*/
            float   pricePerUnit;
            /*---------------------------------------------
                Cost per unit
            ---------------------------------------------*/
            float   costPerUnit;
            /*---------------------------------------------
                Fixed costs
            ---------------------------------------------*/
            float   fixedCosts;
            /*---------------------------------------------
                Break even units
            ---------------------------------------------*/
            float breakEvenUnits;
 
        /*---------------------------------------------
            Test case #1 inputs
                pricePerUnit 10.00
                costPerUnit  7.50
                fixedCosts   10000.00
 
        ---------------------------------------------*/
        pricePerUnit = 10.00f;
        costPerUnit = 7.5f;
        fixedCosts = 10000f;
        breakEvenUnits = fixedCosts / (pricePerUnit - costPerUnit);
 
        /*---------------------------------------------
            Test case #1 outputs
        ---------------------------------------------*/
        System.out.println("Test case #1 - Fixed inputs");
 
        System.out.println("Price: \t\t"
            + new DecimalFormat("$#,##0.00#").format( Math.round(pricePerUnit * 100) / 100 ));
        System.out.println("Unit cost: \t"
            + new DecimalFormat("$#,##0.00#").format( Math.round(costPerUnit * 100) / 100 ));
        System.out.println("Fixed costs: \t"
            + new DecimalFormat("$#,##0.00#").format( fixedCosts ));
        System.out.println("Breakeven units: "
            + new DecimalFormat("$#,##0.00#").format( breakEvenUnits ));
        System.out.println();
 

        /*---------------------------------------------
            Test case #2 inputs
                pricePerUnit 1.00 - 25.00
                costPerUnit  Smaller of 1.00 - 25.00
                             or pricePerUnit - .01
                fixedCosts   5,000.00 - 15000.00
        ---------------------------------------------*/
        pricePerUnit = (float)( Math.random() * 24) + 1;// 1.00 - 25.00
        costPerUnit  = (float)
            Math.min(                                   // Smaller of
                        ( Math.random() * 24 ) + 1,     // 1.00 - 25.00
                        pricePerUnit - .01              // pricePerUnit - .01
                    );
        fixedCosts   = (float)(
                                    Math.random()       // 5,000.00 - 15,000.00
                                *   14000
                              ) + 5000;
 
        /*---------------------------------------------
            Test case #2 outputs
        ---------------------------------------------*/
        System.out.println("Test case #2 - Random values");
        System.out.println("Price: \t\t"
            + new DecimalFormat("$#,##0.00#").format( Math.round(pricePerUnit * 100) / 100 ));
        System.out.println("Unit cost: \t"
            + new DecimalFormat("$#,##0.00#").format( Math.round(costPerUnit * 100) / 100 ));
        System.out.println("Fixed costs: \t"
            + new DecimalFormat("$#,##0.00#").format( fixedCosts ));
        System.out.println("Breakeven units: "
            + new DecimalFormat("$#,##0.00#").format( breakEvenUnits ));
        System.out.println();
    }
 
 
 
}