Jasvant Singh Dosanjh Jasvant Singh Dosanjh - 2 months ago 18
Java Question

Total Cost in JOptionPane not showing up

How do I get the total cost to show up as a joptionpane, and the beverages in it as well. I was asked to simulate a Breakfast ordering system for BeBe’s Best Breakfast. I am to display a JOptionPane to prompt the user for choices of breakfast and a beverage to order. I am then to provide a prompt for the number of these breakfasts to be ordered by the office (from 1 - 5) and use a Scanner object to read in his input. And then output the user’s Bill in a JOptionPane.

import javax.swing.*;
import java.util.Scanner;
import java.text.*;
import java.util.Locale;
public class ProjectFourA
{
private static final float bagel = 2.00f, donut = 1.50f, croissant = 3.00f;
private static final float latte = 1.50f, coffee = 1.25f, milk = 1.00f, tea = 0.50f;
private float cost,extracost;
public static void main(String[]args)
{
Scanner breakfast = new Scanner(System.in);
NumberFormat mfmt = NumberFormat.getCurrencyInstance(Locale.US);
int size, choice, num;
String ch = JOptionPane.showInputDialog("Welcome to BeBe's Best Breakfast choose a breakfast item." + "\n1 to order Bagel"+"\n2 to order Donut"+"\n3 to order Croissant");
choice = Integer.parseInt(ch);

String nm = JOptionPane.showInputDialog("Choose one of the following beverages:" + "\n1 for Latte"+"\n2 for Coffee"+"\n3 for Milk"+"\n4 for Tea");
num = Integer.parseInt(nm);
float cost=0.0f, extracost=0.0f;
float price;
String str="", topstr="";
if (choice == 1)
{
cost = bagel;
str = "Bagel";
String inputStr = JOptionPane.showInputDialog(null, "Enter quantity: 1-5");
inputStr = breakfast.nextLine();
}
else if (choice == 2)
{
cost = donut;
str = "Donut";
String inputStr = JOptionPane.showInputDialog(null, "Enter quantity: 1-5");
inputStr = breakfast.nextLine();
}
else if (choice == 3)
{
cost = croissant;
str = "Croissant";
String inputStr = JOptionPane.showInputDialog(null, "Enter quantity: 1-5");
inputStr = breakfast.nextLine();
}
float totCost = extracost + cost;
JOptionPane.showMessageDialog(null,( "Breakfast ordered:" +str + "\nBeverage ordered: "+topstr + "\nTotal cost: $"+totCost));
}
}

Answer

Your problem looks to be the line inputStr = breakfast.nextLine(); in each if block. You are not entering any input for this. So it does not execute your showMessageDialog.

Remove inputStr = breakfast.nextLine();

if (choice == 1) {
            cost = bagel;
            str = "Bagel";
            String inputStr = JOptionPane.showInputDialog(null,
                    "Enter quantity: 1-5");

        } else if (choice == 2) {
            cost = donut;
            str = "Donut";
            String inputStr = JOptionPane.showInputDialog(null,
                    "Enter quantity: 1-5");

        } else if (choice == 3) {
            cost = croissant;
            str = "Croissant";
            String inputStr = JOptionPane.showInputDialog(null,
                    "Enter quantity: 1-5");

        }
        float totCost = extracost + cost;
        JOptionPane.showMessageDialog(null,
                ("Breakfast ordered:" + str + "\nBeverage ordered: " + topstr
                        + "\nTotal cost: $" + totCost));
Comments