Question

In: Computer Science

/* What's wrong with this code? (nothing actually - but let's fix it to use a...

/* What's wrong with this code?
(nothing actually - but let's fix it to use a repetition structure)
Copy and paste this .txt file into jGrasp, 
then save it, compile it, run it.

Modify it!

Bring your code to class at the start of Week 8, with at least 2 output runs with different data.

What the program should do is take orders(input) for three (3) octoberfest guests, using a "for" loop. (+1 pt.)
Each guest will choose 1 or 2 items (take in as strings) from the menu.
Print (output) an itemized receipt as items are ordered, then a total bill and a "split the bill 3 ways" total.

Bonus(+1 pt.) Modify the "take order part" - so a guest can order as many items as they choose, using a "while" loop.

Bonus(+1 pt.) Use decimal formatter object to display output correctly as dollars and cents, i.e. $3.10 not $3.1 )
*/

//import API's needed here:
import java.util.Scanner;
public class COdetoberFest01 
{  

        public static void main(String args[ ]) 
        {
           Scanner kbd = new Scanner(System.in);
           float total =0; //total bill
           float splitTotal; //for 3 way split of bill
           float cost; //item cost
           int guest =0; //count off guests 
           String item;
           int qty; //number of items a guest will order
           
           System.out.println("Will you be ordering 1 or 2 items?" );
           qty =kbd.nextInt();
           kbd.nextLine();
           guest++;
           if(qty ==2)
           {
           System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel ");
           System.out.print("Place your order: ");
           item=kbd.nextLine();
           if(item.charAt(0) == 'j')//jGrasp Pretzel
            cost = 1.50f;
           else if(item.charAt(0) == 's')//sub-routine Saurkraut
            cost = 2.75f;
           else if(item.charAt(0) == 'b')//boolean Bier
            cost = 3.90f;
           else 
            cost =5.35f; // must be double schnitzel
            System.out.println(cost);
            total = total +cost;
        
           }
           System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel ");
           System.out.print("Place your order: ");
           item=kbd.nextLine();
           if(item.charAt(0) == 'j')//jGrasp Pretzel
            cost = 1.50f;
           else if(item.charAt(0) == 's')//sub-routine Saurkraut
            cost = 2.75f;
           else if(item.charAt(0) == 'b')//boolean Bier
            cost = 3.90f;
           else 
            cost =5.35f; // must be double schnitzel
            System.out.println(cost);
            total = total + cost;

           System.out.println("Will you be ordering 1 or 2 items?" );
           qty =kbd.nextInt();
           kbd.nextLine();
           guest++;
           if(qty ==2)
           {
           System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel ");
           System.out.print("Place your order: ");
           item=kbd.nextLine();
           if(item.charAt(0) == 'j')//jGrasp Pretzel
            cost = 1.50f;
           else if(item.charAt(0) == 's')//sub-routine Saurkraut
            cost = 2.75f;
           else if(item.charAt(0) == 'b')//boolean Bier
            cost = 3.90f;
           else 
            cost =5.35f; // must be double schnitzel  
            System.out.println(cost);
            total = total +cost;
        
           }
           System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel ");
           System.out.print("Place your order: ");
           item=kbd.nextLine();
           if(item.charAt(0) == 'j')//jGrasp Pretzel
            cost = 1.50f;
           else if(item.charAt(0) == 's')//sub-routine Saurkraut
            cost = 2.75f;
           else if(item.charAt(0) == 'b')//boolean Bier
            cost = 3.90f;
           else 
            cost =5.35f; // must be double schnitzel
            System.out.println(cost);
            total = total +cost;

           System.out.println("Will you be ordering 1 or 2 items?" );
           qty =kbd.nextInt();
           kbd.nextLine();
           guest++;
           if(qty ==2)
           {
           System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel ");
           System.out.print("Place your order: ");
           item=kbd.nextLine();
           if(item.charAt(0) == 'j')//jGrasp Pretzel
            cost = 1.50f;
           else if(item.charAt(0) == 's')//sub-routine Saurkraut
            cost = 2.75f;
           else if(item.charAt(0) == 'b')//boolean Bier
            cost = 3.90f;
           else 
            cost =5.35f; // must be double schnitzel
             System.out.println(cost);
             total = total +cost;
        
           }
           System.out.println("CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel ");
           System.out.print("Place your order: ");
           item=kbd.nextLine();
           if(item.charAt(0) == 'j')//jGrasp Pretzel
            cost = 1.50f;
           else if(item.charAt(0) == 's')//sub-routine Saurkraut
            cost = 2.75f;
           else if(item.charAt(0) == 'b')//boolean Bier
            cost = 3.90f;
           else 
            cost =5.35f; // must be double schnitzel
            System.out.println(cost);
            total = total +cost;

           System.out.println("For " + guest + ", The total is: $" + total);
           System.out.println("If you split the bill " + guest + " ways, each guest must pay: $" + (total/guest));
                
                                  
                                  
         } //closing main method
} //closing class header

Solutions

Expert Solution

Below is the modified code : I have used DecimalFormat class to format the decimals. Also StringBuffer class to display the itemized bill. Also modified code to take as many items as customer want instead of 1 or 2 items.

Added for loop to iterate over the three customers.

Added while loop to iterate over the number of orders.

----------------------------------------------------

/*
*Program to take the order and provide the bill to customer.
*/
//import API's needed here:
import java.text.DecimalFormat;
import java.util.Scanner;

public class COdetoberFest01 {

   public static void main(String args[]) {
       Scanner kbd = new Scanner(System.in);
       float total = 0; // total bill
       float splitTotal; // for 3 way split of bill
       float cost; // item cost
       int guest = 0; // count off guests
       String item;
       int qty; // number of items a guest will order
      
       //decimal format object
       DecimalFormat df = new DecimalFormat("#.00");
       //StrigBuffer to hold the items to display in itemized bill
        StringBuffer itemized_bill = new StringBuffer();
      
        for(int i =1; i<=3; i++)
        {
            System.out.println("please enter the number of items you want to order");
              qty = kbd.nextInt();
              kbd.nextLine();
              guest++;
              while (qty > 0)
              {
                  System.out.println(
                          "CODETOBER FEST MENU:\n $1.50: jGrasp Pretzel,\n $2.75 sub-routine Saurkraut,\n $3.90 boolean Bier,\n $5.35 double schnitzel ");
                  System.out.print("Place your order: ");
                  item = kbd.nextLine();
                  if (item.charAt(0) == 'j')// jGrasp Pretzel
                  {
                      cost = 1.50f;
                      itemized_bill.append("jGrasp Pretzel \n");
                  }
                  else if (item.charAt(0) == 's')// sub-routine Saurkraut
                  {
                      cost = 2.75f;
                      itemized_bill.append("sub-routine Saurkraut \n");
                  }
                  else if (item.charAt(0) == 'b')// boolean Bier
                  {
                      cost = 3.90f;
                      itemized_bill.append("boolean Bier \n");
                  }
                  else
                  {
                      cost = 5.35f; // must be double schnitzel
                      itemized_bill.append("double schnitzel \n");
                  }
                  System.out.println(df.format(cost));
                  total = total + cost;
                  qty--;
              }
        }
       System.out.println("For " + guest + ", The total is: $" + df.format(total));
       System.out.println("Itemized bill : \n" + itemized_bill);
       System.out.println("If you split the bill " + guest + " ways, each guest must pay: $" + df.format(total / guest));

   } // closing main method
} // closing class header

------------------------------------------------------

Below is the output:

Ran the code from eclipse. Right clickon the program > Run As > Java Application:

Output of First run:

please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: jGrasp Pretzel
1.50
please enter the number of items you want to order
2
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: sub-routine Saurkraut
2.75
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: double schnitzel
5.35
please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: boolean Bier
3.90
For 3, The total is: $13.50
Itemized bill :
jGrasp Pretzel
sub-routine Saurkraut
double schnitzel
boolean Bier

If you split the bill 3 ways, each guest must pay: $4.50

-----------------------------------------------------------------------------------

2nd Run output:

please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: j
1.50
please enter the number of items you want to order
2
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: b
3.90
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: d
5.35
please enter the number of items you want to order
1
CODETOBER FEST MENU:
$1.50: jGrasp Pretzel,
$2.75 sub-routine Saurkraut,
$3.90 boolean Bier,
$5.35 double schnitzel
Place your order: b
3.90
For 3, The total is: $14.65
Itemized bill :
jGrasp Pretzel
boolean Bier
double schnitzel
boolean Bier

If you split the bill 3 ways, each guest must pay: $4.88

-----------------------------------------------------------------------------------


Related Solutions

What is wrong with the following code? Give two effective ways to fix it. 1 import...
What is wrong with the following code? Give two effective ways to fix it. 1 import javax.swing.*; 2 import java.awt.event.*; 3 4 public class H2ClassJ extends JFrame { 5    public static final long serialVersionUID = 22; 6 7 public H2ClassJ () { 8      addMouseListener (new MouseListener () { 9    public void mouseClicked (MouseEvent e) {} 10     }); 11   } // end constructor 12 13 } // end class H2ClassJ
My Javascript code isn't working (when i press calculate button) - what's wrong with it ?...
My Javascript code isn't working (when i press calculate button) - what's wrong with it ? Car Rental Enter Number of Rental Days: Select Car Type: onclick= "priceofcar = 50;"/> Compact onclick= "priceofcar = 60;"/> Economy onclick= "priceofcar = 70;"/> Intermediate Select Loss Damage Waiver onclick= "damagewaiver='yes'"/> Yes onclick= "damagewaiver='no'"/> No damagewaiver = boxDays.value * 25;} else if (damagewaiver == No) { damagewaiver = 0; }/> Select Roadside Issues Coverage: onclick= "issuescoverage='yes'"/> Yes onclick= "issuescoverage='no'"/> No issuescoverage = boxDays.value *...
What's wrong with my Python code. We have to find the regularexpression in Python My output...
What's wrong with my Python code. We have to find the regularexpression in Python My output should look like this My IP address 128. 0. 0. 1 My IP address 53. 20. 62. 201 My code ipAddresses = [] ipRegEx = re.compile(r"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")    result = myRegEx.find all(Search Text)    def Regular_expression(ipAddresses)       for i in ipAddresses:          print(i) ipSearchResult = ipRegEx.search(line)    if ipSearchResult != None and ipSearchResult.group() not in ipAddresses:       ipAddresses.append(ipSearchResult.group()) we have to use loop too.
Need to fix this code for tc -tac-toe game .. see the code below and fix...
Need to fix this code for tc -tac-toe game .. see the code below and fix it #include <iostream> using namespace std; void display_board(); void player_turn(); bool gameover (); char turn ; bool draw = false; char board [3][3] = { {'1', '2', '3'}, { '4', '5', '6'}, { '7', '8', '9'}}; int main() { cout << " Lets play Tc- Tac- toe game " <<endl ; cout << " Player 1 [X] ----- player 2 [0] " <<endl <<endl;...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should...
use the python language and fix the error code #Write a function called rabbit_hole. rabbit_hole should have #two parameters: a dictionary and a string. The string may be #a key to the dictionary. The value associated with that key, #in turn, may be another key to the dictionary. # #Keep looking up the keys until you reach a key that has no #associated value. Then, return that key. # #For example, imagine if you had the following dictionary. #This one...
fix this code in python and show me the output. do not change the code import...
fix this code in python and show me the output. do not change the code import random #variables and constants MAX_ROLLS = 5 MAX_DICE_VAL = 6 #declare a list of roll types ROLLS_TYPES = [ "Junk" , "Pair" , "3 of a kind" , "5 of a kind" ] #set this to the value MAX_ROLLS pdice = [0,0,0,0,0] cdice = [0,0,0,0,0] #set this to the value MAX_DICE_VAL pdice = [0,0,0,0,0,0] cdice = [0,0,0,0,0,0] #INPUT - get the dice rolls i...
What's wrong with this way of thinking? "Economists argue that lower prices will result in fewer...
What's wrong with this way of thinking? "Economists argue that lower prices will result in fewer units being supplied. However, there are exceptions to this rule. For example, in 1972 a very simple ten-digit electronic calculator sold for $120.00. By 2000, the price of the same type of calculators had declined to less than $5.00. Yet business firms produced and sold many more calculators in 2000 than they did in 1972. Lower prices did not result in less production or...
Nutrition Question- - .what's wrong with the following hypothesis. How can it be improved?    Multivitamins...
Nutrition Question- - .what's wrong with the following hypothesis. How can it be improved?    Multivitamins mineral supplements improve health
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT