top of page

Java Assignment

Public·2 members

Solution: P08 Badger Coaster

Overview

The executives of Theme Parks Inc. have decided to build a new theme park in the Madison

area featuring some Bucky Badger themed rides . Hearing of your awesome programming skills,

they have decided to come to you for assistance. You are tasked with helping the creation of

an application for the park that uses boarding passes and a queue to manage ride lines as well

as provide some other basic functionalities.


Learning Objectives

The goals of this assignment includes implementing a variation on a traditional queue as well

as re-enforcing previous course topics.










P08 Badger Coaster requirement PDF







Complete Solution:


BGNode.java


public class BGNode {

  private BoardingGroup group;
  private BGNode next;

  public BGNode(BoardingGroup group) {
    this.group = group;
    this.next = null;
  }

  public BGNode(BoardingGroup group, BGNode next) {
    this.group = group;
    this.next = next;
  }

  public BoardingGroup getGroup() {
    return group;
  }

  public BGNode getNext() {
    return next;
  }

  public void setNext(BGNode next) {
    this.next = next;
  }

}


BoardingGroup.java


public class BoardingGroup {

    private String groupName;
    private int groupSize;
    private boolean isVIP;

    public BoardingGroup(String groupName, int groupSize) {
        this.groupName = groupName;
        this.groupSize = groupSize;
        this.isVIP = false;
    }

    public int getSize() {
        return groupSize;
    }

    public String getName() {
        return groupName;
    }

    public void makeVIP() {
        isVIP = true;
    }

    public boolean isVIP() {
        return isVIP;
    }
}


QueueADT.java


public interface QueueADT <T> {
	
	public boolean isEmpty();
	
	public int size();
	
	public void	enqueue(T newObject);
	
	public void clear();
	
	public T peek();
	
	public T dequeue();
}


RideQueue.java


public class RideQueue implements QueueADT<BoardingGroup> {
    BGNode front, back;
    int capacity, numOfPeople, numOfGroups;

    public RideQueue(int capacity) {
        this.capacity = capacity;
        this.numOfPeople = 0;
        this.numOfGroups = 0;
        this.front = null;
        this.back = null;
    }

    public void enqueue(BoardingGroup newGroup) {
        BGNode n = new BGNode(newGroup);
        if (numOfPeople + newGroup.getSize() <= capacity) {
            numOfPeople += newGroup.getSize();
            numOfGroups++;
            if (newGroup.isVIP()) {
                n.setNext(front);
                front = n;
            } else if (front == null) {
                front = n;
                back = front;
            } else {
                back.setNext(n);
                back = back.getNext();
            }
            return;
        }
        throw new java.lang.IllegalStateException("Capacity of Ride is Full");
    }

    public boolean isEmpty() {

        return numOfPeople == 0;
    }

    public int size() {

        return numOfGroups;
    }

    public BoardingGroup peek() {
        if (front != null) {
            return front.getGroup();
        }
        throw new java.util.NoSuchElementException("No Group in the Queue");
    }

    public BoardingGroup dequeue() {
        if (front != null) {
            BoardingGroup f = front.getGroup();
            numOfPeople -= f.getSize();
            numOfGroups--;
            front = front.getNext();
            return f;
        }
        throw new java.util.NoSuchElementException("Empty Ride");
    }

    public String toString() {
        String s = "Number of People in Queue: " + numOfPeople + "\n";
        s += "Number of Groups in Queue: " + numOfGroups + "\n";
        s += "Group Names in Queue: ";
        BGNode current = front;
        while (current != null) {
            String groupName = current.getGroup().getName();
            s += groupName + " ";
            current = current.getNext();
        }
        return s;
    }

    public void clear() {
        front = back = null;
        numOfGroups = numOfPeople = 0;
    }

}


ThemeParkApp.java


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * Driver for RideQueue. Reads commands from a text files and executes them
 * accordingly.
 * 
 * @author Michelle Jensen
 */
public class ThemeParkApp {
  public static void main(String[] args) throws IOException {
    List<String> fileLines = Files.readAllLines(Paths.get("sample.txt"));
    String command = "";
    String[] commandParts;

    // Default queue capacity and ride capacity. Can change values if desired.
    RideQueue coaster = new RideQueue(50);
    int trainCapacity = 24;

    // Process each line in the text file.
    for (int i = 0; i < fileLines.size(); i++) {
      commandParts = fileLines.get(i).split(" ");
      command = commandParts[0].toUpperCase();

      // ENTER Command
      if (command.equals("E")) {
        enter(coaster, commandParts);
      }

      // BREAKDOWN Command
      if (command.equals("B")) {
        breakdown(coaster);
      }

      // PREVIEW Command
      if (command.equals("P")) {
        preview(coaster);
      }

      // RIDE Command
      if (command.equals("R")) {
        ride(coaster, trainCapacity);
      }

      // STATUS Command
      if (command.equals("S")) {
        status(coaster);
      }
    }
  }

  private static void status(RideQueue coaster) {
    System.out.println("Retrieving Status...");
    System.out.println(coaster.toString());
    System.out.println("------------------------------------");
  }

  private static void enter(RideQueue coaster, String[] commandParts) {
    System.out.println("Entering into ride line...");
    String groupName = commandParts[1];
    int groupSize = Integer.parseInt(commandParts[2]);

    BoardingGroup newGroup = new BoardingGroup(groupName, groupSize);
    /*
     * newGroup = CALL YOUR BoardingGroup CONSTRUCTOR HERE. NOTE: var groupName is
     * the name of the group from the file and var groupsize is the number of people
     */
    // done

    if (commandParts.length == 4) {
      if (commandParts[3].toUpperCase().equals("V")) {
        newGroup.makeVIP();
        // Only do this once you have completed section 7.
        /* CALL YOUR VIP STATUS MUTATOR HERE ON newGroup */
      }
    }

    try {
      coaster.enqueue(newGroup);
      System.out.println(groupName + "'s group of " + groupSize + " has entered the line for Badger Coaster.");
    } catch (IllegalStateException e) {
      System.out.println("Cannot fit group of that size into queue.");
    }

    System.out.println("------------------------------------");
  }

  private static void breakdown(RideQueue coaster) {
    System.out.println("Ride Breakdown...");
    System.out
        .println("The ride has broken down. All " + coaster.size() + " group(s) have been removed from the line.");
    coaster.clear();
    System.out.println("------------------------------------");
  }

  private static void preview(RideQueue coaster) {
    System.out.println("Previewing the front of the line...");

    try {
      BoardingGroup peeked = coaster.peek();
      int peekedSize = peeked.getSize();
      /* peekedSize = CALL YOUR NUMBER OF PEOPLE IN GROUP ACCESSOR HERE ON peeked */
      String peekedName = peeked.getName();
      /* peekedName = CALL YOUR NAME ACCESSOR HERE ON peeked */
      System.out.println(peekedName + "'s group of " + peekedSize + " is at the front of the line.");
    } catch (NoSuchElementException e) {
      System.out.println("Cannot look at a group from an empty queue.");
    }

    System.out.println("------------------------------------");
  }

  private static void ride(RideQueue coaster, int trainCapacity) {
    System.out.println("Boarding and Running the Ride...");
    int ridingTrain = 0;

    while (!coaster.isEmpty()) {
      BoardingGroup peeked = coaster.peek();
      int peekedSize = peeked.getSize();
      /* peekedSize = CALL YOUR NUMBER OF PEOPLE IN GROUP ACCESSOR HERE ON peeked */

      if (ridingTrain + peekedSize > trainCapacity) {
        break;
      }

      try {
        BoardingGroup removed = coaster.dequeue();
        String removedName = removed.getName();
        /* removedName = CALL YOUR NAME ACCESSOR HERE ON removed */
        int removedSize = removed.getSize();
        /* removedSize = CALL YOUR NUMBER OF PEOPLE IN GROUP ACCESSOR HERE ON removed */

        System.out.println(removedName + "'s group of " + removedSize + " has boarded the Badger Coaster train.");

        ridingTrain += removedSize;
      } catch (NoSuchElementException e) {
        System.out.println("Cannot remove a group from an empty queue.");
      }
    }

    if (ridingTrain == 0) {
      System.out.println("There is no one on the train to ride.");
    } else {
      System.out.println("Train of " + ridingTrain + " people has left the ride station.");
    }

    System.out.println("------------------------------------");
  }
}


Keeping us on top of that foundation, we are creating and providing the best solutions and almost every type of coding solutions to help students.


Contact us for this java assignment Solutions by Codersarts Specialist who can help you mentor and guide for such java assignment, homework, project and coursework



If you have project or assignment files, You can send at contact@codersarts.com directly

19 Views
bottom of page