/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code6feb2018; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author jcmtiernan */ public class Code6Feb2018 { /** * @param args the command line arguments */ public static void main(String[] args) { // Calculating values of coinage final int nickel = 5; final int dime = 10; final int penny = 1; final int quarter = 25; final int halfDollar = 50; final int dollar = 100; double couchMoney = 0.0; Scanner input = new Scanner(System.in); Logger.getGlobal().setLevel(Level.OFF);// Use Level.OFF to turn off int pennies = 0; // Use Level.INFO to see output int nickels = 0; int dimes = 0; int quarters = 0; int halfDollars = 0; int dollars = 0; String moreCoins = ""; double lunchCost = 0.0; String runAgain = "N"; Logger.getGlobal().info("Example program with coins\n"); boolean moreRuns = true; while (moreRuns) { moreRuns = false; System.out.println("Let's find out how much money is inside the couch (or in your pocket, etc.)"); System.out.print("How many pennies do you have? "); pennies = input.nextInt(); Logger.getGlobal().info("The user entered "+pennies+" pennies.\n"); System.out.print("\nHow many nickels do you have? "); nickels = input.nextInt(); Logger.getGlobal().info("The user entered "+nickels+" nickels.\n"); System.out.print("\nHow many dimes do you have? "); dimes = input.nextInt(); Logger.getGlobal().info("The user entered "+dimes+" dimes.\n"); System.out.print("\nHow many quarters do you have? "); quarters = input.nextInt(); Logger.getGlobal().info("The user entered "+quarters+" quarters.\n"); System.out.print("\nDo you have any other coins? Enter Y or N "); moreCoins = input.next(); if (moreCoins.equalsIgnoreCase("Y") ) { // check on halfdollars and dollar coins System.out.print("\nHow many halfDollars do you have? "); halfDollars = input.nextInt(); System.out.print("\nHow many dollars do you have? "); dollars = input.nextInt(); } // How do we calculate the amount of money in dollars? couchMoney = (nickel * nickels + penny * pennies + dime * dimes + quarter * quarters + halfDollar * halfDollars + dollar * dollars) / 100.0 ; //couchMoney = couchMoney/100; System.out.printf("\nYour total couch money is $%.2f. TA DA!\n\n",couchMoney); //System.out.println("Your total couch money is "+couchMoney+". TA DA!"); System.out.println("Is this enough money to buy lunch? Y / N "); String lunchMoney = input.next(); // If user says yes, then print "Yay! Let's go eat!" if (lunchMoney.equalsIgnoreCase("Y")) { System.out.println("Yay! Let's go eat!"); } else // If the user says no, then ask the user if they have food at home for lunch. { System.out.println("Do you have food at home for lunch? Y / N "); String homeFood = input.next(); // If user says yes, then print "OK, let's make lunch!" if (homeFood.equalsIgnoreCase("Y") || homeFood.equalsIgnoreCase("Yes")) { System.out.println("OK, let's make lunch!"); // let the user choose from a variety of lunch choices // Version A: number the choices // Version B: name the choices // [This section will use a switch] System.out.println("Here are the choices for lunch: "+"\n1. Mashed potatoes" +"\n2. Leftovers"+"\n3. Chicken"+"\n4. Hot dog"+"\n5. Sandwich"+"\n6. Pasta" +"\n7. Rice and beans"+"\n8. Spinach salad"+"\nPlease enter the number of your choice: "); int choice = input.nextInt(); String yourLunch = "fasting"; switch (choice) { case 1: yourLunch = "Mashed potatoes"; break; case 2: yourLunch = "Leftovers"; break; case 3: yourLunch = "Chicken"; break; case 4: yourLunch = "Hot dog"; break; case 5: yourLunch = "Sandwich"; break; case 6: yourLunch = "Pasta"; break; case 7: yourLunch = "Rice and beans"; break; case 8: yourLunch = "Spinach salad"; break; default: yourLunch = "dust bunnies"; break; } System.out.println("Your lunch at home will be "+yourLunch); } else { // else, ask the user how much lunch will cost and [ask lunch cost] System.out.println("How much will lunch cost? "); lunchCost = input.nextDouble(); // then calculate how much more money they need to find in // the chair cushions lunchCost = lunchCost - couchMoney; System.out.println("You need an additional $"+lunchCost+" for lunch."); System.out.printf("Go find $%.2f in another piece of furniture\n",lunchCost); // [This section will show nested if-else examples] } } // Now we want to ask the user if they want run the whole program again // Set up control to allow program to run more than once. // [This will have us go back and put in a loop structure] System.out.println("\nWould you like to run the program again? Y / N"); runAgain = input.next(); if (runAgain.equalsIgnoreCase("Y") || runAgain.equalsIgnoreCase("Yes")) { // handle while loop control moreRuns = true; } // After this, check out Example1Feb18WhatCoins.java6 } System.out.println("Thank you for running the CouchMoney program"); } }