Filip_Markoski

4.Lab4.3 MVR (Solved)

Nov 5th, 2017
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.88 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3. import java.util.stream.IntStream;
  4.  
  5. class ArrayQueue<E> {
  6.     E[] elems;
  7.     int length, front, rear;
  8.  
  9.     @SuppressWarnings("unchecked")
  10.     public ArrayQueue(int maxlength) {
  11.         elems = (E[]) new Object[maxlength];
  12.         clear();
  13.     }
  14.  
  15.     public boolean isEmpty() {
  16.         return (length == 0);
  17.     }
  18.  
  19.     public int size() {
  20.         return length;
  21.     }
  22.  
  23.     public E peek() {
  24.         if (length > 0)
  25.             return elems[front];
  26.         else {
  27.             System.out.println("Queue is empty");
  28.             return null;
  29.         }
  30.     }
  31.  
  32.     public void clear() {
  33.         length = 0;
  34.         front = rear = 0;
  35.     }
  36.  
  37.     public void enqueue(E x) {
  38.         elems[rear++] = x;
  39.         if (rear == elems.length) rear = 0;
  40.         length++;
  41.     }
  42.  
  43.     public E dequeue() {
  44.         if (length > 0) {
  45.             E frontmost = elems[front];
  46.             elems[front++] = null;
  47.             if (front == elems.length) front = 0;
  48.             length--;
  49.             return frontmost;
  50.         } else {
  51.             System.out.println("Queue is empty");
  52.             return null;
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public String toString() {
  58.         StringBuffer sb = new StringBuffer();
  59.         IntStream.range(0, size()).forEach(i -> sb.append(elems[i]));
  60.         sb.setLength(sb.length() - 1);
  61.         return sb.toString();
  62.     }
  63. }
  64.  
  65. class Citizen {
  66.     String imePrezime;
  67.     int lKarta, pasos, vozacka;
  68.  
  69.     public Citizen(String imePrezime, int lKarta, int pasos, int vozacka) {
  70.         super();
  71.         this.imePrezime = imePrezime;
  72.         this.lKarta = lKarta;
  73.         this.pasos = pasos;
  74.         this.vozacka = vozacka;
  75.     }
  76.  
  77.     @Override
  78.     public String toString() {
  79.         return imePrezime + "\n";
  80.     }
  81. }
  82.  
  83. public class MVR {
  84.  
  85.     /* ID then Passport then Driving license */
  86.     /* Each of them has its own queue */
  87.     /* if multiple documents, apply first for ID, then passport than driving */
  88.     public static void otvoriShalter(ArrayQueue<Citizen> redicaLkarti, ArrayQueue<Citizen> redicaPasos, ArrayQueue<Citizen> redicaVozacka) {
  89.         while (!redicaLkarti.isEmpty()){
  90.             Citizen temp = redicaLkarti.dequeue();
  91.             if (temp.pasos == 0 && temp.vozacka == 0){
  92.                 System.out.print(temp);
  93.             } else if(temp.pasos == 1) {
  94.                 redicaPasos.enqueue(temp);
  95.             } else {
  96.                 redicaVozacka.enqueue(temp);
  97.             }
  98.         }
  99.  
  100.         while (!redicaPasos.isEmpty()){
  101.             Citizen temp = redicaPasos.dequeue();
  102.             if (temp.vozacka == 0){
  103.                 System.out.print(temp);
  104.             } else {
  105.                 redicaVozacka.enqueue(temp);
  106.             }
  107.         }
  108.  
  109.         while (!redicaVozacka.isEmpty()){
  110.             System.out.print(redicaVozacka.dequeue());
  111.         }
  112.  
  113.     }
  114.  
  115.     public static void main(String[] args) {
  116.  
  117.         Scanner br = new Scanner(System.in);
  118.         ArrayQueue<Citizen> redicaLkarti = new ArrayQueue<Citizen>(10);
  119.         ArrayQueue<Citizen> redicaPasos = new ArrayQueue<Citizen>(10);
  120.         ArrayQueue<Citizen> redicaVozacka = new ArrayQueue<Citizen>(10);
  121.  
  122.         int N = Integer.parseInt(br.nextLine());
  123.         for (int i = 1; i <= N; i++) {
  124.             String imePrezime = br.nextLine();
  125.             int lKarta = Integer.parseInt(br.nextLine());
  126.             int pasos = Integer.parseInt(br.nextLine());
  127.             int vozacka = Integer.parseInt(br.nextLine());
  128.             Citizen covek = new Citizen(imePrezime, lKarta, pasos, vozacka);
  129.             if (lKarta == 1) {
  130.                 redicaLkarti.enqueue(covek);
  131.             } else if (pasos == 1)
  132.                 redicaPasos.enqueue(covek);
  133.             else
  134.                 redicaVozacka.enqueue(covek);
  135.         }
  136.         otvoriShalter(redicaLkarti, redicaPasos, redicaVozacka);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment