Hey, it's been a year never using Java.

I want to get help with this, so I was trying to print the number of ticket entered, but got error.

Can help?

import java.util.*;

class Transport
{
    protected double price; // Price per person for each transport
    protected int seatsRemaining; // Number of seats remaining for each transport

    public double getPrice() {
        return price;
    }

    public int getSeatsRemaining() {
        return seatsRemaining;
    }
}

class Train extends Transport
{
    Train()
    {
        price = 38;
        seatsRemaining = 120;
    }
    void printTickets()
    {
        seatsRemaining -= numTrainTickets;
    }
}

class Bus extends Transport
{
    Bus()
    {
        price = 25;
        seatsRemaining = 42;
    }
    void printTickets()
    {
        seatsRemaining -= numBusTickets;
    }
}

class Taxi extends Transport
{
    Taxi()
    {
        price = 60;
        seatsRemaining = 4;
    }
    void printTickets()
    {
        seatsRemaining -= numTaxiTickets;
    }
}

public class ticketCounter 
{
    Train train = new Train();
    Bus bus = new Bus();
    Taxi taxi = new Taxi();

    public static void main (String[] args) 
    {
        Scanner scan = new Scanner (System.in);
        int numSelect;
        int transportSelect;

        do
        {
            do
            {
                System.out.println("\nMenu:"
                + "\n1. Print tickets"
                + "\n2. View transaction"
                + "\n3. Exit");
                System.out.print("Select (1 - 3): ");
                numSelect = scan.nextInt(); 
            } while (numSelect < 1 || numSelect > 3);

            switch (numSelect)
            {
                case 1:
                    do
                    {
                        System.out.println("\nSelect transport:"
                        + "\n1. Train"
                        + "\n2. Bus"
                        + "\n3. Taxi"
                        + "\n4. Exit");
                        System.out.print("Select (1 - 4): ");
                        transportSelect = scan.nextInt();
                    } while (transportSelect < 1 || transportSelect > 4);

                    if (transportSelect == 1)
                    {
                        pTrain();
                        break;
                    }
                    else if (transportSelect == 2)
                    {
                        pBus();
                        break;
                    }
                    else if (transportSelect == 3)
                    {
                        pTaxi();
                        break;
                    }
                    break;
            }
        } while (numSelect != 3);
    }

    public static void pTrain()
    {
        Scanner scan = new Scanner (System.in);
        int numTrainTickets; // Number of train tickets to print

        System.out.println("How many train tickets do you want to print out?");
        System.out.print("Number of tickets: ");
        numTrainTickets = scan.nextInt();

        while (numTrainTickets > train.getSeatsRemaining())
        {
            System.out.println("The number of tickets you've entered has exceeded the number of tickets remaining.");
            System.out.print("Number of tickets: ");
            numTrainTickets = scan.nextInt();
        }

        train.printTickets();
    }

    public static void pBus()
    {
        Scanner scan = new Scanner (System.in);
        int numBusTickets; // Number of bus tickets to print

        System.out.println("How many bus tickets do you want to print out?");
        System.out.print("Number of tickets: ");
        numBusTickets = scan.nextInt();

        while (numBusTickets > bus.getSeatsRemaining())
        {
            System.out.println("The number of tickets you've entered has exceeded the number of tickets remaining.");
            System.out.print("Number of tickets: ");
            numBusTickets = scan.nextInt();
        }

        bus.printTickets();
    }

    public static void pTaxi()
    {
        Scanner scan = new Scanner (System.in);
        int numTaxiTickets; // Number of bus tickets to print

        System.out.println("How many taxi tickets do you want to print out?");
        System.out.print("Number of tickets: ");
        numTaxiTickets = scan.nextInt();

        while (numTaxiTickets > taxi.getSeatsRemaining())
        {
            System.out.println("The number of tickets you've entered has exceeded the number of tickets remaining.");
            System.out.print("Number of tickets: ");
            numTaxiTickets = scan.nextInt();
        }

        taxi.printTickets();
    }

}

Post the complete text of your error message, or a detailed deescription of your unexpected behaviour. Don't expect people to guess what "got error" means in this case.

Got inheritance problem

Error called: Non-static variable cannot be referenced from static context.

Lets try again:
What is the complete exacact text of your error mesage, including the line number(s)?

Lets try again:
What is the complete exacact text of your error mesage, including the line number(s)?

lines 123, 130, 142, 149, 161, and 168.
Exact text of the error message: "Non-static variable cannot be referenced from static context."

123,130: The variable train is declared as an instance variable - it has a different value for every instance of its class. You refer to that variable in a static method pTrain(). Becuase that's a static method it has no instance of the class associated with it, so it does not know which value of train to use. (And similarly for the other error lines).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.