-7

Hello I have a simple list here for foods that deal with adding them to a total cost. (like a cash register) I am having trouble making a delete button to delete items from the list and subtracting from the total cost. Please help I don't know where to start with this one. Here is the code.

// Import Core Java packages
import java.awt.*;
import java.awt.event.*;

public class FastFood extends Frame {

/**
 * 
 */
private static final long serialVersionUID = 1L;
// Initial Frame size
static final int WIDTH = 400;                // frame width
static final int HEIGHT = 300;               // frame height

// Arrays of selection items
static final String[] FRUIT_FOODS = {"Pear", "Apple", "Peach", "Banana"};
static final int[] FRUIT_FOODS_PRICE = {79, 69, 89, 59};
static final String[] BEVERAGE_DRINKS = {"Soda", "Coffee", "Milk", "Water"};
static final int[] BEVERAGE_DRINKS_PRICE = {79, 95, 85, 65};
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};

// List and Button control
List fruitFoodList;
List beverageList;
List junkFoodList;
List dessertList;
List itemsOrderedList;
Button addFruitFood;
Button addBeverage;
Button addJunkFood;
Button addDessert;
Button deleteItem;

// 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<JUNK_FOODS.length; i++) {
        junkFoodList.add(JUNK_FOODS[i]+ "   $" + (float)JUNK_FOODS_PRICE[i]/100);
    }
    junkFoodPanel.add(junkFoodList, BorderLayout.CENTER);
    Panel buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
    junkFoodPanel.add(buttonPanel, BorderLayout.SOUTH);
    addJunkFood = new Button("Get Food Item");
    buttonPanel.add(addJunkFood);

  //create a display for fruit items
    Panel fruitFoodPanel = new Panel(new BorderLayout());
    itemPanel.add(fruitFoodPanel);
    Label fruitFood = new Label("Fruit Items", Label.CENTER);
    fruitFood.setForeground(Color.red);
    fruitFoodPanel.add(fruitFood, BorderLayout.NORTH);
    fruitFoodList = new List();
    for(int i=0; i<FRUIT_FOODS.length; i++) {
        fruitFoodList.add(FRUIT_FOODS[i]+ "   $" + (float)FRUIT_FOODS_PRICE[i]/100);
    }
    fruitFoodPanel.add(fruitFoodList, BorderLayout.CENTER);
    buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
    fruitFoodPanel.add(buttonPanel, BorderLayout.SOUTH);
    addFruitFood = new Button("Get Fruit");
    buttonPanel.add(addFruitFood);

    // 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<DESSERTS.length; i++) {
        dessertList.add(DESSERTS[i]+ "   $" + (float)DESSERTS_PRICE[i]/100);
    }
    dessertPanel.add(dessertList, BorderLayout.CENTER);
    buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
    dessertPanel.add(buttonPanel, BorderLayout.SOUTH);
    addDessert = new Button("Get Dessert Item");
    buttonPanel.add(addDessert);

  //create a display for Beverages
    Panel beveragePanel = new Panel(new BorderLayout());
    itemPanel.add(beveragePanel);
    Label beverage = new Label("Beverages", Label.CENTER);
    beverage.setForeground(Color.red);
    beveragePanel.add(beverage, BorderLayout.NORTH);
    beverageList = new List();
    for(int i=0; i<BEVERAGE_DRINKS.length; i++) {
        beverageList.add(BEVERAGE_DRINKS[i]+ "   $" + (float)BEVERAGE_DRINKS_PRICE[i]/100);
    }
    beveragePanel.add(beverageList, BorderLayout.CENTER);
    buttonPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
    beveragePanel.add(buttonPanel, BorderLayout.SOUTH);
    addBeverage = new Button("Get Beverage");
    buttonPanel.add(addBeverage);


    // create display and control for items ordered and amount
    Panel orderedPanel = new Panel(new BorderLayout());
    add(orderedPanel, BorderLayout.EAST);

    // 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);
    fruitFoodList.addActionListener(selListener);
    beverageList.addActionListener(selListener);

    // create and add button listener
    ButtonListener buttonListener = new ButtonListener();
    addJunkFood.addActionListener(buttonListener);
    addDessert.addActionListener(buttonListener);
    addFruitFood.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() == fruitFoodList) {  // select item from dessert list
            selectedIndex = fruitFoodList.getSelectedIndex();
            selectedItem = fruitFoodList.getSelectedItem();
            itemPrice = FRUIT_FOODS_PRICE[selectedIndex];
            addOrderedItem(selectedItem, itemPrice);
        }
        else if(event.getSource() == beverageList) {  // select item from dessert list
            selectedIndex = beverageList.getSelectedIndex();
            selectedItem = beverageList.getSelectedItem();
            itemPrice = BEVERAGE_DRINKS_PRICE[selectedIndex];
            addOrderedItem(selectedItem, itemPrice);
        }
    }
}

/**
 *  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);
            }
        }
        else if(event.getSource() == addFruitFood) {  // get item from fruit food list
            selectedIndex = fruitFoodList.getSelectedIndex();
            if(selectedIndex >= 0) {
                selectedItem = fruitFoodList.getSelectedItem();
                itemPrice = FRUIT_FOODS_PRICE[selectedIndex];
                addOrderedItem(selectedItem, itemPrice);
            }
        }
        else if(event.getSource() == addBeverage) {  // get item from fruit food list
            selectedIndex = beverageList.getSelectedIndex();
            if(selectedIndex >= 0) {
                selectedItem = beverageList.getSelectedItem();
                itemPrice = BEVERAGE_DRINKS_PRICE[selectedIndex];
                addOrderedItem(selectedItem, itemPrice);
            }
        }
    }
}

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


/**
 * 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);
}

}

Kakkarot
  • 11
  • 6
  • 2
    Please reduce the amount of code here. If your issue really is just deleting from a list, 99% of this code is likely irrelevant. – Carcigenicate Oct 03 '16 at 19:10
  • 1
    please see http://stackoverflow.com/help/how-to-ask on how to ask a question. Also, it's pretty clear you haven't even tried googling for the answer to your question, so you might want to start there instead of flooding StackOverflow with something you could easily answer yourself. – searchengine27 Oct 03 '16 at 19:13
  • check https://docs.oracle.com/javase/8/docs/api/java/util/List.html#remove-java.lang.Object- and https://docs.oracle.com/javase/8/docs/api/java/util/List.html#remove-int- – Nicolas Filotto Oct 03 '16 at 19:25
  • Possible duplicate of [Remove Item from ArrayList](http://stackoverflow.com/questions/10714233/remove-item-from-arraylist). – Ole V.V. Oct 03 '16 at 19:59

1 Answers1

0

I advise you re write your code. One mistake to note, you are attempting to create objects of type List. List is not a class and cannot be instantiated, it is an interface that contains methods to be implemented but these methods contain no bodies. To use java.util.List, you must implement it and then implement the methods in the interface providing the bodies for them. If you need a list of some sort, you need to use an java.util.ArrayList or java.util.LinkedList depending upon your needs.

Other than that, the code you have posted on here is too long, youre best to find which block of code the error is in, and tell us what exceptions or error messages you get.

Daniel Baron
  • 34
  • 1
  • 5