Click here to Skip to main content
15,313,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a class which reads an ambulances.csv and patients.csv file, run for a set period and then output the updated information to ambulances-2.csv and patients-2.csv (both input and output files use the same data formats).
Each patient has an (x, y) location. The initial positions of the ambulances and patients are read in from the data files. Every second, the ambulances will update their status and position. The new status and position depends on the current status:
The hospital: this exists on the grid at location (50, 50) • The ambulance stations: there are three stations at different locations on the grid: o Greenfields – (10, 0) o Bluelane – (30, 80) o Redvill – (90, 20)

1st spec ‘At station’: check if there is a new patient to pick up, if so, assign the closest unassigned patient to the ambulance and change the status to ‘Responding’. Otherwise, do nothing.

2nd spec ‘Responding’: move the ambulance towards the assigned patient by four moves. If the ambulance reaches the patient, change the status to ‘At scene’.

I was thinking to maybe create an ArrayList<Ambulance> and ArrayList<Patient> to be able to reference it and use if statements and then print it out onto the console to get an idea? But I wasn''t sure how to reference the hospital and stations. Any ideas would be great ! my second question as well so any criticism on the style is happily accepted

Contents of files.

id	x.location	y.location	status	patient 
A1	  10	               0	At Station	
A2   10	               0	At Station	
A4	  30	              80	At Station	
A8   90	             20	At Station	
A16	  90	             20	At Station	
A64	  12	             12	Responding	1 
A32	  50	             50	At Destination	
A128	  30	             76	Returning	


id	x.location	y.location	status	ambulance 
1	17	             19	Assigned	A64 
2	50	             50	Completed	
3	78	             29	Pending	
4	97	             12	Pending	
5	27	              17	Pending	
6	28	               17	Pending	
7	67	               2	Pending	
8	3	             99	Pending	
9	30	             81	Pending	
10	49	             50	Pending

What I have tried:

reading a file and I displayed it on Jtable but i want to try display it on console first

try {
    BufferedReader br = new BufferedReader(new FileReader(new File("ambulances.csv")));

    ArrayList<String[]> elements = new ArrayList<String[]>();
    String line = "";
    while((line = br.readLine())!=null) {
         String[] splitted = line.split(",");

    String[] columNames = new String[] {"ID", "Location", "Status", "Patient"};

    final Object[][] content = new Object[elements.size()-1][5];

    for(int i=1; i<elements.size(); i++) {
        content[i-1][0] = elements.get(i)[0];
        content[i-1][1] = "("+ elements.get(i)[1] + ", " + elements.get(i)[2] + ")";
        content[i-1][2] = elements.get(i)[3].replace("\"", "");
        if (elements.get(i).length == 5) {
            content[i-1][3] = elements.get(i)[4].replace("\"", "");
        else content[i-1][3] = "-";


    table = new JTable();
    table.setModel(new DefaultTableModel(content,columNames));
Updated 19-Oct-16 22:25pm
Richard MacCutchan 20-Oct-16 4:29am
Why are you using raw objects, strings and arrays? You should create proper classes for your Ambulances and your Patients, and if necessary have properties that link one with the other.
Member 12804008 20-Oct-16 4:44am
I do have classes but it's to big to put on here :/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900