SHARE
TWEET

Untitled

a guest Nov 22nd, 2019 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.time.*;
  7.  
  8. class Book{
  9.     String title;
  10.     String author;
  11.     int year;
  12.    
  13.     // constructor with arguments
  14.     public Book(String title, String author, int year){
  15.         this.title = title;
  16.         this.author = author;
  17.         this.year = year;
  18.     }
  19.    
  20.     // toString method used for printout the object
  21.     public String toString(){
  22.         return "Title: \"" + title + "\", author: " + author + ", (" + year + ")";
  23.     }
  24.  
  25.     // getters
  26.     public String  getTitle(){
  27.         return title;
  28.     }
  29.    
  30.     public String getAuthor(){
  31.         return author;
  32.     }
  33.    
  34.     public int getYear(){
  35.         return year;
  36.     }
  37. }
  38.  
  39. class Order{
  40.     Book book;
  41.     LocalDate dateOfOrder;
  42.    
  43.     // constructor with arguments
  44.     public Order(Book book, int year, int month, int day){
  45.         this.book = book;
  46.         this.dateOfOrder = LocalDate.of(year, month, day);
  47.     }
  48.    
  49.     // method used to printout the order
  50.     public String toString(){
  51.         return "Order created: " + dateOfOrder + "\n" +
  52.                " ordered book: " + book;
  53.        
  54.     }
  55.  
  56.     //getters
  57.     public Book getBook(){
  58.         return book;
  59.     }
  60.    
  61.     public LocalDate getDateOfOrder(){
  62.         return dateOfOrder;
  63.     }
  64. }
  65.  
  66. /* Name of the class has to be "Main" only if the class is public. */
  67. class OrdersQueueExample
  68. {
  69.     public static void main (String[] args) throws java.lang.Exception
  70.     {
  71.         // Create 3 sample books
  72.         Book theBook1 = new Book("Assassins' Creed the Book", "William Shakespeare", 2016);
  73.         Book theBook2 = new Book("Book of jungle", "Rudyard Kipling", 1894);
  74.         Book theBook3 = new Book("50 shades of gray - lexicon for graphicians", "Dante Alighieri", 1572);
  75.  
  76.         // Create 5 sample orders
  77.         Order theOrder1 = new Order(theBook1, 2017, 6, 28);
  78.         Order theOrder2 = new Order(theBook2, 2017, 6, 28);
  79.         Order theOrder3 = new Order(theBook1, 2017, 6, 29);
  80.         Order theOrder4 = new Order(theBook2, 2017, 6, 30);
  81.         Order theOrder5 = new Order(theBook3, 2017, 6, 30);
  82.  
  83.         // Create the FIFO queue
  84.         ArrayDeque<Order> theOrders = new ArrayDeque<Order>();
  85.  
  86.         // Check size of the queue
  87.         System.out.println("Queue is created. It's size: " + theOrders.size());
  88.  
  89.         // Put the orders into the queue
  90.         theOrders.offer(theOrder1);
  91.         theOrders.offer(theOrder2);
  92.         theOrders.offer(theOrder3);
  93.         theOrders.offer(theOrder4);
  94.         theOrders.offer(theOrder5);
  95.  
  96.         // Check size of the queue
  97.         System.out.println("Queue is filled. It's size: " + theOrders.size());
  98.         System.out.println();
  99.        
  100.         // Examine the order at beginning of the queue
  101.         System.out.println("First order in the queue is: \n" + theOrders.peek());
  102.         System.out.println();
  103.  
  104.         // Get objects from queue
  105.         Order temporaryOrder;
  106.         temporaryOrder = theOrders.poll();
  107.  
  108.         // here you can do something with order taken from the queue
  109.         temporaryOrder = theOrders.poll();
  110.         temporaryOrder = theOrders.poll();
  111.         temporaryOrder = theOrders.poll();
  112.         temporaryOrder = theOrders.poll();
  113.  
  114.         // Check size of the queue
  115.         System.out.println("Queue is emptied. It's size: " + theOrders.size());
  116.         System.out.println();
  117.  
  118.         // Check what was taken as last
  119.         System.out.println("Last order taken from queue was:\n" + temporaryOrder);
  120.         System.out.println();
  121.     }
  122. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top