domingo, 5 de junho de 2011

Programa: Quartel de Bombeiros

Problema: Quartel de Bombeiros

Linguagem: Java

Pais: Portugal

Cidade: Porto

Criador: Tiago Pereira dos Santos Silva (DCC-FCUP) & Fernando Silva (DCC-FCUP)

Código:




import java.util.*;


class FireFighter {

String name; // nome do bombeiro

int n_eventos; // numero de eventos em que participou

int workHours; // numero de horas que trabalhou em eventos


// construtor

FireFighter(String nameFF) {

name = nameFF;

n_eventos = 0;

workHours = 0;

}

// actualiza stats do bombeiro

public void updateFireFighterStats(int duration) {

n_eventos++;

workHours += duration;

}

}

//

// Caracterizacao de um Evento

//

class Event {

int id; // identificador do evento

int n_ff; // numero de bombeiros necessarios

int n_ff_assigned; // numero de bombeiros associados ao evento

int startTime; // hora de inicio do evento

int endTime; // hora a que termina o evento

FireFighter ffs[]; // bombeiros que participaram no evento


// constructor

Event(int idFF, int nFF, int start) {

id = idFF;

n_ff = nFF;

startTime = start;

n_ff_assigned = 0;

endTime = 0;

ffs = new FireFighter[n_ff]; // reserva espaco para todos bombeiros necessarios

}

public int durassao(){

return endTime-startTime;

}


public void endTime(int fimx){

endTime=fimx;

}

public void assignFireFighter(FireFighter ff) {

ff.updateFireFighterStats(durassao());

}

// escreve os bombeiros escalados por evento

public void printAssignedFireFighters() {

System.out.println("EVENTO "+id);

if(n_ff_assigned==0){

System.out.println("Nenhum");


}else{

for(int i=0;i

FireFighter ricardo =ffs[i];

if(ricardo!=null){

System.out.println(ricardo.name);

}else{

break;

}


}

}

}

}

//

// classe principal

//

class prob95_v0 {

// lista com os bombeiros lives -- criada vazia

public static List free_ff = new List();

// lista dos eventos -- criada vazia

public static List events = new List();


// procura sequencial pelo evento com identificador id

// assume-se que o evento existe sempre

public static Event findEvent(int eventID) {

// percorrer a lista de eventos ate encontrar o eventID

for (Event ev: events)

if (ev.id== eventID)

return ev;

// nunca devera acontecer sair com null

return null;

}

// main method

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int flag, nff;

String action;


int n_eventos=0;

flag = in.nextInt();

nff = in.nextInt();

in.nextLine();

for(int i=0;i

String nome = in.nextLine();

FireFighter eugenia =new FireFighter(nome);

free_ff.addLast(eugenia);

}

String ops = in.nextLine();

while(!ops.equals("FIM")){

Scanner tiago = new Scanner(ops);

action=tiago.next();

if(action.equals("PARTIDA")){

n_eventos++;

int id=tiago.nextInt();

int n_bomb=tiago.nextInt();

int hora=tiago.nextInt();

Event xv=new Event(id,n_bomb,hora);

for(int i=0;i

FireFighter radio = free_ff.removeFirst();

if(radio!=null){

xv.ffs[i]=radio;

xv.n_ff_assigned++;

}


}

events.addLast(xv);

}else if(action.equals("CHEGADA")){

int idx = tiago.nextInt();

int fimx = tiago.nextInt();

Event evented = findEvent(idx);

evented.endTime(fimx);

for(int i =0;i

FireFighter cruel = evented.ffs[i];

if(cruel!=null){

free_ff.addLast(cruel);

evented.assignFireFighter(cruel);

}else{

break;

}



}

}






ops=in.nextLine();


}


// outputs de acordo com a flag

if (flag==1) {

System.out.printf("Ocorreram %d eventos\n",n_eventos);

}

else if (flag==2) {

System.out.println("Bombeiros Destacados");

while(!events.isEmpty()){

Event filipe=events.removeFirst();

if(filipe!=null){

filipe.printAssignedFireFighters();

}



}

}

else {

System.out.println("Listagem de Bombeiros");

while(!free_ff.isEmpty()){

FireFighter carlos= free_ff.removeFirst();

if(carlos!=null){

System.out.println(carlos.name +" "+carlos.n_eventos+" "+carlos.workHours);

}else{

break;

}

}

}

}

}

class List implements Iterable {

private int size;

private Node first;

private Node last;


public boolean isEmpty() {return size == 0;}

public int size() {return size;}


// construtor de lista vazia

List() {

size = 0;

first = last = null;

}

// remove primeiro elemento da lista

public E removeFirst() {

E rr=null;

if(!isEmpty()){

rr=first.val;

first=first.next;

size--;

if(first==null){

last=null;

}

}

return rr;

}

// adiciona no fim da lista

public void addLast(E v) {

Node novo = new Node(v,null);

if(isEmpty()){

last=first=novo;

}

last.next=novo;

last=novo;

size++;

}

// um no da lista

private class Node {

E val;

Node next;


Node(E v, Node n) {

val = v;

next = n;

}

}

// metodo iterador, devolve um objecto ListIterator

public Iterator iterator() {

return new ListIterator();

}

// definicao de iteradores sobre os elementos da lista

private class ListIterator implements Iterator {

Node current = first;


public boolean hasNext() {return current != null;}

public void remove() {

throw new UnsupportedOperationException();

}

public E next() {

if (!hasNext()) throw new NoSuchElementException();

E v = current.val;

current = current.next;

return v;

}

}

}








Sem comentários:

Enviar um comentário