Tuesday, 18 November 2014

Lab 5 cont class Queue

class Data {
  String name;
  int priority;
  Data(String name, int priority) {
    this.name = name;
    this.priority = priority;
  }
  String toString() {
    String print = "name#"+name+" priority#"+priority;
    return print;
  }
}
class Queue {
  Data[] wait = new Data[1];
  int limit;
  Queue(int limit) {
    this.limit = limit;
    this.wait = new Data[limit];
  }
  void show_queue() {
    for (int i = 0; i < wait.length; i++) {
      if (wait[i] != null) {
        println(wait[i]);
      }
    }
  }
  void add(Data in) {
    for (int i = 0; i < wait.length; i++) {
      if ( wait[i] == null ) {
        wait[i] = in;
        i = wait.length;
      }
    }
  }
  void remove() {
    int high = 0;
    for (int i = 0; i < wait.length; i++) {
      if (wait[i] != null) {
        if (wait[high].priority < wait[i].priority) {
          high = i;
        }
      }
    }
    wait[high] = null;
  }
}
Data human;
Data human2;
Queue hos = new Queue(50);
void setup() {
  human = new Data("Somsak", 300);
  human2 = new Data("Prasert", 60);
  hos.add(human);
  hos.add(human2);
  hos.remove();
  hos.show_queue();
}

No comments:

Post a Comment