• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading files and producing output  RSS feed

 
rey des
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ambulances.csv




Patient.csv

 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rey,

You can create a Location class that encapsulates an x and a y coordinate. Your Ambulance and Patient classes have a Location, and they can be updated to have a new Location. Ambulance can check that when you relocate it, the target location is within 4 steps, and a Patient can just copy the location of the Ambulance it is transported by.

Whatever class contains the List<Ambulance> and List<Patient>, you can add the following fields to it:

Here, stations just maps a location to the name of the station.
 
rey des
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stephan I havn't created a seperate patient class yet that is empty at the moment so I'm still trying to figure out what to add in there but thank you for the idea
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried adding code tags with the Text option from the dropdown list to your posts, and I think your files look better I suspect however that you are mixing tabs and spaces, which messes up indentation and also makes the files harder to parse.

Maybe set up a Scanner to read the file. Discard the first line with nextLine() then use nextInt() or next() depending on how many columns you have in the files. You can probably use the default delimiter for the Scanner.
The trailing numbers can probably be picked up with nextLine. Get the String from nextLine, call trim on it (remove extraneous spaces) and if it is not empty, pass the String to a new Scanner and call nextInt on that. Otherwise assume the number is 0.
Pass those values to the constructors of your objects.
Beware of nextLine after nextAnythingElse.
Read about Scanners here and here.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Patient has an id, a Location, a Patient.Status and an assigned Ambulance. Ambulance has an id, a Location, an Ambulance.Status and an assigned Patient.

You can move(Location) an Ambulance but only if the new location is within 4 steps, and you can move() a Patient to the location of its assigned Ambulance, but only if the Ambulance's status() is RETURNING.

Start with a class outline and document what the methods are supposed to do and return, before you implement them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!