Create a new Java class called: PriceList Write a price-look up program for a fl
ID: 3712356 • Letter: C
Question
Create a new Java class called: PriceList
Write a price-look up program for a florist. The program should ask the user to enter the name of a flower. Next, the program should print to the screen the price of the flower. The price should be displayed to the screen with a dollar sign, and two decimal places. If the flower is not found in the price list, then the program should print the message "That flower was not found in the price list".
The flowers sold by this florist, along with their prices are as follows:
The program should store the names of the flowers in an array of Strings, which will be declared as a global constant named FLOWERS. Remember that you can create an array and initialize it with values all at the same time by using the notation:
String [] FLOWERS = {name1, name2, name3};
Make sure that you list the flowers in alphabetical order in the array in order to pass the automated tests.
The program should store the prices of the flowers in an array of doubles, which will be declared as a global constant named PRICES. Make sure that the price positions match up to the flower name positions.... in other words if the "aconitum" is stored in position 0 of the FLOWERS array, then it's price of $5.00 should be stored in the same position (pos 0) in the PRICES array.
The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's values are shown below in italics:
Enter the name of the flower:
Gerbera
The price is: $4.00
Here is another example run of the program:
Enter the name of the flower:
daffodil
That flower was not found in the price list
In order to pass the tests:
Your program must print the correct wording, with the correct spacing, and the correct output
The user should be able to type in any combination of upper and lowercase letters
Your program must have global constants named FLOWERS and PRICES
The flowers and prices arrays must contain the correct values in the correct order
NO BREAK STATEMENTS TO EXIT LOOPS SHOULD BE USED
MUST USE STARTING CODE:
import java.util.*;
public class PriceList {
public static Scanner kbd;
public static final String [] FLOWERS = {};
public static final double [] PRICES = {};
public static void main(String[] args) {
kbd = new Scanner(System.in);
kbd.close();
}
}
aconitum - $5.00 brassica - $5.00 eryngium - $10.00 iris - $2.00 agpanthus - $6.00 carthamus - $4.00 eustoma - $5.00 larkspur - $4.00 alstroemeria - $3.50 chrysanthemum - $5.00 freesia - $2.50 lilium - $5.00 antirrhinum - $3.50 cymbidium - $45.00 gerbera - $4.00 nerine - $3.00 aster - $4.00 delphinium - $8.00 gladiolus - $3.00 rose - $3.50 asterspray - $3.00 dendrobium - $4.00 helianthus - $3.00 tulip - $2.50 bird of paradise - $62.00 dianthus - $2.00 hydrangea - $8.00Explanation / Answer
import java.util.*;
// Class PriceList definition
public class PriceList
{
// Scanner class object declared
public static Scanner kbd;
// Final String array to store flowers name
public static final String [] FLOWERS = {"aconitum", "agpanthus", "alstroemeria", "antirrhinum", "aster", "asterspray",
"bird of paradise", "brassica", "carthamus", "chrysanthemum", "cymbidium",
"delphinium", "dendrobium", "dianthus", "eryngium", "eustoma", "freesia", "gerbera", "gladiolus",
"helianthus", "hydrangea", "iris", "larkspur", "lilium", "nerine", "rose", "tulip"};
// Final float array to store price for the above flower
public static final double [] PRICES = {5.00, 6.00, 3.50, 3.50, 4.00, 3.00, 62.00, 5.00, 4.00, 5.00, 45.00, 8.00,
4.00, 2.00, 10.00, 5.00, 2.50, 4.00, 3.00, 3.00, 8.00, 2.00, 4.00, 5.00, 3.00, 3.50, 2.50};
// main method definition
public static void main(String[] args)
{
// To store found status. Initializes to zero for not found.
int found = 0;
// To store flower name entered by the user
String flowerName;
// Scanner class object created
kbd = new Scanner(System.in);
// Accepts flower name from the user
System.out.print(" Enter the name of the flower: ");
flowerName = kbd.nextLine();
// Loops till number of flowers available
for(int x = 0; x < FLOWERS.length; x++)
{
// Checks if the current flower is equals to the flower name entered by the user
if(flowerName.equalsIgnoreCase(FLOWERS[x]))
{
// Displays the price stored at the found index position of the array PRICE
// Found index position is x
System.out.printf(" The price is: $%.2f", PRICES[x]);
// Sets the found status to 1
found = 1;
}// End of if condition
}// End of for loop
// Checks if the found status is zero then flower not found
if(found == 0)
System.out.print(" That flower " + flowerName + " was not found in the price list.");
// Closer the scanner
kbd.close();
}// End of main method
}// End of class
Sample Output 1:
Enter the name of the flower: antirrhinum
The price is: $3.50
Sample Output 2:
Enter the name of the flower: chrysanthemum
The price is: $5.00
Sample Output 3:
Enter the name of the flower: Genea
That flower Genea was not found in the price list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.