josherickson
josherickson

Reputation: 43

Passing parameters from method to main

I can't seem to figure out what my parameters should be for my method "public static int[][] sellSeatByPrice". I need to prompt the user for a price (of a seat), then figure out if that price is taken (if it = 0) and if not, assign it the value 0.

Below is my code, help please!

import java.util.Scanner;
/**
 * Write a description of class A10_TheaterSeating here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class A10_TheaterSeating
{
public static void main(String args[])
{
    System.out.println("***Welcome to the Ticket Choice App!***");
    System.out.println();

    int[][] theaterSeats = //set values in seating chart array
    {
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
        {10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
        {10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
        {10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
        {10, 10, 20, 20, 20, 20, 20, 20, 10, 10},
        {20, 20, 30, 30, 40, 40, 30, 30, 20, 20},
        {20, 30, 30, 40, 50, 50, 40, 30, 30, 20},
        {30, 40, 50, 50, 50, 50, 50, 50, 40, 30}
    };
    int[][] seats = theaterSeats;

    printArray(seats); //print the seating chart
    System.out.println();

    //Defining variables
    String str = "";
    String input = "";

    while (!input.equalsIgnoreCase("Q"))
    {
        System.out.print("Select 'S' to pick a seat, 'P' choose a price or 'Q' to quit:     ");
        Scanner in = new Scanner(System.in);
        input = in.next();

        if (input.equalsIgnoreCase("S"))
        {
            System.out.print("Enter row and seat number desired: ");
            int row = in.nextInt();
            int seat = in.nextInt();
            System.out.println();
            sellSeatByNumber(seats, row, seat);

            printArray(seats);
            System.out.println();
        }
        else if (input.equalsIgnoreCase("P"))
        {
           System.out.print("Enter price of seat desired: ");
           int price = in.nextInt();
           System.out.println();
           sellSeatByPrice(seats, row, seat, price);

           printArray(seats);
           System.out.println();
        }
    }
    System.out.println();
    System.out.println("Thank you for choosing the ticket choice app.");
    System.out.println();
}

public static void printArray(int[][] currSeat)
{
    final int ROWS = 9;
    final int COLUMNS = 10;

    for(int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLUMNS; j++)
        {
            System.out.print(currSeat[i][j] + "\t");
        }
        System.out.println();
    }
}

public static int[][] sellSeatByNumber(int[][] seats, int row, int seat)
{
    if (row <= 0 || row > 9)
    {
        if (seat <= 0 || seat > 10)
        {
            System.out.print("Please enter a valid row and seat: ");
        }
    }
    if (seats[row][seat] == 0)
    {
        System.out.print("That seat is taken. Please select another seat: ");
    }
    else
    {
        seats[seats.length - row][seat - 1] = 0;
    }
    return seats;
}

public static int[][] sellSeatByPrice(int[][] seats, int row, int seat, int price)
{
    if (seats[row][seat] = price)
    {
        seats[seats.length - row][seat - 1] = 0;
    }
    return seats;
}
}

Upvotes: 2

Views: 354

Answers (1)

Makoto
Makoto

Reputation: 106528

Your parameters seem fine - the internal logic and syntax is incorrect.

Since you're using static methods, and passing along the seat matrix, that is okay - but you have to check that the values being passed in are inside the bounds of your matrix - or you will get exceptions on them.

For instance, you don't check the bounds in sellSeatByPrice(). You really should do that, or a bogus row/seat combination could blow your program up.

It's also the case that the comparison given is incorrect:

if (seats[row][seat] = price)

should really be

if (seats[row][seat] == price)

as = is assignment, and == is primitive comparison.

Furthermore, in sellSeatByNumber(), you can still run into issues since an out of bounds row/seat combo will still blow up. You do check the bounds - kudos - but you don't return anything if they're outside of those bounds. In all reality, an IllegalArgumentException should be raised if they're stepping out of bounds, or you can return the matrix unmodified (which may be more straightforward).

Upvotes: 1

Related Questions