我有一个程序,将CSV文件中的数据放入ArrayList
。在用户菜单上,用户将首先按1来执行此操作,即从CSV文件中调用数据并将其放入ArrayList
,然后按2以便控制台向用户显示哪些数据是存储
如何执行选项2?
这是我的菜单:
public class Main {
public static void main(String args[]){
SimpleCarFactory factory = SimpleCarFactory.getInstance();
CarStore store = new CarStore(factory);
Car Vauxhall = store.createCar("Vaux");
Car Volvo = store.createCar("Vol");
Scanner input = new Scanner(System.in);
int choice;
do
{
userInput();
choice = input.nextInt();
switch(choice)
{
case 1:
vauxhall.getData();
System.out.println("Data for " + vauxhall.getName() + " car loaded successfully");
volvo.getData();
System.out.println("Data for " + vol.getName() + " car loaded successfully");
break;
case 2:
System.out.println(vauxhall.getData());
System.out.println(vauxhall.getData());
case 0:
System.out.println("BYE");
System.exit(0);
break;
}
System.out.println();
}while(choice !=0);
input.close();
}
}
这是我的Car类:
public class Car {
static String name;
static FileParser fileParser = null;
ArrayList data = new ArrayList();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ArrayList getData() {
return data;
}
public void setData(ArrayList data) {
this.data = data;
}
public Card(String name) {
super();
this.name = name;
}
}
这是我的一个汽车子类:
public class Vauxhall extends Car {
public Vauxhall(String name) {
super(name);
// TODO Auto-generated constructor stub
}
static FileParser fileParser = null;
public ArrayList getData() {
fileParser = new FileParser();
ArrayList<String> data = fileParser.parseFile("filepath.csv");
List<Vauxhall> VauxhallList = fileParser.populateData(data);
return data;
}
}
答案 0 :(得分:0)
只需存储数据arraylists,并在case 2
中使用它们(我还添加了一个缺少的break
语句):
ArrayList vauxhallData = null;
ArrayList volvoData = null;
switch(choice)
{
case 1:
vauxhallData = vauxhall.getData();
System.out.println("Data for " + vauxhall.getName() + " car loaded successfully");
volvoData = volvo.getData();
System.out.println("Data for " + vol.getName() + " car loaded successfully");
break;
case 2:
System.out.println(vauxhallData);
System.out.println(volvoData);
break;
case 0:
System.out.println("BYE");
System.exit(0);
break;
}