Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.List;
- /**
- * This class represents a Ticket that stores the details of a Ticket
- */
- class Ticket {
- // Do NOT modify Ticket class
- private int id;
- private String studentName;
- private String description;
- /**
- * Constructor for Ticket class
- * @param id Ticket's id
- * @param studentName Name of student who submitted ticket
- * @param description Describes the type of help the student needs
- */
- public Ticket(int id, String studentName, String description) {
- this.id = id;
- this.studentName = studentName;
- this.setDescription(description);
- }
- /**
- * Checks if two tickets are the same.
- * @param Obj Object that we will be compared with the calling ticket object
- * @return True if the two tickets are the same, False otherwise
- */
- public boolean equals(Object obj) {
- if (!(obj instanceof Ticket))
- return false;
- if (((Ticket) obj).id == this.id
- && ((Ticket) obj).studentName.equals(this.studentName)
- && ((Ticket) obj).getDescription()
- .equals(this.getDescription())) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Get description of ticket object.
- * @return description (instance variable).
- */
- public String getDescription() {
- return description;
- }
- /**
- * Set description of ticket object
- * @param description The description of the problem.
- */
- public void setDescription(String description) {
- this.description = description;
- }
- /**
- * Print the contents of the ticket
- * @return a string that contains the ticket id, studentName and description
- */
- @Override
- public String toString() {
- return id + ": " + studentName + " (" + description + ")";
- }
- }
- // NOTE: For all the classes below, you should write the class and methods
- // headers as we have written for the Ticket class above. Refer to our CSE 11
- // style guide that is linked from the PA8 specifications.
- class Node {
- // TODO #1: Add instance variables below
- private Ticket data;
- private Node next;
- // TODO #2: Implement the Node constructor below
- public Node(Ticket data) throws NullPointerException{
- if (data==null) {
- throw new NullPointerException();
- }
- else {
- this.data = data;
- }
- }
- // TODO #3: Implement the getters and setters below
- public Node getNext() {
- return next;
- }
- public void setNext(Node next) {
- this.next = next;
- }
- public Ticket getData() {
- return data;
- }
- public void setData(Ticket data) {
- this.data = data;
- }
- @Override
- public String toString() {
- return "| " + data + " | "; //copy & paste this line into your code.
- }
- }
- class CustomLL {
- // TODO #4: Add instance variables below
- private Node head;
- private Node tail;
- private int size;
- // TODO #5: Implement the CustomLL constructor below
- public CustomLL() {
- }
- // TODO #6: Implement the CustomLL constructor below
- public CustomLL(List<Ticket> list) {
- if (list == null) {
- throw new NullPointerException();
- }
- for(int i = 0; i<list.size(); i++) {
- try {
- addLast(list.get(i));
- }
- catch(NullPointerException e) {
- Ticket x = new Ticket(-1, null, null);
- addLast(x);
- }
- }
- }
- // TODO #8: Implement the size method below
- public int size() {
- return this.size;
- }
- // TODO #9: Implement the getters and setters below
- public Node getHead() {
- return this.head;
- }
- public void setHead(Node head) {
- this.head = head;
- }
- public Node getTail() {
- return this.tail;
- }
- public void setTail(Node tail) {
- this.tail = tail;
- }
- // TODO #10: Implement the addFirst method below
- public void addFirst(Ticket toAdd) {
- Node add = new Node(toAdd);
- add.setNext(head);
- setHead(add);
- if (tail == null) {
- tail = head;
- }
- size++;
- }
- // TODO #11: Implement the addLast method below
- public void addLast(Ticket toAdd) {
- Node last = new Node(toAdd);
- if(head == null) {
- head = last;
- tail = last;
- }
- else {
- tail.setNext(last);
- tail = last;
- }
- size++;
- }
- // TODO #12: Implement the remove method below
- public boolean remove(Ticket toRemove) throws NullPointerException {
- if(toRemove == null) {
- throw new NullPointerException();
- }
- else {
- return true;
- }
- }
- // TODO #13: Implement the remove method below
- public Ticket remove(int index) throws IndexOutOfBoundsException {
- if (index >= size || 0 > index) {
- throw new IndexOutOfBoundsException();
- }
- size--;
- remove(index);
- return ();
- }
- @Override
- public String toString() {
- // Do NOT change this method!
- if (head == null) {
- return "Linked List is empty!";
- }
- Node curr = head;
- String str = "";
- while (curr != null) {
- if (curr == head)
- str = curr.toString();
- else
- str = str + "-> " + curr;
- curr = curr.getNext();
- }
- return str;
- }
- }
- // TODO #14: implement LockedQueueException class
- class LockedQueueException extends Exception {
- public LockedQueueException(String s) {
- super(s);
- }
- }
- // TODO #15: implement FullQueueException class
- class FullQueueException extends Exception {
- public FullQueueException(String s){
- super (s);
- }
- }
- class LabQueue {
- // TODO #16: Add instance variables below
- private CustomLL tickets;
- private int capacity;
- private boolean locked;
- // TODO #17: Implement the LabQueue constructor below
- public LabQueue(int capacity) {
- this.tickets = new CustomLL();
- this.locked = true;
- this.capacity = capacity;
- }
- // TODO #18: Implement getCapacity method below
- public int getCapacity() {
- return this.capacity;
- }
- // TODO #19: Implement isLocked method below
- public boolean isLocked() {
- return this.locked;
- }
- // TODO #20: Implement getTickets method below
- public CustomLL getTickets() {
- return tickets;
- }
- // TODO #21: Implement unlockQueue method below
- public void unlockQueue() {
- this.locked = false;
- }
- // TODO #22: Implement lockQueue method below
- public void lockQueue() {
- this.locked = true;
- }
- // TODO #23: Implement submitTicket method below
- public boolean submitTicket(Ticket t)
- throws LockedQueueException, FullQueueException {
- if (this.locked == true) {
- throw new LockedQueueException("Cannot submit a ticket to a locked queue");
- }
- if (capacity>tickets.size()) {
- throw new FullQueueException("Cannot submit a ticket to a queue that is at capacity");
- }
- try {
- tickets.addLast(t);
- return true;
- }
- catch (NullPointerException e) {
- System.out.println("Cannot submit a ticket with null value");
- return false;
- }
- }
- // TODO #24: Implement deleteTicket method below
- public boolean deleteTicket(int i) {
- try {
- tickets.remove(i);
- return true;
- }
- catch (IndexOutOfBoundsException e) {
- System.out.println("Cannot remove a ticket at index <i> in LabQueue of size <size>");
- return false;
- }
- }
- }
- public class PA8 {
- public static void main(String[] args) {
- System.out.println("\n***********Testing Ticket class***********");
- Ticket t1 = new Ticket(1, "Jenny", "PA8 help");
- System.out.println(t1);
- Ticket t2 = new Ticket(2, "Matthew", "Help with exceptions!");
- System.out.println(t2);
- Ticket t3 = new Ticket(3, "Cade", "Junit help");
- System.out.println(t3);
- System.out.println("\n***********Testing Node class***********");
- Node n1 = new Node(t1);
- System.out.println(n1);
- Node n2 = new Node(t2);
- System.out.println(n2);
- Node n3 = new Node(t3);
- System.out.println(n3);
- System.out.println("\n***********Testing CustomLL() & addFirst()***********");
- CustomLL myList1 = new CustomLL();
- myList1.addFirst(t1);
- myList1.addFirst(t2);
- myList1.addFirst(t3);
- System.out.println(myList1);
- System.out.println("head = " + myList1.getHead());
- System.out.println("tail = " + myList1.getTail());
- System.out.println("size = " + myList1.size());
- System.out.println("\n***********Testing overloaded CustomLL constructor***********");
- List<Ticket> myTickets = new ArrayList<>();
- myTickets.add(t1);
- myTickets.add(t2);
- myTickets.add(t3);
- CustomLL myList2 = new CustomLL(myTickets);
- System.out.println(myList2);
- System.out.println("head = " + myList2.getHead());
- System.out.println("tail = " + myList2.getTail());
- System.out.println("size = " + myList2.size());
- System.out.println("\n***********Testing addLast() method***********");
- CustomLL myList3 = new CustomLL();
- myList3.addLast(t2);
- myList3.addLast(t1);
- myList3.addLast(t3);
- System.out.println(myList3);
- System.out.println("head = " + myList3.getHead());
- System.out.println("tail = " + myList3.getTail());
- System.out.println("size = " + myList3.size());
- System.out.println("\n***********Testing CustomLL remove by object***********");
- boolean isRemoved = myList3.remove(t2);
- System.out.println("isRemoved = " + isRemoved);
- System.out.println(myList3);
- isRemoved = myList3.remove(t2);
- System.out.println("isRemoved = " + isRemoved);
- System.out.println(myList3);
- isRemoved = myList3.remove(t3);
- System.out.println("isRemoved = " + isRemoved);
- System.out.println(myList3);
- isRemoved = myList3.remove(t1);
- System.out.println("isRemoved = " + isRemoved);
- System.out.println(myList3);
- //
- //
- // System.out.println("\n***********Testing CustomLL remove by index***********");
- // System.out.println(myList2);
- // Ticket removed = myList2.remove(1);
- // System.out.println("Ticket removed = " + removed);
- // System.out.println(myList2);
- // removed = myList2.remove(0);
- // System.out.println("Ticket removed = " + removed);
- // System.out.println(myList2);
- // removed = myList2.remove(0);
- // System.out.println("Ticket removed = " + removed);
- // System.out.println(myList2);
- //
- //
- //
- // System.out.println("\n***********Testing Exception classes***********");
- // Exception lqe = new LockedQueueException("The queue is locked");
- // System.out.println(lqe.getMessage());
- // Exception fqe = new FullQueueException("The queue is full");
- // System.out.println(fqe.getMessage());
- //
- //
- //
- // System.out.println("\n***********Testing LabQueue***********");
- // LabQueue labQ = new LabQueue(3);
- // System.out.println("Capacity is: " + labQ.getCapacity());
- // try {
- // labQ.submitTicket(t1);
- // } catch(LockedQueueException e) {
- // System.out.println(e.getMessage());
- // } catch(FullQueueException e) {
- // System.out.println(e.getMessage());
- // }
- // System.out.println("Is queue locked?: " + labQ.isLocked());
- // System.out.println("Unlocking the queue...");
- // labQ.unlockQueue();
- // System.out.println("Is queue locked?: " + labQ.isLocked());
- // try {
- // labQ.submitTicket(t1);
- // } catch(Exception e) {
- // System.out.println(e.getMessage());
- // }
- // try{
- // labQ.submitTicket(t2);
- // } catch(Exception e) {
- // System.out.println(e.getMessage());
- // }
- // try{
- // labQ.submitTicket(t3);
- // } catch(Exception e) {
- // System.out.println(e.getMessage());
- // }
- // System.out.println("Tickets in queue: " + labQ.getTickets());
- // Ticket t4 = new Ticket(4, "Deva", "Help with LabQueue class");
- // try{
- // labQ.submitTicket(t4);
- // } catch(Exception e) {
- // System.out.println(e.getMessage());
- // }
- // labQ.deleteTicket(1);
- // System.out.println("Tickets in queue: " + labQ.getTickets());
- // try{
- // labQ.submitTicket(t4);
- // } catch(Exception e) {
- // System.out.println(e.getMessage());
- // }
- // System.out.println("Tickets in queue: " + labQ.getTickets());
- // labQ.deleteTicket(0);
- // System.out.println("Tickets in queue: " + labQ.getTickets());
- // try{
- // labQ.submitTicket(null);
- // } catch(Exception e) {
- // System.out.println(e.getMessage());
- // }
- //
- // System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement