Hallo Ich habe eine einfache Liste hier für Lebensmittel, die mit dem Hinzufügen von ihnen zu einer Gesamtkosten befassen. (Wie eine Registrierkasse) Ich habe Probleme, einen Löschknopf zu machen, um Gegenstände von der Liste zu löschen und von den Gesamtkosten zu subtrahieren. Bitte helfen Sie mir, ich weiß nicht, wo ich anfangen soll. Hier ist der Code.So löschen Sie ein Element aus Array-Liste Java-Eclipse
// 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);
}
}
Bitte reduzieren Sie die Menge an Code hier. Wenn Ihr Problem wirklich nur aus einer Liste gelöscht wird, sind 99% dieses Codes wahrscheinlich irrelevant. – Carcigenicate
Bitte lesen Sie http://StackOverflow.com/Help/How-to-ask, um eine Frage zu stellen. Es ist auch ziemlich klar, dass Sie noch nicht einmal versucht haben, nach der Antwort auf Ihre Frage zu suchen, also sollten Sie dort anfangen, anstatt StackOverflow mit etwas zu überschwemmen, das Sie selbst leicht beantworten könnten. – searchengine27
überprüfen Sie https://docs.oracle.com/javase/8/docs/api/java/util/List.html#remove-java.lang.Object- und https://docs.oracle.com/javase/8 /docs/api/java/util/List.html#remove-int- –