In: Computer Science
In Java): A vending machine serves chips, fruit, nuts, juice, water, and coffee. The machine owner wants a daily report indicating what items sold that day. Given boolean values (true or false) indicating whether or not at least one of each item was sold (in the order chips, fruit, nuts, juice, water, and coffee), output a list for the owner. If all three snacks were sold, output "All-snacks" instead of individual snacks. Likewise, output "All-drinks" if appropriate. For coding simplicity, output a space after every item, including the last item.
/**
*
* problem
*
* A vending machine serves chips, fruit, nuts, juice, water,
and
* coffee. The machine owner wants a daily report indicating what
items
* sold that day. Given boolean values (true or false)
indicating
* whether or not at least one of each item was sold (in the
order
* chips, fruit, nuts, juice, water, and coffee), output a list for
the
* owner. If all three snacks were sold, output "All-snacks" instead
of
* individual snacks. Likewise, output "All-drinks" if appropriate.
For
* coding simplicity, output a space after every item, including
the
* last item.
*/
public class ItemsSold {
public static void main(String arg[]) {
boolean isChipsSold = true,
isFruitSold = true, isNutsSold = false, isJuiceSold = false,
isWaterSold = false,
isCoffeeSold = false;
boolean snacksSold = isChipsSold
&& isFruitSold && isNutsSold;
boolean drinksSold = isJuiceSold
&& isWaterSold && isCoffeeSold;
if (snacksSold) {
System.out.println("All-snacks");
} else {
if (isChipsSold)
{
System.out.println("Chips sold");
}
if
(isFruitSold) {
System.out.println("Fruit sold");
} if
(isNutsSold) {
System.out.println("Nuts sold");
}
}
if (drinksSold) {
System.out.println("All-drinks");
} else {
if (isJuiceSold)
{
System.out.println("Juice sold");
}
if
(isWaterSold) {
System.out.println("Water sold");
} if
(isCoffeeSold) {
System.out.println("Coffee sold");
}
}
}
}