Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Sammy Samkough
- // Simple Show
- // Spec: A seat could / should have more information (be it's own object), but here it is either taken or not taken only.
- // Default hall is a small 50 seat venue.
- // The first 5 rows go for $40 each... yes cheap by today's standards... but I remember prices like that :)
- // subsequent rows are just $20 each
- public class SimpleShow
- {
- private boolean[][] seat;
- /** Creates a concert hall with 10 rows and 5 seats in each row
- */
- public SimpleShow()
- {
- seat = new boolean[10][5];
- }
- /** Creates a concert hall with the size specified by rows and cols
- */
- public SimpleShow(int rows, int cols)
- {
- seat = new boolean[rows][cols];
- }
- /** If the seat is not already occupied then it makes it occupied and returns true
- /* If it is already occupied the method will return false
- */
- public boolean reserveSeat(int row, int col)
- {
- boolean isOccupied = false;
- if (seat[row][col] == false)
- {
- isOccupied = true;
- }
- else
- {
- isOccupied = false;
- }
- return isOccupied;
- }
- /** Makes the seat at row, col empty
- */
- public void clearSeat(int row, int col)
- {
- seat[row][col] = false;
- }
- /** Resets all seats to available.
- */
- public void clearAllSeats()
- {
- for (int r = 0; r < seat.length; r++)
- {
- for (int c = 0; c < seat[r].length; c++)
- {
- seat[r][c] = false;
- }
- }
- }
- /** @return the number of seats still available in the hall
- */
- public int numSeatsAvailable()
- {
- int count = 0;
- for (int r = 0; r < seat.length; r++)
- {
- for (int c = 0; c < seat[r].length; c++)
- {
- if (seat[r][c] == true)
- {
- count++;
- }
- }
- }
- return count;
- }
- /** @return the total revenue for the show based upon tickets sold
- /* The first 5 rows go for $40 each
- /* The next 5 rows are just $20 each
- */
- public int getTotalRevenue()
- {
- int total = 0;
- for (int r = 0; r < seat.length; r++)
- {
- for (int c = 0; c < seat.length; c++)
- {
- if (seat[r][c].length <= 5)
- {
- if (seat[r][c] == true)
- {
- total += 40;
- }
- }
- else
- {
- total += 20;
- }
- }
- }
- return total;
- }
- /** Reserve the first 2 adjacent seats and return true if successful
- /* If two adjacent seats could not be found, leave the state of the show unchanged
- /* and return false.
- /* return true if two adjacent seats were found, false otherwise
- */
- public boolean getTwoTogether()
- {
- boolean isAdjacent = false;
- for (int r = 0; r < seat.length; r++)
- {
- for (int c = 0; c < seat[r].length; c++)
- {
- if (seat[r][c] == false && seat[r][c + 1] == false)
- {
- isAdjacent = true;
- break;
- }
- }
- }
- return isAdjacent;
- }
- /** Return the lowest seat number in the specified row for a block
- /* of adjacent seats. If no such block exists, return -1.
- /* @param row the row number to check
- /* @param seatsNeeded the number of adjacent empty seats needed
- /* @return lowest seat number for a block of contiguous adjacent seats of the given size
- /* or -1 if no such block exists
- */
- public int findAdjacent(int row, int seatsNeeded)
- {
- int index = 0, count = 0, lowIndex = 0;
- for (int r = 0; r < seat.length; r++)
- {
- for (int c = 0; c < seat.length; c++)
- {
- // if (row.getTwoTogether)
- }
- }
- return -1;
- }
- /** If a seat is available show a 'O', if occumpied show an 'X'
- /* return a String with one row per line
- */
- public String toString()
- {
- String result = "AP Concert Hall - Simplified\n";
- return result;
- }
- }
- -------------------------------------------------------------------------------------------------------------------------------
- // Mr. A
- // Simple Show Runner
- // Spec: A simple client to test out the SimpleShow object
- import java.util.*;
- public class SimpleShowRunner
- {
- public static void main(String[] args)
- {
- SimpleShow show = new SimpleShow();
- int seatNum;
- show.reserveSeat(0,0);
- show.reserveSeat(0,1);
- show.reserveSeat(0,2);
- show.reserveSeat(0,4);
- show.reserveSeat(2,2);
- show.reserveSeat(3,2);
- show.reserveSeat(4,2);
- show.reserveSeat(5,1);
- show.getTwoTogether();
- System.out.println(show);
- System.out.println("Seats Still Available: " + show.numSeatsAvailable());
- System.out.println("Total Revenue: $" + show.getTotalRevenue());
- System.out.println("\nLooking for 3 adjacent seats in row 2 (third row)... ");
- seatNum = show.findAdjacent(2,3);
- if(seatNum == -1)
- System.out.println("\t\tNot available");
- else
- System.out.println("\t\tAvailable in row 3 beginning with seat number " + seatNum);
- System.out.println("Looking for 3 adjacent seats in row 6... ");
- seatNum = show.findAdjacent(6,3);
- if(seatNum == -1)
- System.out.println("\t\tNot available");
- else
- System.out.println("\t\tAvailable in row 6 beginning with seat number " + seatNum);
- System.out.println("Looking for 8 adjacent seats in row 7 (third row)... ");
- seatNum = show.findAdjacent(7,8);
- if(seatNum == -1)
- System.out.println("\t\tNot available");
- else
- System.out.println("\t\tAvailable in row 7 beginning with seat number " + seatNum);
- System.out.println("Looking for 3 adjacent seats in row 5 (third row)... ");
- seatNum = show.findAdjacent(5,3);
- if(seatNum == -1)
- System.out.println("\t\tnot available");
- else
- System.out.println("\t\tAvailable in row 5 beginning with seat number " + seatNum);
- show.clearAllSeats();
- System.out.println("\nNew Show\nSeats Still Available: " + show.numSeatsAvailable());
- System.out.println("Total Revenue: $" + show.getTotalRevenue());
- show = new SimpleShow(2,4);
- System.out.println(show);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement