Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extend and complete program FastFood .java used in the class as follows: Add two more item lists to the right of the two existing item

Extend and complete program FastFood.java used in the class as follows:

Add two more item lists to the right of the two existing item lists. The first contains a list of fruits with these items and their prices, Apple (89 cents), Orange (79 cents), Peach (99 cents), and Banana (59 cents). The second is a list of beverages with these items and their prices, Soda (85 cents), Coffee (95 cents), Juice (75 cents), and Water (55 cents). Display the first list with a label Fruit Items above it and a button Get Fruit Item below it. Display the second list with a label Beverage Items above it and a button Get Beverage Item below it. The new lists and buttons should appear and work in the same way as the ones currently in the program.

My Code is Not Coming Out Correctly

import java.awt.*; import java.awt.event.*;

public class FastFood extends Frame {

// Initial Frame size static final int WIDTH = 900; // frame width static final int HEIGHT = 400; // frame height

// Arrays of selection items static final String[] JUNK_FOODS = {"Burger", "Hotdog", "Fries", "Pizza"}; static final int[] JUNK_FOODS_PRICE = {199, 150, 99, 299}; static final String[] DESSERTS = {"Ice cream", "Pie", "Cake", "Donut"}; static final int[] DESSERTS_PRICE = {149, 249, 299, 99}; static final String[] FRUIT_ITEMS = {"Apple", "Orange", "Peach", "Banana"}; static final int[] FRUIT_PRICE = {89, 79, 99, 59}; static final String[] BEVERAGE = {"Soda", "Coffee", "Juice", "Water"}; static final int[] BEVERAGE_PRICE = {85, 95, 75, 55};

// List and Button control List junkFoodList; List dessertList; List fruitList; List beverageList; List itemsOrderedList; Button addJunkFood; Button addDessert; Button addFruit; Button addBeverage; Button deleteItem; Button clearAll;

// Total amount and display Label amountLabel; int amount = 0;

/** * Constructor */ public FastFood() { setTitle("Fast Food To Go"); setLayout(new BorderLayout());

// create display for item selection Panel itemPanel = new Panel(new GridLayout(2, 1)); add(itemPanel, BorderLayout.CENTER);

// create display and control for junk food selection items Panel junkFoodPanel = new Panel(new BorderLayout()); itemPanel.add(junkFoodPanel); Label junkFood = new Label("Fast Food Items", Label.CENTER); junkFood.setForeground(Color.red); junkFoodPanel.add(junkFood, BorderLayout.NORTH); junkFoodList = new List(); for(int i=0; i

// create display and control for dessert selection items Panel dessertPanel = new Panel(new BorderLayout()); itemPanel.add(dessertPanel); Label dessertLabel = new Label("Dessert Items", Label.CENTER); dessertLabel.setForeground(Color.red); dessertPanel.add(dessertLabel, BorderLayout.NORTH); dessertList = new List(); for(int i=0; i

// create display and control for beverage selection items Panel beveragePanel = new Panel(new BorderLayout()); itemPanel.add(beveragePanel); Label beverageLabel = new Label("Beverage Items", Label.CENTER); beverageLabel.setForeground(Color.red); beveragePanel.add(buttonPanel, BorderLayout.NORTH); beverageList = new List(); for(int i=0; i

// create display and control for items ordered Panel itemsOrderedPanel = new Panel(new BorderLayout()); orderedPanel.add(itemsOrderedPanel, BorderLayout.CENTER); Label itemsOrdered = new Label("Items Ordered", Label.CENTER); itemsOrdered.setForeground(Color.red); itemsOrderedPanel.add(itemsOrdered, BorderLayout.NORTH); itemsOrderedList = new List(); itemsOrderedPanel.add(itemsOrderedList, BorderLayout.CENTER); buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER)); itemsOrderedPanel.add(buttonPanel, BorderLayout.SOUTH); deleteItem = new Button("Delete Ordered Item"); buttonPanel.add(deleteItem);

// create display for amount Panel amountPanel = new Panel(new BorderLayout()); orderedPanel.add(amountPanel, BorderLayout.SOUTH); Label totalLabel = new Label("Total Amount: "); amountPanel.add(totalLabel, BorderLayout.WEST); amountLabel = new Label("$0.00"); amountPanel.add(amountLabel, BorderLayout.CENTER);

// create and add list selection listener SelectionListener selListener = new SelectionListener(); junkFoodList.addActionListener(selListener); dessertList.addActionListener(selListener);

// create and add button listener ButtonListener buttonListener = new ButtonListener(); addJunkFood.addActionListener(buttonListener); addDessert.addActionListener(buttonListener); addFruit.addActionListener(buttonListener); addBeverage.addActionListener(buttonListener); } // end of constructor

/** * Listener class for list selection */ class SelectionListener implements ActionListener { public void actionPerformed(ActionEvent event) { String selectedItem; int selectedIndex; int itemPrice;

if(event.getSource() == junkFoodList) { // select item from junk food list selectedIndex = junkFoodList.getSelectedIndex(); selectedItem = junkFoodList.getSelectedItem(); itemPrice = JUNK_FOODS_PRICE[selectedIndex]; addOrderedItem(selectedItem, itemPrice); } else if(event.getSource() == dessertList) { // select item from dessert list selectedIndex = dessertList.getSelectedIndex(); selectedItem = dessertList.getSelectedItem(); itemPrice = DESSERTS_PRICE[selectedIndex]; addOrderedItem(selectedItem, itemPrice); } else if(event.getSource() == fruitList) { // select item from fruit list selectedIndex = fruitList.getSelectedIndex(); selectedItem = fruitList.getSelectedItem(); itemPrice = FRUIT_PRICE[selectedIndex]; addOrderedItem(selectedItem, itemPrice); } else if(event.getSource() == beverageList) { //beverage list select selectedIndex = beverageList.getSelectedIndex(); selectedItem = beverageList.getSelectedItem(); itemPrice = BEVERAGE_PRICE[selectedIndex]; addOrderedItem(selectedItem, itemPrice); } else if(event.getSource() == itemsOrderedList) {//remove items selectedIndex = itemsOrderedList.getSelectedIndex(); selectedItem = itemsOrderedList.getSelectedItem(); Double price = new Double(selectedItem.substring(selectedItem.indexOf('$') + 1)); removeOrderedItem(selectedItem,(int) (price * 100)); } } }

/** * Listener class for button selection */ class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { String selectedItem; int selectedIndex; int itemPrice;

if(event.getSource() == addJunkFood) { // get item from junk food list selectedIndex = junkFoodList.getSelectedIndex(); if(selectedIndex >= 0) { selectedItem = junkFoodList.getSelectedItem(); itemPrice = JUNK_FOODS_PRICE[selectedIndex]; addOrderedItem(selectedItem, itemPrice); } } else if(event.getSource() == addDessert) { // get item from dessert list selectedIndex = dessertList.getSelectedIndex(); if(selectedIndex >= 0) { selectedItem = dessertList.getSelectedItem(); itemPrice = DESSERTS_PRICE[selectedIndex]; addOrderedItem(selectedItem, itemPrice); } } } }

/** * method to add an ordered item */ void addOrderedItem(String item, int price) { itemsOrderedList.add(item); amount += price; amountLabel.setText("$"+(float)amount/100); }

//remove an item

void removeOrderedItem(String item, int price) {

itemsOrderedList.remove(item);

amount -= price;

amountLabel.setText("$"+(float)amount/100);

}

void removeAllOrderedItems(){

//Clear the whole itemOrderList

itemsOrderedList.removeAll(); amountLabel.setText("$"+0);

} /** * the main method */ public static void main(String[] argv) { // Create a frame FastFood frame = new FastFood(); frame.setSize(WIDTH, HEIGHT); frame.setLocation(150, 100);

// add window closing listener frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { System.exit(0); } });

// Show the frame frame.setVisible(true); } }

The button to delete an ordered item does not work currently. Extend the program so that an ordered item can be removed either by double-clicking the item or by first selecting the item with a single mouse click and then pressing the delete button. In either case, the price of the item removed should be deducted from the running total. If no selection is made first, the delete button should do nothing.

Add a Clear All button next and to the right of the item delete button and program the button to remove all the items in the ordered list and reset the running total to zero when the button is clicked.

Clear All

button next and to the right of the item delete button and program the

button to remove all the items in the ordered list and reset the running total to zero when the

button is clicked.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions

Question

List three differences between data security and data integrity.

Answered: 1 week ago