very inexperienced coder here. I have recently been tasked with creating a baking calculator. Whenever I run this program, it gives me very inaccurate values and sometimes even rounds to zero. What are the biggest mistakes I'm making in this code?
I've tried to compare it to another peers code but I still don't see the exact mistake I'm making. Really just looking for some information that I could use to fix this code.
public class BakingCalculator {
public static int bagsOfFlour(int cookieCount, int loafCount) {
cookieCount = 48;
loafCount = 1;
double cups_of_flour = (cookieCount*0.046875) + (loafCount*1.5);
int bags_of_flour = (int)Math.ceil(cups_of_flour/16.67);
return bags_of_flour;
// Every four dozen cookies needs 2 1/4 cups of flour.
// Every loaf of banana bread requires 1 1/2 cups of flour.
// Each bag of flour is 5 lbs and contains 3 1/3 cups of flour per pound or 16 2/3 cups total.
}
public static int containersOfSalt(int cookieCount, int loafCount) {
double teaspoons_of_salt = (cookieCount*(0.020834) + loafCount*(0.125));
int containers_of_salt = (int)Math.ceil(teaspoons_of_salt/156);
return containers_of_salt;
// Every four dozen cookies needs 1 teaspoon of salt.
// Every loaf of banana bread needs 1/8 of a teaspoon of salt.
// Each container of salt is 26 oz and there are 6 teaspoons in 1 oz.
}
public static int boxesOfBakingSoda(int cookieCount, int loafCount) {
double teaspoons_of_baking_soda = (cookieCount*(0.020834) + loafCount*(1.0));
int boxes_of_baking_soda = (int)Math.ceil(teaspoons_of_baking_soda/96);
return boxes_of_baking_soda;
// Every four dozen cookies needs 1 teaspoon of baking soda.
// Every loaf of banana bread needs 1 teaspoon of baking soda.
// Each container of baking soda is 16 oz and there are 6 teaspoons in 1 oz.
}
public static int bottlesOfVanilla(int cookieCount, int loafCount) {
double teaspoons_of_vanilla = (cookieCount*(0.020834) + loafCount*(1.0));
int bottles_of_vanilla = (int)Math.ceil(teaspoons_of_vanilla/6);
return bottles_of_vanilla;
// Every four dozen cookies needs 1 teaspoon of vanilla extract.
// Every loaf of banana bread needs 1 teaspoon of vanilla extract.
// Vanilla extract comes as an ounce and there are 6 teaspoons in 1 liquid ounce.
}
public static int cartonsOfEggs(int cookieCount, int loafCount) {
double eggs = (cookieCount*(0.04167) + loafCount*(1.0));
int egg_cartons = (int)Math.ceil(eggs/12);
return egg_cartons;
// Every four dozen cookies needs 2 eggs.
// Every loaf of banana bread needs 1 egg.
// Each carton of eggs contains 12 eggs.
}
public static int bagsOfSugar(int cookieCount, int loafCount) {
double cups_of_sugar = (cookieCount*(0.03125) + loafCount*(0.75));
int bags_of_sugar = (int)Math.ceil(cups_of_sugar/2);
return bags_of_sugar;
// Every four dozen cookies needs 1 1/2 cups of sugar.
// Every loaf of banana bread needs 3/4 cups of sugar.
// Each bag of sugar is 1 lb and contains 2 cups of sugar.
}
public static int packagesOfButter(int cookieCount, int loafCount) {
double cups_of_butter = (cookieCount*(0.02083) + loafCount*(0.33334));
int packages_of_butter = (int)Math.ceil(cups_of_butter/2);
return packages_of_butter;
// Every four dozen cookies needs 1 cup (2 sticks) of butter.
// Every loaf of banana bread needs 1/3 cups of butter.
// Each package of butter is 1 lb and contains 4 sticks.
}
public static int bananas(int loafCount) {
return loafCount*(3);
// Every loaf of banana bread needs 3 bananas
// Bananas are sold individually.
}
public static int bagsOfChocolateChips(int cookieCount) {
double cups_of_chips = cookieCount*(0.052083);
int bags_of_chips = (int)Math.ceil(cups_of_chips/2);
return bags_of_chips;
// Every four dozen cookies needs 2 1/2 cups of chocolate chips.
// Each bag of chocolate chips is 2 cups.
}
public static double totalCost(int cookieCount, int loafCount) {
// Flour is $1.79.
double flourCost = bagsOfFlour(cookieCount, loafCount)*1.79;
// Salt is $1.09.
double saltCost = containersOfSalt(cookieCount, loafCount)*1.09;
// Baking soda is $1.09.
double bakingSodaCost = boxesOfBakingSoda(cookieCount, loafCount)*1.09;
// Vanilla extract is $3.99.
double vanillaCost = bottlesOfVanilla(cookieCount, loafCount)*3.99;
// Eggs are $2.19.
double eggCost = cartonsOfEggs(cookieCount, loafCount)*2.19;
// Sugar is $1.99.
double sugarCost = bagsOfSugar(cookieCount, loafCount)*1.99;
// Butter is $4.49.
double butterCost = packagesOfButter(cookieCount, loafCount)*4.49;
// Bananas are $0.32.
double bananaCost = bananas(loafCount)*0.32;
// Chocolate chips are $3.29.
double chipCost = bagsOfChocolateChips(cookieCount)*3.29;
return flourCost + saltCost + bakingSodaCost + vanillaCost + eggCost + sugarCost + butterCost + bananaCost + chipCost;
}
public static void main(String[] args) {
int cookieCount = 48;
int loafCount = 1;
System.out.println("Number of cookies = " + cookieCount);
System.out.println("Number of banana bread loaves = " + loafCount);
System.out.println("Bags of flour = " + bagsOfFlour(cookieCount, loafCount));
System.out.println("Containers of salt = " + containersOfSalt(cookieCount, loafCount));
System.out.println("Boxes of baking soda = " + boxesOfBakingSoda(cookieCount, loafCount));
System.out.println("Bottles of vanilla = " + bottlesOfVanilla(cookieCount, loafCount));
System.out.println("Cartons of eggs = " + cartonsOfEggs(cookieCount, loafCount));
System.out.println("Bags of sugar = " + bagsOfSugar(cookieCount, loafCount));
System.out.println("Packages of butter = " + packagesOfButter(cookieCount, loafCount));
System.out.println("Bananas = " + bananas(loafCount));
System.out.println("Bags of chocolate chips = " + bagsOfChocolateChips(cookieCount));
System.out.println("Total cost = " + totalCost(cookieCount, loafCount));
}
}