Instructions
Requirements and Specifications
- Write the pseudocode for the following program.
- Create a flowchart.
- Design a program based on the below information.
- Proper naming conventions.
- Proper use of comments.
- Using objects.
- Providing well-formatted user output.
- 5. Concise writing of code, if you can write it in two lines instead of five, two lines is typically nicer.
- Do not write repetitive code.
- Use proper methods/syntax.
- Using arrays.
- Using sentinel value.
- Code is formatted properly.
- Code output is formatted as if it were a receipt
- Error check user input as needed to run program properly.
Source Code
import java.text.SimpleDateFormat;
import java.util.Date;
public class CorgiWorkshop {
// This our constants to easier to read the array of money denominations
private static final int TENS = 0;
private static final int FIVES = 1;
private static final int ONES = 2;
private static final int QUARTERS = 3;
private static final int DIMES = 4;
private static final int NICKELS = 5;
private static final int PENNIES = 6;
// $10 = 1000 pennies
// $5 = 500 pennies
// $1 = 100 pennies
// Quarter = 25 pennies
// Dimes = 10 pennies
// Nickels = 5 pennies
// This will be useful for calculating based on the available bills
private static final int[] PENNIES_DENOMINATIONS = {
10 * 100,
5 * 100,
100,
25,
10,
5,
1
};
// Initialize available items and prices
private static final String[] FURS = {"Large Red and White", "Large Tri-color"};
private static final double[] FUR_PRICES = {12, 13.50};
private static final String[] OUTFITS = {"Shark", "Mermaid", "Swim trunks"};
private static final double[] OUTFIT_PRICES = {6.49, 8, 6.99};
private static final String[] SOUNDS = {"Puppy 5-in-1 sounds", "Personalized message", "I love you", "Heartbeat"};
private static final double[] SOUND_PRICES = {4.49, 3.47, 2.99, 2.49};
private static final String[] ACCESSORIES = {"Beach ball", "Pet carrier", "Radio flyer wagon", "Water cooler"};
private static final double[] ACCESSORY_PRICES = {3.50, 7.99, 10, 3.29};
// Create a starting money for a customer
// Start with 1 x $10, 2 x $5, 6 x $1,
// 6 quarters, 10 dimes, 20 nickels, and 50 pennies
private static int[] initializeStartingMoney() {
int[] money = new int[7];
// These all stores the pennies rate of each bill
money[TENS] = 1 * 10 * 100;
money[FIVES] = 2 * 5 * 100;
money[ONES] = 6 * 100;
money[QUARTERS] = 6 * 25;
money[DIMES] = 10 * 10;
money[NICKELS] = 20 * 5;
money[PENNIES] = 50;
return money;
}
// Force the user to choose from one of the given
// options then returns the cost of the selected option
private static int chooseOne(String optionName, String[] options, double[] prices) {
System.out.println(optionName);
for (int i = 0; i < options.length; i++) {
System.out.println((i + 1) + ". " + options[i] + " - $" + String.format("%.2f", prices[i]));
}
System.out.print("Option: ");
int option = Keyboard.readInt(1, options.length);
return option - 1;
}
// Start a new customer, give them $30 in different
// denominations, then make them shop some items
private static void processNextCustomer(String customerName) {
System.out.println("Hello " + customerName + ", let's buy you some Corgi!");
int[] money = initializeStartingMoney();
double totalCost = 0;
int furOption, outfitOption, soundOption;
boolean[] chosenAccessories;
do {
chosenAccessories = new boolean[ACCESSORIES.length];
// Shop for fur
furOption = chooseOne("Corgi Fur: Choose One", FURS, FUR_PRICES);
System.out.println("Excellent! Your Corgi would love the " + FURS[furOption] + " fur.");
// Shop for outfit
outfitOption = chooseOne("Corgi Outfit: Choose One", OUTFITS, OUTFIT_PRICES);
System.out.println("Great choice! Your Corgi will make an excellent " + OUTFITS[outfitOption] + ".");
// Shop for sound
soundOption = chooseOne("Corgi Sound: Choose One", SOUNDS, SOUND_PRICES);
System.out.println("Nice, Corgi loves " + SOUNDS[soundOption] + " sounds.");
// Shop for accessories
boolean moreAccessory;
do {
System.out.print("Do you want to add an accessory? (Y/N) ");
moreAccessory = Keyboard.readBoolean();
if (moreAccessory) {
int accessoryOption = chooseOne("Corgi Accessory: Choose One", ACCESSORIES, ACCESSORY_PRICES);
if (chosenAccessories[accessoryOption]) {
System.out.println("You have that previously chosen that already.");
} else {
System.out.println("Awesome! Your corgi will love that accessory!");
chosenAccessories[accessoryOption] = true;
}
}
} while (moreAccessory);
// Calculate the total cost
totalCost = FUR_PRICES[furOption] + OUTFIT_PRICES[outfitOption] + SOUND_PRICES[soundOption];
for (int i = 0; i < ACCESSORIES.length; i++) {
if (chosenAccessories[i]) {
totalCost += ACCESSORY_PRICES[i];
}
}
// We only have $30 initially so if it doesn't add up
// then re-do shopping
System.out.println("Total cost: $" + String.format("%.2f", totalCost));
if (totalCost > 30) {
System.out.println("Oops. You do not have enough budget. You might want to re-select again your choices.");
}
} while (totalCost > 30);
// Calculate the cost in pennies
int costInPennies = (int) (totalCost * 100);
// If we can pay for it then start reducing money
// We pay with larger bills first
// TENS, FIVES, ONES ... PENNIES in that order
int totalGivenInPennies = 0;
for (int i = 0; i < money.length && totalGivenInPennies < costInPennies; i++) {
while (money[i] > 0 && totalGivenInPennies < costInPennies) {
totalGivenInPennies += PENNIES_DENOMINATIONS[i];
money[i] -= PENNIES_DENOMINATIONS[i];
}
}
// Put the payment given back to decimal value
double totalGiven = totalGivenInPennies / 100;
double change = totalGiven - totalCost;
// Round the change to the nearest 10th
change = Double.parseDouble(String.format("%.2f", change));
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
// Pay for it, congratulate user
System.out.println();
System.out.println("Congratulations on your new Corgi!");
System.out.println();
System.out.println("Your new " + FURS[furOption] + " Corgi is so cute! "
+ "Your new friend will delight everyone in his or her " + OUTFITS[outfitOption] + " outfit! "
+ "Squeeze your puppy's ear to hear a " + SOUNDS[soundOption] + " just for you!");
System.out.println();
System.out.println("Corgi Birth Certificate");
System.out.println();
System.out.println("Your friend will need to bring back this certificate if he or she needs to be patched up!");
System.out.println("Take home day: " + dateFormat.format(new Date()));
System.out.println();
System.out.println("Your new friend includes:");
System.out.println();
System.out.printf("%-30s%5s\n", FURS[furOption] + ":", "$" + String.format("%.2f", FUR_PRICES[furOption]));
System.out.printf("%-30s%5s\n", OUTFITS[outfitOption] + " outfit:", "$" + String.format("%.2f", OUTFIT_PRICES[outfitOption]));
System.out.printf("%-30s%5s\n", SOUNDS[soundOption] + ":", "$" + String.format("%.2f", SOUND_PRICES[soundOption]));
for (int i = 0; i < chosenAccessories.length; i++) {
if (chosenAccessories[i]) {
System.out.printf("%-20s%5s\n", ACCESSORIES[i] + ":", "$" + String.format("%.2f", ACCESSORY_PRICES[i]));
}
}
System.out.println();
System.out.println("Total: $" + String.format("%.2f", totalCost));
System.out.println("Cash received: $" + String.format("%.2f", totalGiven));
System.out.println("Your change is: $" + String.format("%.2f", change));
if (change <= 0) {
return;
}
// Break the change into bills, for extr credit
int changeInPennies = (int) (change * 100);
String[] billNames = {"$10 bills", "$5 bills", "$1 bills", "quarters", "dimes", "nickels", "pennies"};
int[] quantities = {0, 0, 0, 0, 0, 0, 0};
for (int i = 0; i < PENNIES_DENOMINATIONS.length && changeInPennies > 0; i++) {
int quantity = 0;
while (changeInPennies > 0 && changeInPennies - PENNIES_DENOMINATIONS[i] >= 0) {
quantity++;
changeInPennies -= PENNIES_DENOMINATIONS[i];
}
quantities[i] = quantity;
}
// Now we print out the change in bills
String changeInBills = "";
for (int i = 0; i < quantities.length; i++) {
if (quantities[i] > 0) {
changeInBills += quantities[i] + " " + billNames[i] + ", ";
}
}
// Remove the extra ", " at the end
changeInBills = changeInBills.substring(0, changeInBills.length() - 2);
// Break the change in to tokens for proper sentence structuring
String[] tokens = changeInBills.split(", ");
if (tokens.length == 1) {
System.out.println(changeInBills);
return;
}
for (int i = 0; i < tokens.length; i++) {
System.out.print(tokens[i]);
if (i + 1 < tokens.length) {
System.out.print(", ");
} else {
System.out.print(" and ");
}
}
System.out.println();
}
// Entry point of the program
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.print("Hello next customer, what should I call you? ");
String customerName = Keyboard.readString();
processNextCustomer(customerName);
System.out.println();
}
}
}