如何在不同方法中编辑arraylist中的修改对象

时间:2018-03-08 03:55:00

标签: java arrays arraylist methods

我希望能够修改存储在名为list的数组列表中的元素的不同属性。但是,我需要能够在我的销售命令中执行此操作。例如,我想在sell方法中接受项目的名称和用户想要销售的金额。那么我需要在sell命令中设置我相信的新数量。我将简要介绍如何在新方法中编辑阵列列表中的任何内容。任何帮助都会很棒。

package control;

import java.util.ArrayList;    
import javax.swing.JOptionPane;    
import inventory.Item;
import inventory.itemType;
/*** @author valam      */
public class Driver {
    double myBalance;

    public static void main(String[] args) {
        ArrayList<Item> list = new ArrayList<Item>();

        list.add(new Item("Ketchup", 1.00, 10, 2.00, itemType.FOOD));
        list.add(new Item("Mayo", 2.00, 20, 3.0, itemType.FOOD));
        list.add(new Item("Bleach", 3.00, 30, 4.00, itemType.CLEANING));
        list.add(new Item("Lysol", 4.00, 40, 5.00, itemType.CLEANING));

        for(Item i : list) {
            System.out.println(i.getName());
            System.out.println(i.getQty());
        }
    }

    public static void sell(String itemName, int amount) {      
    /*String Answer1 = 
  JOptionPane.showInputDialog ("What would you like to sell (options: Ketchup, Mayo, Bleach, or Lysol").toLowerCase();
    String Answer2 =
  JOptionPane.showInputDialog("How much of said item would you like to sell").toLowerCase();
    */      
    }
}

1 个答案:

答案 0 :(得分:0)

使用Map<String, Item>保存商品数据

public class Driver {
    private static Map<String, Item> map = new HashMap<String, Item>();

    public static void main(String[] args) {
        map.put("Ketchup", new Item("Ketchup", 1.00, 10, 2.00, itemType.FOOD));
        map.put("Mayo", new Item("Mayo", 2.00, 20, 3.0, itemType.FOOD));
        map.put("Bleach", new Item("Bleach", 3.00, 30, 4.00, itemType.CLEANING));
        map.put("Lysol", new Item("Lysol", 4.00, 40, 5.00, itemType.CLEANING));
    }

    public static void sell(String itemName, int amount){
        //if itemName is 'Ketchup', you can get Item("Ketchup", 1.00, 10, 2.00, itemType.FOOD)
        Item item = map.get(itemName);
        item.setAmount(amount);
    }
}

如果不使用static,则使用对象会更好 Map<String, Integer>记录名称和数组索引

public class Driver {
    private Map<String, Integer> map = new HashMap<String, Integer>();
    private List<Item> items = new ArrayList<Item>();

    public Driver(List<Item> items){
        this.items = items;
        //use map to record index
        for(int index=0; index<items.size(); index++){
            this.map.put(items.get(index).getName(), index);
        }
    }

    public void sell(String itemName, int amount){
        if(map.containsKey(itemName)){
            int index = this.map.get(itemName);
            this.items.get(index).setAmount(amount);
        }
    }

    public Item getItem(String itemName){
        int index = this.map.get(itemName);
        return this.items.get(index);
    }

    public static void main(String[] args) {
        List<Item> list = new ArrayList<Item>();

        list.add(new Item("Ketchup", 1.00, 10, 2.00, itemType.FOOD));
        list.add(new Item("Mayo", 2.00, 20, 3.0, itemType.FOOD));
        list.add(new Item("Bleach", 3.00, 30, 4.00, itemType.CLEANING));
        list.add(new Item("Lysol", 4.00, 40, 5.00, itemType.CLEANING));

        Driver driver = new Driver(list);
        driver.sell("Ketchup", 100);
        Item item = driver.getItem("Ketchup");
    }
}