Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Carro {
- private static final int KM = 20000;
- private int numReg;
- private int comp;
- private int km;
- private int proxRev;
- private boolean revisaoFeita;
- public Carro (int numRegisto, int comprimento, int km) {
- this.numReg = numRegisto;
- this.comp = comprimento;
- this.km = km;
- this.proxRev = this.km + KM;
- this.revisaoFeita = true;
- }
- public int obtemKm () {
- return this.km;
- }
- public int obtemComprimento () {
- return this.comp;
- }
- public void actualizarKm (int km) {
- this.km += km;
- if (proximaRevisao() - this.km <= 0) {
- revisaoFeita = false;
- }
- }
- public boolean precisaRevisao() {
- return !this.revisaoFeita;
- }
- public int proximaRevisao() {
- return this.proxRev;
- }
- public void fazRevisao () {
- this.revisaoFeita = true;
- this.proxRev += KM;
- }
- public int obtemNumeroRegisto() {
- return this.numReg;
- }
- public String toString () {
- return "Carro " + this.numReg+": (" + this.km + " km, " + this.comp + " cm, "
- + "Revisao: " + (precisaRevisao() ? "em falta" : "ok") + ")";
- }
- }
- import java.util.ArrayDeque;
- import java.util.Deque;
- public class Pista{
- private int id;
- private int comp;
- private int ocupado;
- private Deque<Carro> pilhaCarros = new ArrayDeque<Carro>();
- public Pista (int identificacao, int comprimentoGaragem) {
- this.id = identificacao;
- this.comp = comprimentoGaragem;
- this.ocupado = 0;
- }
- public int obtemComprimento () {
- return this.comp;
- }
- public int obtemComprimentoOcupado () {
- return this.ocupado;
- }
- private int obtemComprimentoLivre() {
- return this.comp - this.ocupado;
- }
- public int obtemIdentificacao() {
- return this.id;
- }
- public boolean estaVazia() {
- return this.pilhaCarros.isEmpty();
- }
- public Carro carroNoTopo() {
- return pilhaCarros.peek();
- }
- public Carro tiraCarro() {
- this.ocupado -= carroNoTopo().obtemComprimento();
- return pilhaCarros.pop();
- }
- public boolean cabeNaPista(Carro carro) {
- if (obtemComprimentoLivre() >= carro.obtemComprimento()) {
- return true;
- }
- return false;
- }
- public boolean estacionaCarro(Carro carro) {
- if (cabeNaPista(carro)) {
- this.ocupado += carro.obtemComprimento();
- this.pilhaCarros.push(carro);
- return true;
- }
- return false;
- }
- public String toString () {
- StringBuilder sb = new StringBuilder( "Pista " + id +": \n"
- + (obtemComprimento() - obtemComprimentoOcupado()) + " cm de espaco livre. " + "\n" + "[");
- Carro[] carros = new Carro[pilhaCarros.size()];
- for (int i = 0; i < carros.length; i++) {
- carros[i] = pilhaCarros.pop();
- sb.append(carros[i]);
- if (i <= carros.length - 2) {
- sb.append(", ");
- }
- }
- for (int i = carros.length - 1; i >= 0; i--) {
- pilhaCarros.push(carros[i]);
- }
- sb.append("]");
- return sb.toString();
- }
- }
- import java.util.Arrays;
- public class Garagem{
- private static final int MAX_CARROS = 50;
- private int nCarros;
- private boolean[] emServico;
- private Pista[] pistas;
- private Carro[] carrosEmServico;
- public Garagem (int numPistas, int comprimento) {
- this.pistas = new Pista[numPistas];
- for (int i = 0; i < numPistas; i++) {
- pistas[i] = new Pista (i, comprimento);
- }
- this.nCarros = 0;
- this.emServico = new boolean[MAX_CARROS];
- this.carrosEmServico = new Carro[MAX_CARROS];
- }
- public void registaNovoCarro (int comprimento, int numKm) {
- if (this.nCarros < MAX_CARROS) {
- this.nCarros++;
- this.pistas[pistaMaisVazia()].estacionaCarro(new Carro(this.nCarros, comprimento, numKm));
- }
- }
- private int pistaMaisVazia() {
- int pista = 0;
- for (int i = 1; i < this.pistas.length; i++) {
- if (this.pistas[i].obtemComprimentoOcupado() < this.pistas[i-1].obtemComprimentoOcupado()) {
- pista = i;
- }
- }
- return pista;
- }
- private int pistaOcupada() {
- int pista = 0;
- while (this.pistas[pista].estaVazia() && pista < this.pistas.length) {
- pista++;
- }
- return pista == this.pistas.length ? -1 : pista;
- }
- public int obtemNumeroCarrosRegistados () {
- return this.nCarros;
- }
- private int numTopo(int p) {
- return this.pistas[p].carroNoTopo().obtemNumeroRegisto();
- }
- private int kmTopo(int p) {
- return this.pistas[p].carroNoTopo().obtemKm();
- }
- public int escolhaPorNumRegisto() {
- return pistaOcupada() == -1 ? -1 : escolhaPorNum(pistaOcupada());
- }
- private int escolhaPorNum(int p) {
- for (int i = p; i < pistas.length - 1; i++ ) {
- if (!this.pistas[i+1].estaVazia() && menorRegisto(i+1,p)) {
- p = i + 1;
- }
- }
- this.emServico[numTopo(p) - 1] = true;
- //tentei mudar o indice do carrosEmSerivco para numTopo mas da erro, nao me lembro
- //porque escolhi este mas so da assim
- this.carrosEmServico[carrosEmServico().length - 1] = this.pistas[p].carroNoTopo() ;
- return this.pistas[p].tiraCarro().obtemNumeroRegisto();
- }
- private boolean menorRegisto(int x, int y) {
- return numTopo(x) < numTopo(y)? true : false;
- }
- public int escolhaPorNumKm() {
- return pistaOcupada() == -1 ? -1 : escolhaPorKm( pistaOcupada());
- }
- public int escolhaPorKm (int p) {
- for (int i = p; i < pistas.length - 1; i++ ) {
- if (!this.pistas[i+1].estaVazia() && menorKm(i+1,p)) {
- p = i + 1;
- }
- }
- this.emServico[numTopo(p) - 1] = true;
- //tentei mudar o indice do carrosEmSerivco para numTopo mas da erro, nao me lembro
- //porque escolhi este mas so da assim
- this.carrosEmServico[carrosEmServico().length - 1] = this.pistas[p].carroNoTopo();
- return this.pistas[p].tiraCarro().obtemNumeroRegisto();
- }
- private boolean menorKm(int x, int y) {
- return kmTopo(x) < kmTopo(y)? true : false;
- }
- public int[] carrosEmServico () {
- int contador = 0;
- for (int i = 0; i < this.nCarros; i++) {
- if (this.emServico[i]) {
- contador++;
- }
- }
- int[] servico = new int[contador];
- int indice = 0;
- for (int i = 0; i < this.nCarros; i++) {
- if (this.emServico[i]) {
- servico[indice] = i + 1;
- indice++;
- }
- }
- return servico;
- }
- private static boolean pertenceArray(int[] v, int n) {
- for (int i = 0; i < v.length; i++) {
- if (v[i] == n) {
- return true;
- }
- }
- return false;
- }
- private Carro carroPorNumRegisto(Carro[] v, int n) {
- for (int i = 0; i < v.length; i++) {
- if (v[i].obtemNumeroRegisto() == n) {
- return v[i];
- }
- }
- return null;
- }
- public void estacionaCarro (int numRegisto, int quantosKmsFez) {
- int[] carros = carrosEmServico();
- if (numRegisto <= this.nCarros && pertenceArray(carros, numRegisto)) {
- Carro carro = carroPorNumRegisto(this.carrosEmServico, numRegisto);
- carro.actualizarKm(quantosKmsFez);
- if (carro.precisaRevisao()) {
- carro.fazRevisao();
- }
- this.pistas[pistaMaisVazia()].estacionaCarro(carro);
- carro = null;
- this.emServico[numRegisto - 1] = false;
- }
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("A garagem tem " + this.pistas.length + " pistas com a seguinte configuracao:\n");
- for (int i = 0; i < pistas.length; i++) {
- sb.append(pistas[i] + "\n");
- }
- sb.append("estao em servico: " + carrosEmServico().length + " carros");
- return sb.toString();
- }
- public void reorganiza(){
- int[] carrosEstacionados = new int[this.nCarros - carrosEmServico().length];
- for (int i = 0; i < carrosEstacionados.length; i++) {
- carrosEstacionados[i] = escolhaPorNumRegisto();
- }
- Arrays.sort(carrosEstacionados);
- for (int i = 0; i < this.nCarros; i++) {
- estacionaCarro(carrosEstacionados[i],0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement