Vending Machine
Здравейте,
Някакви насоки за това решение - 85/100.
https://pastebin.com/fhYjUfC0
Благодаря
Здравейте,
Някакви насоки за това решение - 85/100.
https://pastebin.com/fhYjUfC0
Благодаря
Здравей ето така дава 100/100.
import java.util.Scanner;
public class P07VendingMachine {
public static void main(String[] args) {
/*Your task is to calculate the total purchase price from a vending machine.
Until you receive "Start" you will be given different coins that are being
inserted into the machine. You have to sum them to have the total money inserted.
There is a problem though. Your vending machine only works with 0.1, 0.2, 0.5, 1,
and 2 coins. If someone tries to insert some other coins, you have to display
"Cannot accept {money}", where the value is formatted to the second digit after
the decimal point and not add it to the total money. On the next few lines
until you receive "End" you will be given products to purchase. Your machine has,
however, only "Nuts", "Water", "Crisps", "Soda", "Coke". The prices are: 2.0, 0.7,
1.5, 0.8, 1.0 respectively. If the person tries to purchase a not existing product,
print "Invalid product". Be careful that the person may try to purchase a product
for which he doesn't have money. In that case, print "Sorry, not enough money".
If the person purchases a product successfully print "Purchased {product name}".
After the "End" command, print the money that is left formatted to the second
decimal point in the format "Change: {money left}".
* */
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
double sum = 0;
double change = sum;
boolean isEnoughMoney = false;
while (!input.equals("Start")) {
double coints = Double.parseDouble(input);
if (coints == 0.1 || coints == 0.2 || coints == 0.5 || coints == 1 || coints == 2) {
sum += coints;
} else {
System.out.printf("Cannot accept %.2f%n", coints);
}
input = scanner.nextLine();
}
while (!input.equals("End")) {
String choice = input;
if (input.equals("Start")) {
input = scanner.nextLine();
continue;
}
if (choice.equals("Nuts") && sum >= 2) {
sum = sum - 2;
} else if (choice.equals("Water") && sum >= 0.7) {
sum = sum - 0.7;
} else if (choice.equals("Crisps") && sum >= 1.5) {
sum = sum - 1.5;
} else if (choice.equals("Soda") && sum >= 0.8) {
sum = sum - 0.8;
} else if (choice.equals("Coke") && sum >= 1) {
sum = sum - 1;
} else if (!choice.equals("Coke") && !choice.equals("Soda") && !choice.equals("Crisps")
&& !choice.equals("Water") && !choice.equals("Nuts")) {
System.out.println("Invalid product");
input = scanner.nextLine();
continue;
} else {
System.out.println("Sorry, not enough money");
isEnoughMoney = true;
}
if (!isEnoughMoney) {
System.out.printf("Purchased %s%n", choice);
}
isEnoughMoney = false;
input = scanner.nextLine();
}
System.out.printf("Change: %.2f", sum);
}
}
Благодаря, чесно казано доста я помъчих, но точно за това нямаше да се сетя.