package 案例;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;
class action {
public static Scanner scanner = new Scanner(System.in);
private String name;
private String color;
private String price;
private String amount;
public static Collection<action> collection = new ArrayList<action>();
private int x;
public action(String name, String color, String price, String amount) {
this.name = name;
this.color = color;
this.price = price;
this.amount = amount;
}
public action() {
}
public String toString() {
return "商品:" + name + "\t颜色:" + color + "\t价格:" + price + "\t数量:" + amount;
}
public void add() {
String xString;
System.out.println("是否要录入商品");
xString = scanner.next();
if (xString.equals("是")) {
System.out.print("商品名称:");
String name = scanner.next();
System.out.print(" 颜色:");
String color = scanner.next();
System.out.print(" 价格:");
String price = scanner.next();
System.out.print(" 数量:");
String amount = scanner.next();
action action = new action(name, color, price, amount);
collection.add(action);
System.out.println(name + "录入成功!");
} else {
System.out.println("返回系统首页!");
}
}
public void show() {
System.out.println("仓库所有商品信息:");
for (Object object : collection) {
System.out.println(object);
}
}
public void delete() {
Iterator<action> iterator = collection.iterator();
System.out.println("删除商品的编号为:");
x = scanner.nextInt();int i = 1;
while (iterator.hasNext()) {
if(i==x) {
iterator.remove();
}
Object object= iterator.next();
System.out.println(object);
i++;
}
}
}
public class P146_6杠1库存管理系统 {
public static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
action action = new action();
menu();
while (true) {
String string;
System.out.println("输入选择的功能:");string = scanner.next();
switch (string) {
case "商品入库功能":
action.add();
break;
case "商品显示功能":
action.show();
break;
case "删除商品功能":
action.delete();
break;
case "退出系统功能":
System.exit(0);
default:
System.out.println("不存在该功能!");
break;
}
}
}
public static void menu() {
System.out.println("库存管理系统:");System.out.println("1.商品入库功能:");
System.out.println("2.商品显示功能:");
System.out.println("3.删除商品功能:");
}
}
对于上面的delete方法中,我是想靠记录循环次数,在指定的循环位置删除对象数据,为啥会报错说我非法,对于迭代器自己的方法它应该是能预支的呀,为啥报错