2014-04-01 1 views
-1

안녕하세요 저는이 코드를 가지고 있으며 사용자에게 비디오 및 고객 ID를 입력하고 hires라는 배열 목록에 추가 할 것을 요청합니다. 파일에 arraylist를 저장하는 방법은 무엇입니까? 이 프로그램을 마치고 종료 했습니까?Java 저장 방법

import java.util.ArrayList; 
    import java.util.Scanner; 


    public class Driver { 
     public static final int LISTCUSTOMERS = 1; 
     public static final int LISTVIDEOS = 2; 
     public static final int LISTHIRES = 3; 
     public static final int HIRESTATISTICS = 4; 
     public static final int HIREVIDEO = 5; 
     public static final int RETURNVIDEO = 6; 
     public static final int QUIT = 7; 
     public final static int DEFAULT = 0; 
     private String[] menuText; 
     private static final int MAXVIDEOS = 9; 
     private static final int MAXCUSTOMERS = 5; 
     static Scanner sc = new Scanner(System.in); 
     private int selection; 
     public int MAXLENGTH = 50; 
     public static ArrayList<Hire>HireList = new ArrayList<Hire>(); 
     public static Video[]videos= new Video[MAXVIDEOS]; 
     public static Customer [] customers = new Customer[MAXCUSTOMERS]; 
     public static String [] options = {"","List Customers", "List Videos", "List Hires", "Hire Statistics", "Hire Video", "Return Video", "Quit"}; 
     public static Driver myMenu = new Driver (options, sc); 
     public static boolean more = true; 
     public static int option = 0; 
     static Customer customer; 

     public static void main (String [] args){ 
      initilisation(); 
      runHireVideo(); 
     } 
     public Driver (String[] menuText, Scanner sc){ 
      this.menuText = menuText; 
     } 
      public int getMenuSelection(){ 
      this.displayMenu(); 
      this.selection = sc.nextInt(); 
       if(this.selection < DEFAULT || this.menuText.length<this.selection){ 
        System.out.println("Your selection was invalid - using default"); 
        this.selection = DEFAULT; 
       } 
      return this.selection; 
     } 
     public void displayMenu(){ 
      System.out.print("\nMicroVideo Menu: \n"); 
       for (int index = 1; index < menuText.length; index++) 
        System.out.println(index + ". "+ menuText [index]); 
       System.out.print("Enter option: "); 
     } 
     public static void runHireVideo(){ 

      while (more){ 
       option = myMenu.getMenuSelection(); 
       switch(option){ 
       case LISTCUSTOMERS: 
        ListCustomers(); 
       break; 
       case LISTVIDEOS: 
        ListVideos(); 
       break; 
       case LISTHIRES: 
        ListHires(); 
       break; 
       case HIRESTATISTICS: 
        HireStatistics(); 
       break; 
       case HIREVIDEO: 
        HireVideo(); 
       break; 
       case RETURNVIDEO: 
        ReturnVideo(); 
       break; 
       default: 
        more = false; 
        System.out.println("Program Exited"); 
       break; 
       } 
      } 
     } 
     public static void initilisation(){ 
      videos[0]=new Video("140","The Matrix ", 7.50,  3); 
      videos[1]=new Video("141", "Terminator 2 ", 5.00,  3); 
      videos[2]=new Video("142", "Shrek  ", 5.00,  10); 
      videos[3]=new Video("143", "The Castle ", 5.00,  1); 
      videos[4]=new Video("146", "Sound Of Music ", 1.00,  23); 
      videos[5]=new Video("147", "Planet Of The Apes", 5.00,  0); 
      videos[6]=new Video("148", "Mission Impossible", 1.00,  15); 
      videos[7]=new Video("150", "Bagdad by Night", 6.00,   5); 
      videos[8]=new Video("151", "Lord of the Rings 1", 5.00,   0); 

      customers [0]= new Customer("9902JI", "Innes ", 0,43484001); 
      customers [1]= new Customer("8906RH", "Herbert", 0,43484000); 
      customers [2]= new Customer("9012GT", "Turner ", 0,43480009); 
      customers [3]= new Customer("9012GS", "Sparke ", 0,43480007); 
      customers [4]= new Customer("9012MV", "Vallance", 0,43480008); 
     } 
     public static void ListCustomers(){ 
      System.out.printf("%-6s %-5s %6s\n", "Customer ID", "Customer Name", "  Customer Phone Number"); 
      for (int i=0;i<customers.length;i++){ 
      System.out.println(customers[i]); 
      } 
     } 
     public static void ListVideos(){ 
      System.out.printf("%-8s %-17s %6s %6s\n", "Video ID", "VideoName", " Video Price Rate", " In Stock"); 
      for (int i=0;i<videos.length;i++){ 
      System.out.println(videos[i]); 
      } 
     } 
     public static void ListHires(){ 
      System.out.printf("%s %s %s %s %s \n","HireID", "Cust Name", "Video Name", "Days Hired", "Hire Cost"); 
      for (Hire hire: HireList){ 
       String Customer = customers[hire.getID(hire.getVideoID())].getCustomerName(); 
       String videoID = videos[hire.getID(hire.getVideoID())].getTitle(); 
       double DailyRate = videos[hire.getID(hire.getVideoID())].getRate()*hire.getDaysHired(); 
       System.out.printf("%d %s %s %d %1.2f \n",hire.getHireID(), Customer, videoID, hire.getDaysHired(), DailyRate); 
      } 
     } 
     public static void HireStatistics(){ 
      System.out.printf("%s %s %s\n", "Video ID", "Video Name", "Days Hired"); 
       for (Hire hire: HireList){ 
        String videoID = videos[hire.getID(hire.getVideoID())].getTitle(); 
        System.out.printf("%s  %s %d \n",hire.getVideoID(), videoID, hire.getDaysHired()); 
      } 
     } 
     public static void HireVideo(){ 
      int count =0; 
      while (count == 0){ 
       int hireID = 1000+ HireList.size()+1; 
       System.out.println("Enter Customer ID"); 
       String customerID = "9902JI"; 
       System.out.println("Enter Video ID"); 
       String videoID = "140"; 
       System.out.println("Enter Days Hired"); 
       int daysHired = 4; 
       System.out.println("Enter Another Hire [Y/N]?"); 
       String Yes = sc.next(); 
        if(Yes.equals("y")){ 
        count = 0; 
       } 
       else if (Yes.equals("n")){ 
        count = 1; 
       } 
       HireList.add(new Hire(hireID,customerID, videoID, daysHired, 0)); 
      } 

     } 
     public static void ReturnVideo(){ 
      System.out.println("Enter Customer ID"); 
      String customerID = "9902JI"; 
      System.out.println("Enter Video ID"); 
      String videoID = "140"; 

      for (int i = 0; i < HireList.size();i++){ 
       if (HireList.get(i).getCustomerID().equals(customerID)){ 
        HireList.remove(i); 
       } 
      } 
      for (int ii = 0; ii < HireList.size();ii++){ 
        if (HireList.get(ii).getVideoID().equals(videoID)){ 
         HireList.remove(ii); 
       } 
      } 
      System.out.println("Removed " + customerID); 
     } 

    } 
+0

[사용 가능한 복제 (http://stackoverflow.com/q/19065797/1883647) – ajp15243

+1

아이디어는 당신이 실제 자바 객체를 저장하지 않는다는 것입니다. 데이터 (변수/필드)를 텍스트로 저장합니다. 그런 다음로드하기 위해 새 ArrayList에 넣은 새 객체를 만들기 위해 텍스트를 읽습니다. 그렇게한다면 데이터는 동일해질 것입니다. – Aarowaim

답변

1

가장 간단한 방법은 앞서 언급 한 바와 같이, 텍스트 파일의 각 구성원을 저장하고 스캐너 객체 (http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html)와 다시 읽을 단순히 것입니다.

텍스트 파일에 쓰는 방법에 대해 궁금한 점이 있으면이 문서의 내용을 읽어 보시기 바랍니다 : FileWriter 개체를 사용하는 방법을 설명합니다.

다른 옵션은 Driver가 일련의 바이트로 전체 Driver 객체를 저장할 수있는 serialisable 인터페이스를 구현하도록하는 것이지만 약간 더 복잡합니다. 직렬화에 괜찮은 튜토리얼은 여기에서 찾을 수 있습니다 : http://www.tutorialspoint.com/java/java_serialization.htm