嘿嘿

   try {
             while (true) {
                 System.out.println("1.插入  2.刪除  3.搜索 4.顯示所有記錄 5.退出");
                 System.out.println("請選擇功能號");
                 BufferedReader br = new BufferedReader(new
                         InputStreamReader(System.in));
                 choice = (char) br.read();
                 switch (choice) {
                 case '1':
                     try {
                         System.out.println("輸入學生 ID");
                         BufferedReader br1 = new BufferedReader(new
                                 InputStreamReader(System.in));
                         id = Integer.parseInt(br1.readLine());
                         System.out.println("輸入學生姓名");
                         name = br1.readLine();
                         System.out.println();
                         System.out.println("輸入住址");
                         address = br1.readLine();
                         System.out.println();
                         System.out.println("輸入課程");
                         course = br1.readLine();
                         System.out.println();
                         home.create(new Integer(id), name, address, course);//添加代碼
                         System.out.println("記錄已成功添加");
                         System.out.println();
                     } catch (Exception error) {
                         System.out.println("錯誤!");
                         System.out.println();
                     }
                     break;
 
 
                 case '2':
                     System.out.println();
                     System.out.println("輸入要刪除記錄的學生 ID");
                     BufferedReader br12 = new BufferedReader(new
                             InputStreamReader(System.in));
                     id = Integer.parseInt(br12.readLine());
                     home.remove(new Integer(id));//刪除
                     System.out.println();
                     System.out.println("記錄已成功刪除");
                     break;
 
                 case '3':
                     try {
                         System.out.println();
                         System.out.println("輸入學生 ID");
                         BufferedReader br1 = new BufferedReader(new
                                 InputStreamReader(System.in));
                         input = Integer.parseInt(br1.readLine());
                         remote = home.findByPrimaryKey(new Integer(input));
                         System.out.println();
                         System.out.println("學生的詳細信息爲:");
                         System.out.println();
                         System.out.println("學生 ID:" + remote.getStudentId());
                         System.out.println("學生姓名:"
                                            +
                                            fomart(remote.getStudentName()));
                         System.out.println("住址:"
                                            +
                                            fomart(remote.getAddress()));
                         System.out.println("課程:" + remote.getCourse());
                         System.out.println();
                     } catch (ObjectNotFoundException error) {
                         System.out.println("無此學生");
                         System.out.println();
                     }
                     break;
 
                 case '4':
 
                     java.util.Iterator display
                             = home.findAllStudents().
                                                  iterator();
                     System.out.println();
                     System.out.println("學生 ID     學生姓名          住址        課程");
                     System.out.println(
                             "-------------------"
                             + "----------------------------------------");
 
//                     Integer i= new Integer(0);
                     Integer j = new Integer(0);
                     int i = 0;
                     while (display.hasNext()) {
                         StudentDetailRemote records = (StudentDetailRemote)
                                 display.next();
                         i = i + records.getStudentId().intValue();
                         System.out.print(records.getStudentId());
                         System.out.print("              "
                                          +
                                          fomart(records.getStudentName()));
                         System.out.print(" " + fomart(records.getAddress()));
                         System.out.print("     " + records.getCourse());
                         System.out.println();
                     }
                     System.out.println(i);
                     System.out.println(
                             "-------------------"
                             + "----------------------------------------");
                     System.out.println();
                     break;
                 case '5':
                     System.out.println("應用程序已終止");
                     System.exit(0);
                     break;
                 default:
                     System.out.println("無效選項");
                 }
             }
 
         } catch (Exception error) {
             System.out.println(error);
         }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章