简体   繁体   中英

How do I read in a code from a text file and based on that code do different things in Java?

I'm trying to read in a specific text file that will contain either codes 1, 2, 3, or 4. My program is supposed to keep track of flights and reservations. If the code is 1, it will add a new reservation, so the 1 is followed up by all the information needed for a reservation. If the code is 2, it will give me a reservation number, and I am supposed to print the itinerary for that reservation. If it is 3, I print all itineraries, and if it is 4, I write all of the reservations to a new file. I'm not sure how to go about this, and I'm really stuck! Please help!

Here is what the text file contains:

1  
John Miller  
1234  
UA1235
06/12/2014
ORD
LGA
4:00 PM
7:15 PM
30F
UA673
06/19/2014
LGA
ORD
10:00 AM
11:25 AM
10E
3
1
Bob Barker
8497
UA317
08/04/2014
ORD
SFO
8:10 AM
10:45 AM
12A
UA728
08/14/2014
SFO
ORD
12:52 PM
7:03 PM
18A
2
1235
2
8976
3
4

I do not necessarily need specifics which is why I'm not providing any code. I was just wondering how I can go about doing this. Let me know if you need more to answer the question.

So when you read in each line, just split it by a space, and then convert the string in the first index to an integer.

Then just use a switch for that to determine what you should do.

Since you aren't using a database, just put the reservation into a hash map, so it is faster to look up later.

This should help you start.

OK so if i'm understanding this correctly here is the psedocode you will use for your application.

1. Read file line by line.
2. Check if line.equals() 1,2,3 or 4.
3. If the line equals 1,2,3 or 4 then jump to function 1(),2(),3() or 4() //Name these better.

Function 1

1. This function should contain one parameter which is an object or array which stores the 16 lines of text which follows a '1' in the file.
2. You should now use this information as needed.
3. You should maybe create a custom class for this information.

Function 2

1. The function parameter should be the reservation number.
2. Fetch the data from this reservation.
3. Print the data.

Function 3

1. No parameters.
2. Print all classes which have been created using function 1?

Function 4

1. Get all objects created by function 1.
2. Loop through these objects and print each one to file.

Some notes here,

The most important function is function 1. You want to create a list / array which stores all of the objects created in function 1.... this will help with future stages, for example if your file contains function 3 then you can loop through the list and print out each itinerary. I'm making the assumption that you know what objects are and how to create custom ones.

If you need further explanations I can attempt to give them to you. I suggest practicing with file input / output before tackling a problem like this.

Probably this should help u start.... As James said you are better using Hashmap.

import java.io.*;
import  java.util.*;
public class Test5{
    public static void main(String[] args) {
        Test5 test=new Test5();
        test.calculate("abc.txt");
    }
    public void calculate(String filename){
        String thisLine=null;
        int reservationDetails=16;  //no of loops to input reservation details
        HashMap<String,String> hm=new HashMap<String,String>();
        ArrayList<HashMap<String,String>> reservationData=new ArrayList<HashMap<String,String>>();
        try{
            FileReader infile=new FileReader(filename);
            BufferedReader br=new BufferedReader(infile);
            FileWriter outputfile=new FileWriter("output.txt");

            thisLine=br.readLine();
            while(thisLine!=null){

                int code=Integer.parseInt(thisLine);
            // System.out.println(code);
                switch(code){
                    case 1:
                    for(int i=1;i<=reservationDetails;i++){
                        thisLine=br.readLine();
                    //String[] words =thisLine.split(" ");  //only if you require individual word
                        hm.put("Name",thisLine);
                        hm.put("id",thisLine);
                        hm.put("reservationNumber",thisLine);
                    // do this for all of your data

                    }
                    reservationData.add(new HashMap<String,String>(hm));

                    break;

                    case 2:
                    System.out.println(hm.get("reservationNumber"));
                    break;

                    case 3:
                    System.out.println("Printing all reservation ");
                    Set<Map.Entry<String,String>> set;
                    for (HashMap<String,String> hms :reservationData ) {
                        set=hms.entrySet();
                        for (Map.Entry entity : set ) {
                            System.out.println(entity.getKey()+": "+entity.getValue()); 
                        }
                    }
                    break;
                    case 4:
                    Set<Map.Entry<String,String>> set2;
                    for (HashMap<String,String> hms :reservationData ) {
                        set2=hms.entrySet();
                        for (Map.Entry entity : set2 ) {
                            outputfile.write(entity.getKey()+": "+entity.getValue()+"\n");

                        }
                    }

                    break;

                }
                thisLine=br.readLine();
            }
            outputfile.close();
        }
        catch(Exception e){
            System.out.println(e);
        }
    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM