Advertisement
Guest User

Untitled

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