Advertisement
spiny94

Untitled

Sep 8th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.53 KB | None | 0 0
  1. package gestioneTaxi;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Queue;
  9.  
  10. public class TaxiCompany {
  11.     private Map<String,Taxi> elencoTaxi;
  12.     private Map<String,Quartiere> elencoQuartieri;
  13.     private Queue<Taxi> codaTaxi;
  14.     private Map<Taxi, ArrayList<Corsa>> corseTaxi;
  15.     private int corsePerse = 0;
  16.  
  17.     public TaxiCompany() {
  18.         elencoTaxi = new HashMap<>();
  19.         elencoQuartieri = new HashMap<>();
  20.         codaTaxi = new LinkedList<>();
  21.         corseTaxi = new HashMap<>();
  22.     }
  23.        
  24.     public void addTaxi(String id) throws InvalidTaxiName {    
  25.         if(elencoTaxi.containsKey(id))
  26.             throw (new InvalidTaxiName());
  27.         Taxi t = new Taxi(id);
  28.         elencoTaxi.put(id, t);
  29.         t.setCompany(this);
  30.         corseTaxi.put(t, new ArrayList<Corsa>());
  31.         codaTaxi.add(t);
  32.     }
  33.    
  34.     public Queue<Taxi> getLiberi() {
  35.         return codaTaxi;
  36.     }
  37.  
  38.     public Taxi chiamataTaxi(Passeggero p) {
  39.         if(codaTaxi.isEmpty()){
  40.             corsePerse++;
  41.             return null;
  42.         }
  43.         else {
  44.             Taxi t = codaTaxi.remove();
  45.             t.chiamata(p);
  46.             return t;
  47.         }
  48.     }
  49.    
  50.     public void nuovaCorsa(Taxi t, Corsa c){
  51.         ArrayList<Corsa> corse = corseTaxi.get(t);
  52.         corse.add(c);
  53.         corseTaxi.put(t, corse);
  54.     }
  55.    
  56.     public void corsaTerminata (Taxi t, Luogo arrivo) {
  57.         codaTaxi.add(t);
  58.         String qid = arrivo.getQuartiere();
  59.         Quartiere q = elencoQuartieri.get(qid);
  60.         if(q == null){
  61.             q = new Quartiere(qid);
  62.             elencoQuartieri.put(qid, q);
  63.         }
  64.         q.addCorsa();
  65.     }
  66.    
  67.     public List<Corsa> getCorse(String id) throws InvalidTaxiName {
  68.         Taxi t = elencoTaxi.get(id);
  69.         if(t == null)
  70.             throw (new InvalidTaxiName());
  71.         return corseTaxi.get(t);
  72.     }
  73.    
  74.     public int getCorsePerse(){
  75.         return corsePerse;
  76.     }
  77.    
  78.     public ArrayList<InfoI> statisticheTaxi() {
  79.         ArrayList<InfoI> list = new ArrayList<InfoI>(elencoTaxi.values());
  80.         Collections.sort(list);
  81.         return list;
  82.     }
  83.    
  84.     public ArrayList<InfoI> statisticheQuartieri() {
  85.         ArrayList<InfoI> list = new ArrayList<InfoI>(elencoQuartieri.values());
  86.         Collections.sort(list);
  87.         return list;
  88.     }
  89. }
  90. package gestioneTaxi;
  91.  
  92. public interface InfoI extends Comparable<InfoI>{
  93.     String getId();
  94.     int getValore();
  95. }
  96. package gestioneTaxi;
  97.  
  98. public class Corsa {
  99.    
  100.     private Luogo partenza, arrivo;
  101.  
  102.     public Corsa(Luogo p, Luogo a) {
  103.         partenza = p;
  104.         arrivo = a;
  105.     }
  106.    
  107.     public String toString() {
  108.         return partenza + "," + arrivo;
  109.     }
  110. }package gestioneTaxi;
  111.  
  112. public class Taxi implements InfoI{
  113.     private TaxiCompany company;
  114.     private Luogo partenza, arrivo;
  115.     private Passeggero passeggero;
  116.     private String id;
  117.     private int numCorse;
  118.  
  119.     public Taxi(String ids) {
  120.         id = ids;
  121.         numCorse = 0;
  122.     }
  123.    
  124.     public void setCompany(TaxiCompany tc){
  125.         company = tc;
  126.     }
  127.    
  128.     public void chiamata(Passeggero p) {
  129.         partenza = p.getPosizione();
  130.         passeggero = p;
  131.     }
  132.    
  133.     public void inizioCorsa(Luogo destinazione) {
  134.         arrivo = destinazione;
  135.         company.nuovaCorsa(this, new Corsa(partenza, arrivo));
  136.     }
  137.    
  138.     public void fineCorsa(){
  139.         passeggero.setPosizione(arrivo);
  140.         company.corsaTerminata(this, arrivo);
  141.         numCorse++;
  142.     }
  143.    
  144.     public String toString(){
  145.         return id;
  146.     }
  147.  
  148.     @Override
  149.     public String getId() {
  150.         return id;
  151.     }
  152.  
  153.     @Override
  154.     public int getValore() {
  155.         return numCorse;
  156.     }
  157.  
  158.     @Override
  159.     public int compareTo(InfoI o) {
  160.         int d = o.getValore() - numCorse;
  161.         if(d != 0)
  162.             return d;
  163.         return id.compareTo(o.getId());
  164.     }
  165.  
  166. }
  167. package gestioneTaxi;
  168.  
  169. public class Quartiere implements InfoI {
  170.    
  171.     private String id;
  172.     private int numCorse;
  173.    
  174.     public Quartiere (String ids){
  175.         id = ids;
  176.         numCorse = 0;  
  177.     }
  178.    
  179.     public void addCorsa(){
  180.         numCorse++;
  181.     }
  182.  
  183.     @Override
  184.     public int compareTo(InfoI o) {
  185.         int d = o.getValore() - numCorse;
  186.         if(d != 0)
  187.             return d;
  188.         return id.compareTo(o.getId());
  189.     }
  190.  
  191.     @Override
  192.     public String getId() {
  193.         return id;
  194.     }
  195.  
  196.     @Override
  197.     public int getValore() {
  198.         return numCorse;
  199.     }
  200.  
  201. }package gestioneTaxi;
  202.  
  203. public class Passeggero {
  204.    
  205.     private Luogo posizione;
  206.  
  207.     public Passeggero(Luogo pos) {
  208.         posizione = pos;
  209.     }
  210.    
  211.     public Luogo getPosizione() {
  212.         return posizione;
  213.     }
  214.    
  215.     public void setPosizione(Luogo pos) {
  216.         posizione = pos;
  217.     }
  218.  
  219. }
  220. package gestioneTaxi;
  221.  
  222. public class Luogo {
  223.    
  224.     private String indirizzo;
  225.     private String quartiere;
  226.    
  227.     public Luogo(String ind, String quart) {
  228.         indirizzo = ind;
  229.         quartiere = quart;
  230.     }
  231.    
  232.     public String getQuartiere(){
  233.         return quartiere;
  234.     }
  235.    
  236.     public String toString() {
  237.         return indirizzo;
  238.     }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement