SHARE
TWEET

Untitled

a guest Nov 19th, 2019 102 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. /**
  5.  * This class represents a Ticket that stores the details of a Ticket
  6.  */
  7. class Ticket {
  8.     // Do NOT modify Ticket class
  9.     private int id;
  10.     private String studentName;
  11.     private String description;
  12.  
  13.     /**
  14.      * Constructor for Ticket class
  15.      * @param id Ticket's id
  16.      * @param studentName Name of student who submitted ticket
  17.      * @param description Describes the type of help the student needs
  18.      */
  19.     public Ticket(int id, String studentName, String description) {
  20.         this.id = id;
  21.         this.studentName = studentName;
  22.         this.setDescription(description);
  23.     }
  24.  
  25.     /**
  26.      * Checks if two tickets are the same.
  27.      * @param Obj Object that we will be compared with the calling ticket object
  28.      * @return True if the two tickets are the same, False otherwise
  29.      */
  30.     public boolean equals(Object obj) {
  31.         if (!(obj instanceof Ticket))
  32.             return false;
  33.         if (((Ticket) obj).id == this.id
  34.                 && ((Ticket) obj).studentName.equals(this.studentName)
  35.                 && ((Ticket) obj).getDescription()
  36.                         .equals(this.getDescription())) {
  37.             return true;
  38.         } else {
  39.             return false;
  40.         }
  41.     }
  42.  
  43.     /**
  44.      * Get description of ticket object.
  45.      * @return description (instance variable).
  46.      */
  47.     public String getDescription() {
  48.         return description;
  49.     }
  50.  
  51.     /**
  52.      * Set description of ticket object
  53.      * @param description The description of the problem.
  54.      */
  55.     public void setDescription(String description) {
  56.         this.description = description;
  57.     }
  58.  
  59.     /**
  60.      * Print the contents of the ticket
  61.      * @return a string that contains the ticket id, studentName and description
  62.      */
  63.     @Override
  64.     public String toString() {
  65.         return id + ": " + studentName + " (" + description + ")";
  66.     }
  67. }
  68.  
  69. // NOTE: For all the classes below, you should write the class and methods
  70. // headers as we have written for the Ticket class above. Refer to our CSE 11
  71. // style guide that is linked from the PA8 specifications.
  72.  
  73. class Node {
  74.     // TODO #1: Add instance variables below
  75.     private Ticket data;
  76.     private Node next;
  77.  
  78.     // TODO #2: Implement the Node constructor below
  79.     public Node(Ticket data) throws NullPointerException{
  80.         if (data==null) {
  81.             throw new NullPointerException();
  82.         }
  83.         else {
  84.             this.data = data;
  85.         }
  86.     }
  87.  
  88.     // TODO #3: Implement the getters and setters below
  89.     public Node getNext() {
  90.         return next;
  91.     }
  92.  
  93.     public void setNext(Node next) {
  94.         this.next = next;
  95.     }
  96.  
  97.     public Ticket getData() {
  98.         return data;
  99.     }
  100.  
  101.     public void setData(Ticket data) {
  102.         this.data = data;
  103.     }
  104.  
  105.     @Override
  106.     public String toString() {
  107.         return "| " + data + " | "; //copy & paste this line into your code.
  108.     }
  109. }
  110.  
  111. class CustomLL {
  112.     // TODO #4: Add instance variables below
  113.     private Node head;
  114.     private Node tail;
  115.     private int size;
  116.     // TODO #5: Implement the CustomLL constructor below
  117.     public CustomLL() {
  118.     }
  119.  
  120.     // TODO #6: Implement the CustomLL constructor below
  121.     public CustomLL(List<Ticket> list) {
  122.         if (list == null) {
  123.             throw new NullPointerException();
  124.         }
  125.         for(int i = 0; i<list.size(); i++) {
  126.             try {
  127.                 addLast(list.get(i));
  128.                 }
  129.             catch(NullPointerException e) {
  130.                 Ticket x = new Ticket(-1, null, null);
  131.                 addLast(x);
  132.             }
  133.         }
  134.     }
  135.     // TODO #8: Implement the size method below
  136.     public int size() {
  137.         return this.size;
  138.     }
  139.  
  140.     // TODO #9: Implement the getters and setters below
  141.     public Node getHead() {
  142.         return this.head;
  143.     }
  144.  
  145.     public void setHead(Node head) {
  146.         this.head = head;
  147.     }
  148.  
  149.     public Node getTail() {
  150.         return this.tail;
  151.     }
  152.  
  153.     public void setTail(Node tail) {
  154.         this.tail = tail;
  155.     }
  156.  
  157.     // TODO #10: Implement the addFirst method below
  158.     public void addFirst(Ticket toAdd) {
  159.         Node add = new Node(toAdd);
  160.         add.setNext(head);
  161.         setHead(add);
  162.         if (tail == null) {
  163.             tail = head;
  164.         }
  165.         size++;
  166.     }
  167.  
  168.     // TODO #11: Implement the addLast method below
  169.     public void addLast(Ticket toAdd) {
  170.         Node last = new Node(toAdd);
  171.         if(head == null) {
  172.             head = last;
  173.             tail = last;
  174.         }
  175.         else {
  176.             tail.setNext(last);
  177.             tail = last;
  178.         }
  179.         size++;
  180.     }
  181.  
  182.     // TODO #12: Implement the remove method below
  183.     public boolean remove(Ticket toRemove) throws NullPointerException {
  184.         if(toRemove == null) {
  185.             throw new NullPointerException();
  186.         }
  187.         else {
  188.         return true;
  189.         }
  190.     }
  191.  
  192.     // TODO #13: Implement the remove method below
  193.     public Ticket remove(int index) throws IndexOutOfBoundsException {
  194.         if (index >= size || 0 > index) {
  195.             throw new IndexOutOfBoundsException();
  196.         }
  197.         size--;
  198.         remove(index);
  199.         return ();
  200.     }
  201.  
  202.     @Override
  203.     public String toString() {
  204.         // Do NOT change this method!
  205.         if (head == null) {
  206.             return "Linked List is empty!";
  207.         }
  208.         Node curr = head;
  209.         String str = "";
  210.         while (curr != null) {
  211.             if (curr == head)
  212.                 str = curr.toString();
  213.             else
  214.                 str = str + "-> " + curr;
  215.             curr = curr.getNext();
  216.         }
  217.         return str;
  218.     }
  219. }
  220.  
  221. // TODO #14: implement LockedQueueException class
  222. class LockedQueueException extends Exception {
  223.  
  224.     public LockedQueueException(String s) {
  225.         super(s);
  226.     }
  227. }
  228. // TODO #15: implement FullQueueException class
  229. class FullQueueException extends Exception {
  230.     public FullQueueException(String s){
  231.         super (s);
  232.     }
  233. }
  234.  
  235. class LabQueue {
  236.     // TODO #16: Add instance variables below
  237.     private CustomLL tickets;
  238.     private int capacity;
  239.     private boolean locked;
  240.  
  241.     // TODO #17: Implement the LabQueue constructor below
  242.     public LabQueue(int capacity) {
  243.         this.tickets = new CustomLL();
  244.         this.locked = true;
  245.         this.capacity = capacity;
  246.     }
  247.  
  248.     // TODO #18: Implement getCapacity method below
  249.     public int getCapacity() {
  250.         return this.capacity;
  251.     }
  252.  
  253.     // TODO #19: Implement isLocked method below
  254.     public boolean isLocked() {
  255.         return this.locked;
  256.     }
  257.  
  258.     // TODO #20: Implement getTickets method below
  259.     public CustomLL getTickets() {
  260.         return tickets;
  261.     }
  262.  
  263.     // TODO #21: Implement unlockQueue method below
  264.     public void unlockQueue() {
  265.         this.locked = false;
  266.     }
  267.  
  268.     // TODO #22: Implement lockQueue method below
  269.     public void lockQueue() {
  270.         this.locked = true;
  271.     }
  272.  
  273.     // TODO #23: Implement submitTicket method below
  274.     public boolean submitTicket(Ticket t)
  275.             throws LockedQueueException, FullQueueException {
  276.         if (this.locked == true) {
  277.             throw new LockedQueueException("Cannot submit a ticket to a locked queue");
  278.         }
  279.         if (capacity>tickets.size()) {
  280.             throw new FullQueueException("Cannot submit a ticket to a queue that is at capacity");
  281.         }
  282.         try {
  283.             tickets.addLast(t);
  284.             return true;
  285.         }
  286.         catch (NullPointerException e) {
  287.             System.out.println("Cannot submit a ticket with null value");
  288.             return false;
  289.         }
  290.     }
  291.  
  292.     // TODO #24: Implement deleteTicket method below
  293.     public boolean deleteTicket(int i) {
  294.         try {
  295.             tickets.remove(i);
  296.             return true;
  297.         }
  298.         catch (IndexOutOfBoundsException e) {
  299.             System.out.println("Cannot remove a ticket at index <i> in LabQueue of size <size>");
  300.             return false;
  301.         }
  302.     }
  303.  
  304. }
  305.  
  306. public class PA8 {
  307.     public static void main(String[] args) {
  308.         System.out.println("\n***********Testing Ticket class***********");
  309.         Ticket t1 = new Ticket(1, "Jenny", "PA8 help");
  310.         System.out.println(t1);
  311.         Ticket t2 = new Ticket(2, "Matthew", "Help with exceptions!");
  312.         System.out.println(t2);
  313.         Ticket t3 = new Ticket(3, "Cade", "Junit help");
  314.         System.out.println(t3);
  315.  
  316.         System.out.println("\n***********Testing Node class***********");
  317.         Node n1 = new Node(t1);
  318.         System.out.println(n1);
  319.         Node n2 = new Node(t2);
  320.         System.out.println(n2);
  321.         Node n3 = new Node(t3);
  322.         System.out.println(n3);
  323.  
  324.  
  325.  
  326.         System.out.println("\n***********Testing CustomLL() & addFirst()***********");
  327.         CustomLL myList1 = new CustomLL();
  328.         myList1.addFirst(t1);
  329.         myList1.addFirst(t2);
  330.         myList1.addFirst(t3);
  331.         System.out.println(myList1);
  332.         System.out.println("head = " + myList1.getHead());
  333.         System.out.println("tail = " + myList1.getTail());
  334.         System.out.println("size = " + myList1.size());
  335.  
  336.  
  337.  
  338.         System.out.println("\n***********Testing overloaded CustomLL constructor***********");
  339.         List<Ticket> myTickets = new ArrayList<>();
  340.         myTickets.add(t1);
  341.         myTickets.add(t2);
  342.         myTickets.add(t3);
  343.         CustomLL myList2 = new CustomLL(myTickets);
  344.         System.out.println(myList2);
  345.         System.out.println("head = " + myList2.getHead());
  346.         System.out.println("tail = " + myList2.getTail());
  347.         System.out.println("size = " + myList2.size());
  348.  
  349.  
  350.  
  351.         System.out.println("\n***********Testing addLast() method***********");
  352.         CustomLL myList3 = new CustomLL();
  353.         myList3.addLast(t2);
  354.         myList3.addLast(t1);
  355.         myList3.addLast(t3);
  356.         System.out.println(myList3);
  357.         System.out.println("head = " + myList3.getHead());
  358.         System.out.println("tail = " + myList3.getTail());
  359.         System.out.println("size = " + myList3.size());
  360.  
  361.  
  362.  
  363.         System.out.println("\n***********Testing CustomLL remove by object***********");
  364.         boolean isRemoved = myList3.remove(t2);
  365.         System.out.println("isRemoved = " + isRemoved);
  366.         System.out.println(myList3);
  367.         isRemoved = myList3.remove(t2);
  368.         System.out.println("isRemoved = " + isRemoved);
  369.         System.out.println(myList3);
  370.         isRemoved = myList3.remove(t3);
  371.         System.out.println("isRemoved = " + isRemoved);
  372.         System.out.println(myList3);
  373.         isRemoved = myList3.remove(t1);
  374.         System.out.println("isRemoved = " + isRemoved);
  375.         System.out.println(myList3);
  376.  
  377. //
  378. //
  379. //        System.out.println("\n***********Testing CustomLL remove by index***********");
  380. //        System.out.println(myList2);
  381. //        Ticket removed = myList2.remove(1);
  382. //        System.out.println("Ticket removed = " + removed);
  383. //        System.out.println(myList2);
  384. //        removed = myList2.remove(0);
  385. //        System.out.println("Ticket removed = " + removed);
  386. //        System.out.println(myList2);
  387. //        removed = myList2.remove(0);
  388. //        System.out.println("Ticket removed = " + removed);
  389. //        System.out.println(myList2);
  390. //
  391. //
  392. //
  393. //        System.out.println("\n***********Testing Exception classes***********");
  394. //        Exception lqe = new LockedQueueException("The queue is locked");
  395. //        System.out.println(lqe.getMessage());
  396. //        Exception fqe = new FullQueueException("The queue is full");
  397. //        System.out.println(fqe.getMessage());
  398. //
  399. //
  400. //
  401. //        System.out.println("\n***********Testing LabQueue***********");
  402. //        LabQueue labQ = new LabQueue(3);
  403. //        System.out.println("Capacity is: " + labQ.getCapacity());
  404. //        try {
  405. //            labQ.submitTicket(t1);
  406. //        } catch(LockedQueueException e) {
  407. //            System.out.println(e.getMessage());
  408. //        } catch(FullQueueException e) {
  409. //            System.out.println(e.getMessage());
  410. //        }
  411. //        System.out.println("Is queue locked?: " + labQ.isLocked());
  412. //        System.out.println("Unlocking the queue...");
  413. //        labQ.unlockQueue();
  414. //        System.out.println("Is queue locked?: " + labQ.isLocked());
  415. //        try {
  416. //            labQ.submitTicket(t1);
  417. //        } catch(Exception e) {
  418. //            System.out.println(e.getMessage());
  419. //        }
  420. //        try{
  421. //            labQ.submitTicket(t2);
  422. //        } catch(Exception e) {
  423. //            System.out.println(e.getMessage());
  424. //        }
  425. //        try{
  426. //            labQ.submitTicket(t3);
  427. //        } catch(Exception e) {
  428. //            System.out.println(e.getMessage());
  429. //        }
  430. //        System.out.println("Tickets in queue: " + labQ.getTickets());
  431. //        Ticket t4 = new Ticket(4, "Deva", "Help with LabQueue class");
  432. //        try{
  433. //            labQ.submitTicket(t4);
  434. //        } catch(Exception e) {
  435. //            System.out.println(e.getMessage());
  436. //        }
  437. //        labQ.deleteTicket(1);
  438. //        System.out.println("Tickets in queue: " + labQ.getTickets());
  439. //        try{
  440. //            labQ.submitTicket(t4);
  441. //        } catch(Exception e) {
  442. //            System.out.println(e.getMessage());
  443. //        }
  444. //        System.out.println("Tickets in queue: " + labQ.getTickets());
  445. //        labQ.deleteTicket(0);
  446. //        System.out.println("Tickets in queue: " + labQ.getTickets());
  447. //        try{
  448. //            labQ.submitTicket(null);
  449. //        } catch(Exception e) {
  450. //            System.out.println(e.getMessage());
  451. //        }
  452. //
  453. //        System.out.println();
  454.      }
  455. }
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