Energy drink consumption

The problem

A soft drink company recently surveyed 12,467 of its customers and found that approximately 14 percent of those surveyed purchase one or more energy drinks per week. Of those customers who purchase energy drinks, approximately 64 percent of them prefer citrus flavored energy drinks. Write a program that displays the following:

  • The approximate number of customers in the survey who purchased one or more energy drinks per week
  • The approximate number of customers in the survey who prefer citrus flavored energy drinks

Breaking it down

// constants
final static int NUMBERED_SURVEYED = 12467;
final static double PURCHASED_ENERGY_DRINKS = 0.14;
final static double PREFER_CITRUS_DRINKS = 0.64;

public static void main(String[] args) {

    // Variables
    double energyDrinkers = calculateEnergyDrinkers(NUMBERED_SURVEYED);
    double preferCitrus = calculatePreferCitris(NUMBERED_SURVEYED);

    // Display the results.
    System.out.println("Total number of people surveyed "
            + NUMBERED_SURVEYED);
    System.out.println("Approximately " + energyDrinkers
            + " bought at least one energy drink");
    System.out.println(preferCitrus + " of those "
            + "prefer citrus flavored energy drinks.");
}

/**
 * Calculate the number of energy drinkers.
 * 
 * @param numberSurveyed
 * @return
 */
public static double calculateEnergyDrinkers(int numberSurveyed) {
    return numberSurveyed * PURCHASED_ENERGY_DRINKS;
}

/**
 * Calculate the number of energy drinkers that prefer citrus flavor.
 * 
 * @param numberSurveyed
 * @return
 */
public static double calculatePreferCitris(int numberSurveyed) {
    return numberSurveyed * PREFER_CITRUS_DRINKS;
}

Output

Total number of people surveyed 12467
Approximately 1745.38 bought at least one energy drink
7978.88 of those prefer citrus flavored energy drinks.

Unit tests

@Test
public void test_calculateEnergyDrinkers() {

    double val = EnergyDrinkConsumption.calculateEnergyDrinkers(100);

    assertEquals(14.000000000000002, val, 0);
}

@Test
public void test_calculatePreferCitris() {

    double val = EnergyDrinkConsumption.calculatePreferCitris(100);

    assertEquals(64, val, 0);

}

Level Up

  • Format the number of people surveyed with commas
  • Since it isn't possible to have parts of a person, round up or down to have a whole number in the calculateEnergyDrinkers and calculatePreferCitris methods.