Parallel Arrays The program should either print the name and price for a coffee
ID: 3538516 • Letter: P
Question
Parallel Arrays
The program should either print the name and price for a coffee add-in from the Jumpin%u2019 Jive coffee shop or it should print the message: %u201CSorry, we do not carry that.%u201D
The data file provided for this lab includes the necessary variable declarations and input statements. You need to write the part of the program that searches for the names of the coffee add-in(s) and either prints the name and price of the add-in or prints the error message if the add-in is not found.
%u2022 Write the code that searches the array for the name of the add-in ordered by the customer.
%u2022 Write the code that prints the name and price of the add-in or the error message and also write the code that prints the total order cost.
%u2022 Compile and execute the program using the following data, and record the output:
o Cream
o Caramel
o Whiskey
o chocolate
o Chocolate
o Cinnamon
o Vanilla
// JumpinJive.java - This program looks up and prints the names and prices of coffee orders.
// Input: Interactive.
// Output: Name and price of coffee orders or error message if add-in is not found.
import javax.swing.*;
public class JumpinJive
{
public static void main(String args[]) throws Exception
{
// Declare variables.
String addIn; // Add-in ordered by customer.
final int NUM_ITEMS = 5; // Named constant
// Initialized array of add-ins.
String addIns[] = {"Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"};
// Initialized array of add-in prices.
double addInPrices[] = {.89, .25, .59, 1.50, 1.75};
boolean foundIt;
int x; // Loop control variable.
double orderTotal = 2.00; // All orders start with a 2.00 charge
// Get user input.
addIn = JOptionPane.showInputDialog("Enter coffee add-in or XXX to quit: ");
// Write the rest of the program here.
Explanation / Answer
// JumpinJive.java - This program looks up and prints the names and prices of coffee orders.
// Input: Interactive.
// Output: Name and price of coffee orders or error message if add-in is not found.
import javax.swing.*;
public class JumpinJive
{
public static void main(String args[]) throws Exception
{
// Declare variables.
String addIn; // Add-in ordered by customer.
final int NUM_ITEMS = 5; // Named constant
// Initialized array of add-ins.
String addIns[] = {"Cream", "Cinnamon", "Chocolate", "Amaretto", "Whiskey"};
// Initialized array of add-in prices.
double addInPrices[] = {.89, .25, .59, 1.50, 1.75};
boolean foundIt;
int x; // Loop control variable.
double orderTotal = 2.00; // All orders start with a 2.00 charge
// Get user input.
addIn = JOptionPane.showInputDialog("Enter coffee add-in or XXX to quit: ");
// Write the rest of the program here.
if( addIn.equals("XXX"))
{
System.out.println("Exiting!");
}
else
{
for(x=0;x<NUM_ITEMS;x++)
{
if(addIn.equals(addIns[x]))
{
orderTotal = orderTotal+ addInPrices[x];
System.out.println("You ordered "+addIns[x]+" Coffee; Price: $"+orderTotal);
foundIt=true;
}
}
if(!foundIt)
{
System.out.println("Sorry, we do not carry that.");
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.