Skip to content

Instantly share code, notes, and snippets.

@avermeulen
Last active July 12, 2019 10:19
Show Gist options
  • Save avermeulen/d488ff4e1a2553e66b42a0a396b96e89 to your computer and use it in GitHub Desktop.
Save avermeulen/d488ff4e1a2553e66b42a0a396b96e89 to your computer and use it in GitHub Desktop.
A small challenge at codeX about conditional statements while preparing for the OCA exam.

Apple stock count

Change the program to work of multiples of dozens of apples.

For 12 apples it should return: You have bought 1 dozen of apples for a total cost of 54.0.

This should work for any multiple of 12.

For 36 apples it should return: You have bought 3 dozens of apples for a total cost of 138.0.

Extra challenge:

  • Format the prices to 2 decimal points.
  • Supply the price of an apple as a parameter when running the program.
public class AppleStockCount {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Please give me a qty!");
return;
}
int qty = Integer.parseInt(args[0]);
double cost = 4.50;
double total;
if (qty < 1) {
System.out.println("Out of stock.");
}
else {
total = qty * cost;
StringBuilder message = new StringBuilder();
message.append("You have bought ");
message.append(qty);
message.append(" apple");
if (qty > 1) {
message.append("s");
}
message.append(" for a total cost of ");
message.append(total);
message.append(".");
System.out.println(message.toString());
}
}
}
@avermeulen
Copy link
Author

avermeulen commented Jul 11, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment