Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Forked from anonymous/Bake.java
Last active January 11, 2017 20:30
Show Gist options
  • Save JossWhittle/e30b252dec1f9d4f11871e56249708fc to your computer and use it in GitHub Desktop.
Save JossWhittle/e30b252dec1f9d4f11871e56249708fc to your computer and use it in GitHub Desktop.
import java.util.Scanner;
public class Bake {
// Constants for bag sizes
static final double[] FLR_SIZES = { 750, 500, 250 };
static final double[] SUG_SIZES = { 600, 400, 200 };
static final double[] BUT_SIZES = { 500, 250, 125 };
static final double[] EGG_SIZES = { 12, 10, 6 };
// Constants for cupcake recipe
static final double CUP_FLR = 12;
static final double CUP_SUG = 14;
static final double CUP_BUT = 4;
static final double CUP_EGG = 0.1;
// Constants for drizzle cake recipe
static final double DRZ_FLR = 240;
static final double DRZ_SUG = 300;
static final double DRZ_BUT = 80;
static final double DRZ_EGG = 4.5;
public static void main(String[] args) {
// Scanner to get console input
Scanner input = new Scanner(System.in);
// Get quantities of each cake
double numCupCakes = -1, numDrzCakes = -1;
// Query the user until they have given positive quantities
System.out.printf("Please enter a (positive) number of Cupcakes to order:\n");
while (numCupCakes < 0) {
numCupCakes = input.nextInt();
}
System.out.printf("Please enter a (positive) number of Drizzle Cakes to order:\n");
while (numDrzCakes < 0) {
numDrzCakes = input.nextInt();
}
// Prompt back what the user asked for
System.out.printf("\nYou have ordered %d Cupcake(s) and %d Drizzle Cake(s).\n",
(int) numCupCakes, (int) numDrzCakes);
// Calculate total quantities of each ingredient
final double flr = (numCupCakes * CUP_FLR) + (numDrzCakes * DRZ_FLR);
final double sug = (numCupCakes * CUP_SUG) + (numDrzCakes * DRZ_SUG);
final double but = (numCupCakes * CUP_BUT) + (numDrzCakes * DRZ_BUT);
final double egg = (numCupCakes * CUP_EGG) + (numDrzCakes * DRZ_EGG);
// Calculate the number and types of bags needed to satisfy the order
System.out.printf("\nIngredients to dispatch:\n");
calcBags("Flour", flr, FLR_SIZES);
calcBags("Sugar", sug, SUG_SIZES);
calcBags("Butter", but, BUT_SIZES);
calcBags("Eggs", egg, EGG_SIZES);
}
public static void calcBags(String name, double amount, double[] sizes) {
// Start with none of each bag
int[] num = { 0, 0, 0 };
// Start packing with the largest bags first
int currentSize = 0;
// Keep packing bags until there's nothing left to pack
double amountLeft = amount;
while (amountLeft > 0) {
// Pack the current sized bag
if ((amountLeft >= sizes[currentSize]) || (currentSize == (sizes.length - 1))) {
amountLeft -= sizes[currentSize];
num[currentSize]++;
}
else {
// Otherwise go to the next smallest bag size
currentSize++;
}
}
// Output the result to console
System.out.printf("%-10s | Requested %-10.0f | Supplied %-10.0f | %-3d Small | %-3d Medium | %-3d Large\n",
name, amount, (amount - amountLeft), num[2], num[1], num[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment