import java.util.*; import java.io.*; class VectorApp{ Vector v = new Vector(); public VectorApp(){ Process(); }//constructor public static void main(String args[]){ new VectorApp(); }//main public void Process(){ try{ String line=""; int pick=0; Info info; Scanner sc = new Scanner(System.in); while(true){ System.out.println("---------------\nadd: Insert Date search(s): Search Data all: Show All write(w): to file quit(q): exit.\n---------------"); line = sc.next().trim(); if(line.equalsIgnoreCase("quit") || line.equalsIgnoreCase("q")){ break; }else if(line.equalsIgnoreCase("add") || line.equalsIgnoreCase("a")){ // add info = new Info(); System.out.println("Please input your name:(spaces are not allowed, please use _ instead)"); //BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); info.name = sc.next(); //info.name = br.readLine().trim(); //System.out.println(info.name); //br.close(); System.out.println("Please input your age:"); info.age = sc.nextInt(); System.out.println("Please choose your department:\n1. Logistics Management\n2. Infomation Management\n3. Marketing Management"); pick = sc.nextInt(); if(pick==1){ info.department = "Logistics Management"; }else if(pick==2){ info.department = "Infomation Management"; }else{ info.department = "Marketing Management"; }//else System.out.println("Please choose your grade:\n1. Freshman\n2. Sophomore\n3. Junior\n4. Senior\n5. Graduate School"); pick = sc.nextInt(); if(pick==1){ info.grade = "Freshman"; }else if(pick==2){ info.grade = "Sophomore"; }else if(pick==3){ info.grade = "Junior"; }else if(pick==4){ info.grade = "Senior"; }else{ info.grade = "Graduate School"; }//else v.add(info); }else if(line.equalsIgnoreCase("search") || line.equalsIgnoreCase("s")){ // search System.out.println("please input the name you are looking for:"); line = sc.next().trim(); for(Info inf:v){//對v中的每一個inf if(inf.name.equalsIgnoreCase(line)){ System.out.println(inf.toString()); break; }//if }//for System.out.println("Not Found"); }else if(line.equalsIgnoreCase("all")){ //System.out.println(v.toString()); for (Info inf:v){ System.out.println(inf.toString()); } }else if(line.equalsIgnoreCase("write") || line.equalsIgnoreCase("w")){ BufferedWriter bw = new BufferedWriter(new FileWriter("info.txt")); for (Info inf:v){ bw.write(inf.toString()); bw.newLine(); }//for bw.flush(); bw.close(); }else{ System.out.println("Wrong instruction, please try again."); } }//while sc.close(); }catch(IOException e){ e.printStackTrace(); }catch(Exception e){ e.printStackTrace(); }//catch }// Process() }//VectorApp class Info{ String name; int age; String department; String grade; Info(){} Info(String name, int age, String department, String grade){ this.name = name; this.age = age; this.department = department; this.grade = grade; }//Constructor public String toString(){ return name+"\t"+age+"\t"+department+"\t"+grade; }//toString() }//Info