Advertisement
Guest User

Projecto3 LABP FCUL

a guest
Mar 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.93 KB | None | 0 0
  1. public class Carro {
  2.  
  3.     private static final int KM = 20000;
  4.  
  5.     private int numReg;
  6.     private int comp;
  7.     private int km;
  8.     private int proxRev;
  9.     private boolean revisaoFeita;
  10.  
  11.     public Carro (int numRegisto, int comprimento, int km) {
  12.         this.numReg = numRegisto;
  13.         this.comp = comprimento;
  14.         this.km = km;
  15.         this.proxRev = this.km + KM;
  16.         this.revisaoFeita = true;
  17.     }
  18.  
  19.     public int obtemKm () {
  20.         return this.km;
  21.     }
  22.  
  23.     public int obtemComprimento () {
  24.         return this.comp;
  25.     }
  26.  
  27.     public void actualizarKm (int km) {
  28.         this.km += km;
  29.         if (proximaRevisao() - this.km <= 0) {
  30.             revisaoFeita = false;
  31.         }
  32.     }
  33.  
  34.     public boolean precisaRevisao() {
  35.         return !this.revisaoFeita;
  36.     }
  37.  
  38.     public int proximaRevisao() {
  39.         return this.proxRev;
  40.     }
  41.  
  42.     public void fazRevisao () {
  43.         this.revisaoFeita = true;
  44.         this.proxRev += KM;
  45.     }
  46.  
  47.     public int obtemNumeroRegisto() {
  48.         return this.numReg;
  49.     }
  50.  
  51.     public String toString () {
  52.         return "Carro " + this.numReg+": (" + this.km + " km, " + this.comp + " cm, "
  53.                 + "Revisao: " + (precisaRevisao() ? "em falta" : "ok") + ")";
  54.     }
  55. }
  56. import java.util.ArrayDeque;
  57. import java.util.Deque;
  58.  
  59. public class Pista{
  60.  
  61.     private int id;
  62.     private int comp;
  63.     private int ocupado;
  64.     private Deque<Carro> pilhaCarros = new ArrayDeque<Carro>();
  65.  
  66.     public Pista (int identificacao, int comprimentoGaragem) {
  67.         this.id = identificacao;
  68.         this.comp = comprimentoGaragem;
  69.         this.ocupado = 0;
  70.     }
  71.  
  72.     public int obtemComprimento () {
  73.         return this.comp;
  74.     }
  75.  
  76.     public int obtemComprimentoOcupado () {
  77.         return this.ocupado;
  78.     }
  79.    
  80.     private int obtemComprimentoLivre() {
  81.         return this.comp - this.ocupado;
  82.     }
  83.  
  84.     public int obtemIdentificacao() {
  85.         return this.id;
  86.     }
  87.  
  88.     public boolean estaVazia() {
  89.         return this.pilhaCarros.isEmpty();
  90.     }
  91.  
  92.     public Carro carroNoTopo() {
  93.         return pilhaCarros.peek();
  94.     }
  95.  
  96.     public Carro tiraCarro() {
  97.         this.ocupado -= carroNoTopo().obtemComprimento();
  98.         return pilhaCarros.pop();
  99.     }
  100.  
  101.     public boolean cabeNaPista(Carro carro) {
  102.         if (obtemComprimentoLivre() >= carro.obtemComprimento()) {
  103.             return true;
  104.         }
  105.         return false;
  106.     }
  107.  
  108.     public boolean estacionaCarro(Carro carro) {
  109.         if (cabeNaPista(carro)) {
  110.             this.ocupado += carro.obtemComprimento();
  111.             this.pilhaCarros.push(carro);
  112.             return true;
  113.         }
  114.         return false;
  115.     }
  116.  
  117.     public String toString () {
  118.         StringBuilder sb = new StringBuilder( "Pista " + id +": \n"
  119.                 + (obtemComprimento() - obtemComprimentoOcupado()) + " cm de espaco livre. " + "\n" + "[");
  120.        
  121.         Carro[] carros = new Carro[pilhaCarros.size()];
  122.        
  123.         for (int i = 0; i < carros.length; i++) {
  124.             carros[i] = pilhaCarros.pop();
  125.             sb.append(carros[i]);
  126.             if (i <= carros.length - 2) {
  127.                 sb.append(", ");
  128.             }
  129.         }
  130.          
  131.         for (int i = carros.length - 1; i >= 0; i--) {
  132.             pilhaCarros.push(carros[i]);
  133.         }
  134.         sb.append("]");
  135.         return sb.toString();
  136.     }
  137. }
  138. import java.util.Arrays;
  139.  
  140. public class Garagem{
  141.  
  142.     private static final int MAX_CARROS = 50;
  143.  
  144.     private int nCarros;
  145.     private boolean[] emServico;
  146.     private Pista[] pistas;
  147.     private Carro[] carrosEmServico;
  148.  
  149.     public Garagem (int numPistas, int comprimento) {
  150.         this.pistas = new Pista[numPistas];
  151.         for (int i = 0; i < numPistas; i++) {
  152.             pistas[i] = new Pista (i, comprimento);
  153.         }
  154.  
  155.         this.nCarros = 0;
  156.         this.emServico = new boolean[MAX_CARROS];
  157.         this.carrosEmServico = new Carro[MAX_CARROS];
  158.     }
  159.  
  160.     public void registaNovoCarro (int comprimento, int numKm) {
  161.         if (this.nCarros < MAX_CARROS) {
  162.             this.nCarros++;
  163.             this.pistas[pistaMaisVazia()].estacionaCarro(new Carro(this.nCarros, comprimento, numKm));
  164.         }
  165.     }
  166.  
  167.     private int pistaMaisVazia() {
  168.         int pista = 0;
  169.         for (int i = 1; i < this.pistas.length; i++) {
  170.             if (this.pistas[i].obtemComprimentoOcupado() < this.pistas[i-1].obtemComprimentoOcupado()) {
  171.                 pista = i;
  172.             }
  173.         }
  174.         return pista;
  175.     }
  176.  
  177.     private int pistaOcupada() {
  178.         int pista = 0;
  179.         while (this.pistas[pista].estaVazia() && pista < this.pistas.length) {
  180.             pista++;
  181.         }
  182.         return pista == this.pistas.length ? -1 : pista;
  183.     }
  184.  
  185.     public int obtemNumeroCarrosRegistados () {
  186.         return this.nCarros;
  187.     }
  188.  
  189.     private int numTopo(int p) {
  190.         return this.pistas[p].carroNoTopo().obtemNumeroRegisto();
  191.     }
  192.     private int kmTopo(int p) {
  193.         return this.pistas[p].carroNoTopo().obtemKm();
  194.     }
  195.  
  196.     public int escolhaPorNumRegisto() {
  197.         return  pistaOcupada() == -1 ? -1 : escolhaPorNum(pistaOcupada());
  198.     }
  199.  
  200.     private int escolhaPorNum(int p) {
  201.         for (int i = p; i < pistas.length - 1; i++ ) {
  202.             if (!this.pistas[i+1].estaVazia() && menorRegisto(i+1,p)) {
  203.                 p = i + 1;
  204.             }  
  205.         }
  206.         this.emServico[numTopo(p) - 1] = true;
  207.         //tentei mudar o indice do carrosEmSerivco para numTopo mas da erro, nao me lembro
  208.         //porque escolhi este mas so da assim
  209.         this.carrosEmServico[carrosEmServico().length - 1] = this.pistas[p].carroNoTopo() ;
  210.         return this.pistas[p].tiraCarro().obtemNumeroRegisto();  
  211.     }
  212.  
  213.     private boolean menorRegisto(int x, int y) {
  214.         return numTopo(x) < numTopo(y)? true : false;
  215.     }
  216.  
  217.     public int escolhaPorNumKm() {
  218.         return  pistaOcupada() == -1 ? -1 : escolhaPorKm( pistaOcupada());
  219.     }
  220.  
  221.     public int escolhaPorKm (int p) {
  222.         for (int i = p; i < pistas.length - 1; i++ ) {
  223.             if (!this.pistas[i+1].estaVazia() && menorKm(i+1,p)) {
  224.                 p = i + 1;
  225.             }  
  226.         }
  227.         this.emServico[numTopo(p) - 1] = true;
  228.         //tentei mudar o indice do carrosEmSerivco para numTopo mas da erro, nao me lembro
  229.         //porque escolhi este mas so da assim
  230.         this.carrosEmServico[carrosEmServico().length - 1] = this.pistas[p].carroNoTopo();
  231.         return this.pistas[p].tiraCarro().obtemNumeroRegisto();  
  232.     }
  233.  
  234.     private boolean menorKm(int x, int y) {
  235.         return kmTopo(x) < kmTopo(y)? true : false;
  236.     }
  237.  
  238.     public int[] carrosEmServico () {
  239.         int contador = 0;
  240.         for (int i = 0; i < this.nCarros; i++) {
  241.             if (this.emServico[i]) {
  242.                 contador++;
  243.             }
  244.         }
  245.         int[] servico = new int[contador];
  246.         int indice = 0;
  247.         for (int i = 0; i < this.nCarros; i++) {
  248.             if (this.emServico[i]) {
  249.                 servico[indice] = i + 1;
  250.                 indice++;
  251.             }
  252.         }
  253.         return servico;
  254.     }
  255.  
  256.     private static boolean pertenceArray(int[] v, int n) {
  257.         for (int i = 0; i < v.length; i++) {
  258.             if (v[i] == n) {
  259.                 return true;
  260.             }
  261.         }
  262.         return false;
  263.     }
  264.  
  265.     private Carro carroPorNumRegisto(Carro[] v, int n) {
  266.         for (int i = 0; i < v.length; i++) {
  267.             if (v[i].obtemNumeroRegisto() == n) {
  268.                 return v[i];
  269.             }
  270.         }
  271.         return null;
  272.     }
  273.  
  274.     public void estacionaCarro (int numRegisto, int quantosKmsFez) {
  275.         int[] carros = carrosEmServico();
  276.         if (numRegisto <= this.nCarros && pertenceArray(carros, numRegisto)) {
  277.             Carro carro = carroPorNumRegisto(this.carrosEmServico, numRegisto);
  278.             carro.actualizarKm(quantosKmsFez);
  279.             if (carro.precisaRevisao()) {
  280.                 carro.fazRevisao();
  281.             }
  282.             this.pistas[pistaMaisVazia()].estacionaCarro(carro);
  283.             carro = null;
  284.             this.emServico[numRegisto - 1] = false;
  285.         }
  286.     }
  287.  
  288.     public String toString() {
  289.         StringBuilder sb = new StringBuilder();
  290.         sb.append("A garagem tem " + this.pistas.length + " pistas com a seguinte configuracao:\n");
  291.         for (int i = 0; i < pistas.length; i++) {
  292.             sb.append(pistas[i] + "\n");
  293.         }
  294.         sb.append("estao em servico: " + carrosEmServico().length + " carros");
  295.         return sb.toString();
  296.     }
  297.  
  298.     public void reorganiza(){
  299.         int[] carrosEstacionados = new int[this.nCarros - carrosEmServico().length];
  300.         for (int i = 0; i < carrosEstacionados.length; i++) {
  301.             carrosEstacionados[i] = escolhaPorNumRegisto();
  302.         }
  303.         Arrays.sort(carrosEstacionados);
  304.         for (int i = 0; i < this.nCarros; i++) {
  305.             estacionaCarro(carrosEstacionados[i],0);
  306.         }
  307.     }
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement