-1

hello i have this code and it asks the user to enter a video and customer id and adds it to an array list called hires how would i save that arraylist to a file when the are finished and quit this program?

    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 );
        }

    }
2
  • Possible duplicate Commented Apr 1, 2014 at 2:25
  • 1
    The idea is that you don't save the actual java object. You save the data (variables/fields) as text. Then to load it, you read the text to create new objects that you put into a new ArrayList. If you do it right, the data will be the same. Commented Apr 1, 2014 at 2:40

1 Answer 1

1

The simplest approach, as mentioned before, would be simply to save each member in a text file and read them back in with a Scanner object (http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html).

If you are concerned with how to write to a text file, I would suggest reading this article: http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/ which explains how to use the FileWriter Object.

An alternative option would be to have Driver implement the serialisable interface which would allow you store the entire Driver object in a series of bytes, but this is slightly more complicated. A decent tutorial on serialisation can be found here: http://www.tutorialspoint.com/java/java_serialization.htm

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.