英文:
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 + " is " + category + " and we have " + amount + " in warehouse";
}
}
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() {
for (Lists lists : Food) {
System.out.println(lists);
}
}
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("");
}
}
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<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";
}
}
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论