如何从ArrayList中移除索引并打印已移除索引的整数总和?

huangapple go评论77阅读模式
英文:

How to remove index from ArrayList and print total integer of the removed index?

问题

我想为一个仓库制作一个程序。`emptyStock()` 方法应该从列表中移除已售出的食物。`print()` 方法应该打印出所有食物列表和已售出的总食物重量公斤)。

public class Lists {
    private String name;
    private String category;
    private int amount;

    public Lists(String name, String category, int amount) {
       this.name = name;
       this.category = category;
       this.amount = amount;
    }

    public String toString() {
        return name + "是" + category + ",仓库中有" + amount + "公斤";
    }
}

public class Food {
    ArrayList<Lists> Food = new ArrayList<>();

    public void add(String name, String category, int amount) {
        Food.add(new Lists(name, category, amount));
    }

    public void emptyStock(int index) {
        Food.remove(index);
    }

    public void print() {
        int totalSold = 0;
        for (Lists lists : Food) {
            System.out.println(lists);
            totalSold += lists.getAmount();
        }
        System.out.println(totalSold + "公斤的食物已售出!");
    }

    public static void main(String[] args) {
        Food food = new Food();
        food.add("土豆", "蔬菜", 35);
        food.add("胡萝卜", "蔬菜", 15);
        food.add("苹果", "水果", 30);
        food.add("猕猴桃", "水果", 5);
        food.emptyStock(1);
        food.emptyStock(2);
        food.print();
        System.out.print("");
    }
}

输出应该类似于这样:

土豆是蔬菜,仓库中有35公斤
苹果是水果,仓库中有30公斤
65公斤的食物已售出!

我仍然不确定如何使 print() 方法打印出已从列表中移除的食物总重量(公斤)。

英文:

I want to make a program for a warehouse. emptyStock() method should remove the food that has been sold from the list. print() method should print all list of food and total food (kg) that has been sold.

public class Lists {
private String name;
private String category;
private int amount;
public Lists(String name, String category, int amount) {
this.name = name;
this.category = category;
this.amount = amount;
}
public String toString() {
return name + &quot; is &quot; + category + &quot; and we have &quot; + amount + &quot; in warehouse&quot;;
}
}
public class Food {
ArrayList&lt;Lists&gt; Food = new ArrayList&lt;&gt;();
public void add(String name, String category, int amount) {
Food.add(new Lists(name, category, amount));
}
public void emptyStock(int index) {
Food.remove(index);
}
public void print() {
for (Lists lists : Food) {
System.out.println(lists);
}
}
public static void main(String[] args) {
Food food = new Food();
food.add(&quot;Potato&quot;, &quot;Vegetable&quot;, 35);
food.add(&quot;Carrot&quot;, &quot;Vegetable&quot;, 15);
food.add(&quot;Apple&quot;, &quot;Fruit&quot;, 30);
food.add(&quot;Kiwi&quot;, &quot;Fruit&quot;, 5);
food.emptyStock(1);
food.emptyStock(2);
food.print();
System.out.print(&quot;&quot;);
}
}

The output needs to print something like this:

Potato is Vegetable and we have 35 kg in warehouse
Apple is Fruit and we have 30 kg in warehouse
20 kg of food has been sold! // my code cannot print this part

I am still not sure how to make print() method prints the total amount (kg) of food that has been removed from the list.

答案1

得分: 0

只需在Food类中添加一个成员变量(名为removed)来存储总共移除的数量。在emptyStock()方法中更新这个成员变量的值。在下面的代码中,我添加了这个成员变量 removed

import java.util.ArrayList;

public class Food {
    int removed;
    ArrayList<Lists> foods = new ArrayList<>();

    public void add(String name, String category, int amount) {
        foods.add(new Lists(name, category, amount));
    }

    public void emptyStock(int index) {
        removed += foods.get(index).getAmount();
        foods.remove(index);
    }

    public void print() {
        for (Lists lists : foods) {
            System.out.println(lists);
        }
        System.out.printf("%d kg of food has been sold!%n", removed);
    }

    public static void main(String[] args) {
        Food food = new Food();
        food.add("Potato", "Vegetable", 35);
        food.add("Carrot", "Vegetable", 15);
        food.add("Apple", "Fruit", 30);
        food.add("Kiwi", "Fruit", 5);
        food.emptyStock(1);
        food.emptyStock(2);
        food.print();
        System.out.print("");
    }
}

class Lists {
    private String name;
    private String category;
    private int amount;

    public Lists(String name, String category, int amount) {
       this.name = name;
       this.category = category;
       this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }

    public String toString() {
        return name + " is " + category + " and we have " + amount + " in warehouse";
    }
}

运行上述代码时的输出是:

Potato is Vegetable and we have 35 in warehouse
Apple is Fruit and we have 30 in warehouse
20 kg of food has been sold!
英文:

Just add a class member (in class Food) that stores the total amount removed. In method emptyStock() you update the value of this member. In the below code, I added the member removed.

import java.util.ArrayList;

public class Food {
    int removed;
    ArrayList&lt;Lists&gt; foods = new ArrayList&lt;&gt;();

    public void add(String name, String category, int amount) {
        foods.add(new Lists(name, category, amount));
    }

    public void emptyStock(int index) {
        removed += foods.get(index).getAmount();
        foods.remove(index);
    }

    public void print() {
        for (Lists lists : foods) {
            System.out.println(lists);
        }
        System.out.printf(&quot;%d kg of food has been sold!%n&quot;, removed);
    }

    public static void main(String[] args) {
        Food food = new Food();
        food.add(&quot;Potato&quot;, &quot;Vegetable&quot;, 35);
        food.add(&quot;Carrot&quot;, &quot;Vegetable&quot;, 15);
        food.add(&quot;Apple&quot;, &quot;Fruit&quot;, 30);
        food.add(&quot;Kiwi&quot;, &quot;Fruit&quot;, 5);
        food.emptyStock(1);
        food.emptyStock(2);
        food.print();
        System.out.print(&quot;&quot;);
    }
}

class Lists {
    private String name;
    private String category;
    private int amount;

    public Lists(String name, String category, int amount) {
       this.name = name;
       this.category = category;
       this.amount = amount;
    }

    public int getAmount() {
        return amount;
    }

    public String toString() {
        return name + &quot; is &quot; + category + &quot; and we have &quot; + amount + &quot; in warehouse&quot;;
    }
}

Output when running above code is:

<!-- language: lang-none -->

Potato is Vegetable and we have 35 in warehouse
Apple is Fruit and we have 30 in warehouse
20 kg of food has been sold!

huangapple
  • 本文由 发表于 2020年10月24日 23:07:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64514823.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定