Looks good.
Nothing obviously wrong and certainly neater than mine...
Very scrappy at this Java thing still...
import FormatIO.Console;
public class Ex2 {
// Define constants, delivery charge and number of people.
private static final int DELIVERY = 450, NUMBER_OF_PEOPLE = 3, ITEM_NUMBER_DISCOUNT_CHECK = 40;
public static void main(String[] args) {
// Define variables.
String itemName;
int itemNumber, itemsEach, firstPersonItems, itemRemainder;
double firstPersonCost, itemCost, totalCost, individualCost, costRemainder, totalCostDiscountCheck;
Console con = new Console();
// Get input for item name, quantity and cost.
con.println("Please enter the item you wish to order. (as a single word):");
itemName = con.readLine();
con.println("Please enter how many of this item you wish to order:");
itemNumber = con.readInt();
while (itemNumber < NUMBER_OF_PEOPLE) {
con.println("Please order at least three items as whole items:");
itemNumber = con.readInt();
}
// Within, costs for known items.
if (itemName.equalsIgnoreCase("battery")) {
itemCost = 50;
con.println("Batterries cost £0.50 each.");
}
else if (itemName.equalsIgnoreCase("lightbulb")) {
itemCost = 77;
con.println("Lightbulbs cost £0.77 each.");
}
else if (itemName.equalsIgnoreCase("torch")) {
itemCost = 399;
con.println("Torches cost £3.99 each.");
}
else {
con.println("Please enter the cost of one item:");
con.print("£");
itemCost = (int) con.readDouble() * 100;
while (itemCost < 1) {
con.println("Please enter an item cost of greater than £0.01:");
con.print("£");
itemCost = (int) con.readDouble() * 100;
}
}
// Calculate total cost.
totalCost = (itemCost * itemNumber) + DELIVERY;
// Within, discount on more than 40 items.
if (itemNumber >= 40) {
totalCost = (totalCost / 100) * 90;
}
else { // need to check if increasing order to get discount would be cheaper.
totalCostDiscountCheck = (itemCost * ITEM_NUMBER_DISCOUNT_CHECK) + DELIVERY;
totalCostDiscountCheck = (totalCostDiscountCheck / 100) * 90;
if (totalCostDiscountCheck < totalCost) {
totalCost = totalCostDiscountCheck;
itemNumber = 40;
con.println("There is a 10% discount on items of 40 or more items.");
con.println("The number of items you have ordered has been incresed to 40 to allow you to\ntake advantage of this offer as it decreases your order total.");
}
}
// Calculate individual costs and number of items received.
costRemainder = totalCost % 3;
individualCost = (totalCost - costRemainder) / 3;
firstPersonCost = (individualCost + costRemainder) / 100;
individualCost /= 100;
itemRemainder = itemNumber % 3;
itemsEach = itemNumber / 3;
firstPersonItems = itemsEach + itemRemainder;
// Output for amounts to pay.
if (costRemainder > 0) con.println("\nThe first person needs to pay £" + String.format("%4.2f", firstPersonCost) + ".\nThe others need to pay £" + String.format("%4.2f", individualCost) + " each.");
else con.println("\nEach person needs to pay £" + String.format("%4.2f", individualCost) + ".");
// Output for items received.
if (itemRemainder > 0) {
con.print("\nThe first person gets " + firstPersonItems + " ");
if (firstPersonItems > 1) con.print("items");
else con.print("item");
con.print(" and the others get " + itemsEach + " ");
if (itemsEach > 1) con.print("items");
else con.print("item");
con.print(" each.");
}
else {
con.print("\nEach person gets " + itemsEach + " ");
if (itemsEach > 1) con.print("items.");
else con.print("item.");
}
// Niceness...
if (itemName.equalsIgnoreCase("fish")) con.println("\n\nToday's fish is Trout A La Creme, enjoy your meal.");
else con.println("\n\nEnjoy your " + itemName);
}
}