Skip to content

Instantly share code, notes, and snippets.

@KnightsWhoSayNi0
Last active November 28, 2022 03:14
Show Gist options
  • Save KnightsWhoSayNi0/b9c87827868c7b29e31b75d538bb6291 to your computer and use it in GitHub Desktop.
Save KnightsWhoSayNi0/b9c87827868c7b29e31b75d538bb6291 to your computer and use it in GitHub Desktop.
Barstucks
/**
* STRAZZA Pd. 8
* PGM 4
* @author john
* @see Coffee
*/
public class Barista
{ /**
* Input of coffee orders in an array. Graded data from Strazza.
*/
final static int[] coffeePours = {22, 22, 4, 22, 22, 4, -16, -16, 16, 8, 10, 14, 16, 17, 23, 3, 15, 16, 8, 4, 4, 4, 8, 12, -6, 12 };
public static void main(String[] args)
{ Coffee myCoffee = new Coffee();
// for each value in coffeePours, run the pour method
for (int pour : coffeePours)
{ myCoffee.pour(pour);
} } }
/**
* STRAZZA Pd. 8
* PGM 4
* @author john
* @see Barista
*/
public class Coffee
{ final int POT_SIZE = 48, // capacity of pots measured in ounces
MIN_POUR = 4, // smallest pour is not less than 4 ounces
MAX_POUR = 22, // largest coffee pour will not be larger than 22 ounces
NUM_POTS = 2;
int lastBrew = 0; // last pot which was brewed in; used for swapping
private int[] pots = new int[NUM_POTS];
Coffee()
{ for (int i = 0; i < pots.length; i++)
brew(i); // brew in all pots from the start
}
/**
* Brew a new cup of coffee in the given pot.
* @param pot Given pot to brew in.
*/
void brew(int pot)
{ if (pot > -1 && pot <= pots.length)
{ pots[pot] = POT_SIZE;
lastBrew = pot;
System.out.printf("New coffee started in Pot %d\r", ++pot);
} }
/**
* Pour a cup of coffee with a given amount.
* @param amount Given amount to pour. Negative amount implies a spill.
*/
void pour(int amount)
{ boolean spill = (amount < 0);
boolean dualPot = false;
int activePot = (lastBrew == 0) ? 1 : 0; // swap pots by default
/* check for a negative number which implies a spill
* by making the amount positive and using a flag for a spill,
* the rest of the program can use the proper amount while
* still knowing that there is supposed to be a spill
*/
amount = spill ? -amount : amount;
// check if the pour is within boundaries and cancel pour if not
if (amount < MIN_POUR || amount > MAX_POUR)
{ System.out.printf("Can't pour %d oz\r", amount);
return;
}
// check if the active pot is not empty, but isn't enough to pour
dualPot = (pots[activePot] > 0 && pots[activePot] < amount);
// if not using two pots, can the current pot fill the pour?
if (!dualPot && pots[activePot] < amount)
activePot = (activePot == 0) ? 1 : 0;
if (!dualPot)
{ pots[activePot] -= amount; // pour from pot
System.out.printf("%d oz cup poured from Pot %d%s\r",
amount,
activePot + 1, // + 1 for formatting, array begins @ 0
(spill ? ", spilled." : "")); // append formatting
} else
{ System.out.printf("%d oz order: %d oz poured from Pot %d. ",
amount,
pots[activePot],
activePot + 1);
amount -= pots[activePot]; // find remaining amount to pour
pots[activePot] = 0; // empty first pot
activePot = (activePot == 0) ? 1 : 0; // switch pots
System.out.printf("%d oz poured from Pot %d\r",
amount,
activePot + 1);
pots[activePot] -= amount; // pour from second pot
}
// pour a new cup with the positive amount if there is a spill
if (spill)
{ System.out.print("Restarted ");
pour(amount);
}
// if pot was emptied, brew a new pot
for (int i = 0; i < NUM_POTS; i++)
if (pots[i] <= 0)
brew(i);
} }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment