?xml version="1.0" encoding="UTF-8"?>
package RentalService; import java.util.ArrayList; import java.util.Random; public class Rental implements IRental{ private static final long serialVersionUID = 6324598725198583458L; protected Branch[] branches=null; protected ArrayList<Reservation> reservations=new ArrayList<Reservation>(); public boolean initialise(Branch Branches[]){ this.branches = Branches; return setObjects(); } public String registerClient(String city, String clientName){ Branch cBranch = getBranch(city); if (cBranch !=null){ Client newClient = new Client(); newClient.cName =clientName; newClient.cID = cBranch.city + cBranch.cMax; newClient.cID = cBranch.city + "_" + (cBranch.ofClients.size()); cBranch.ofClients.add(newClient); setObjects(); return newClient.cID; } return null; } public String makeReservation(String ClientID, String pickup, String dropoff){ Branch pickupBranch = getBranch(pickup); Branch dropOffBranch = getBranch(dropoff); // Client from pickup branch Client clientMade = getClient(pickupBranch, ClientID); Car car = getCar(pickupBranch); if (pickupBranch==null || dropOffBranch==null || clientMade==null || car==null){ return null; } car.getRegistration(); Reservation mReservation = new Reservation( pickupBranch.city + "_" + pickupBranch.rMax, clientMade, pickupBranch, dropOffBranch, car); pickupBranch.rMax++; this.reservations.add(mReservation); setObjects(); return mReservation.reference; } public void cancelReservation(String Reference){ int iIndex = getReservationIndex(Reference); if (iIndex!=-1){ this.reservations.remove(iIndex); } setObjects(); } public void cancelClientReservation(String clientID){ for (int iIndex=this.reservations.size()-1; iIndex>=0; iIndex--){ if (!this.reservations.get(iIndex).made.cID.equalsIgnoreCase(clientID)){ continue; } this.reservations.remove(iIndex); } setObjects(); } public void pickupCar(String Reference){ int iIndex = getReservationIndex(Reference); if (iIndex==-1){ return; } Reservation getReservation = this.reservations.get(iIndex); // check if it wasn't picked up already if (getReservation.pickup==null){ return; } // check if the reserved car still exist in the pick-up branch iIndex=-1; for (int iCarIndex=0; iCarIndex <getReservation.pickup.atCars.size(); iCarIndex++){ if (getReservation.pickup.atCars.get(iCarIndex).getRegistration().equalsIgnoreCase(getReservation.carFor.getRegistration())){ iIndex=iCarIndex; break; } } if (iIndex==-1){ return; } // remove car from branch getReservation.pickup.atCars.remove(iIndex); // remove pickup branch getReservation.pickup=null; setObjects(); } public void dropoffCar(String Reference){ int iIndex = getReservationIndex(Reference); if (iIndex==-1){ return; } Reservation getReservation = this.reservations.get(iIndex); // check if it has been picked up already if (getReservation.pickup!=null){ return; } // add car to drop-off branch getReservation.dropoff.atCars.add(getReservation.carFor); // remove reservation object this.reservations.remove(iIndex); } public void deliverCars(String strFrom, String strTo){ Branch fromBranch = getBranch(strFrom); Branch toBranch = getBranch(strTo); if (fromBranch==null || toBranch==null){ return; } for (int i=fromBranch.atCars.size()-1; i>=0; i--){ Car checkCar = fromBranch.atCars.get(i); if (checkCar.getOwnedBy().equals(toBranch)){ fromBranch.atCars.remove(i); toBranch.atCars.add(checkCar); } } } public ArrayList<Reservation> showClientReservations(String clientID){ ArrayList<Reservation> getClientReservations = new ArrayList<Reservation>(); for (Reservation reservation: reservations){ if (reservation.made.cID.equalsIgnoreCase(clientID)){ getClientReservations.add(reservation); } } return getClientReservations; } public ArrayList<Client> showClients (String city){ Branch clientsInBranch = this.getBranch(city); if (clientsInBranch!=null){ return clientsInBranch.ofClients; } return null; } /* public Client[] showClients2 (String city){ Branch clientsInBranch = this.getBranch(city); if (clientsInBranch!=null){ Client[] returnClient= (Client[])clientsInBranch.ofClients.toArray(new Client[clientsInBranch.ofClients.size()]); return returnClient; } return null; }*/ public ArrayList<Car> showCars (String city){ Branch carsInBranch = this.getBranch(city); if (carsInBranch!=null){ return carsInBranch.atCars; } return null; } private Client getClient(Branch branch, String ClientID){ if (branch==null) { return null; } for (Client client: branch.ofClients){ if (client.cID.equalsIgnoreCase(ClientID)){ return client; } } return null; } private Branch getBranch(String City){ City = City.trim(); if (City.length()==0){ return null; } for (Branch branch: branches){ if (branch.city.equalsIgnoreCase(City)){ return branch; } } return null; } private Car getCar(Branch branch){ if (branch==null || branch.atCars.size()<1){ return null; } Random randomCar = new Random(); int iCar = randomCar.nextInt(branch.atCars.size()); if (iCar>= branch.atCars.size() || iCar<0){ iCar=0; } return branch.atCars.get(iCar); } private int getReservationIndex(String Reference){ for (int iIndex=0; iIndex<this.reservations.size(); iIndex++){ if (this.reservations.get(iIndex).reference.equalsIgnoreCase(Reference)){ return iIndex; } } return -1; } private boolean setObjects(){ /* try { ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(new File(this.cfsId))); oos.writeObject(this); oos.flush(); oos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } */ return true; } private boolean getObjects(){ /* ObjectInputStream ois; try { ois = new ObjectInputStream( new FileInputStream(new File(this.cfsId))); Rental rental = (Rental)ois.readObject(); this.branches = rental.branches; this.reservations =rental.reservations; } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } catch (ClassNotFoundException e) { e.printStackTrace(); return false; } */ return true; } }