0

Hi,

The ticket counter program looks fine, but I have ran into a different problem.
It is regarding about the transaction class (lines 64 - 110).

After I input the number of tickets to print out, it will register to the transaction class. But when I want to view the transaction once, the total quantity shown is normal. When viewed twice, the quantity will double or increment.

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;
    }

    public void setSeatsRemaining (int seatsRemaining)
    {
        this.seatsRemaining = seatsRemaining;
    }
}

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

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

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

class Transaction
{
    int totalTrainTickets = 0;
    int totalBusTickets = 0;
    int totalTaxiTickets = 0;

    int trTickets, bTickets, txTickets;

    double totalTrainPrice = 0;
    double totalBusPrice = 0;
    double totalTaxiPrice = 0;
    double totalPrice = 0;

    int addTrainTickets() // getter (total train tickets)
    {
        return totalTrainTickets += trTickets;
    }

    int addBusTickets() // getter (total bus tickets)
    {
        return totalBusTickets += bTickets;
    }

    int addTaxiTickets() // getter (total taxi tickets)
    {
        return totalTaxiTickets += txTickets;
    }

    double totalTR() // getter
    {
        return totalTrainPrice = 38.00 * totalTrainTickets;
    }

    double totalB() // getter
    {
        return totalBusPrice = 25.00 * totalBusTickets;
    }

    double totalTX() // getter
    {
        return totalTaxiPrice = 60.00 * totalTaxiTickets;
    }

    double totalP() // getter
    {
        return totalPrice = totalTrainPrice + totalBusPrice + totalTaxiPrice;
    }
}

public class Ticket_Counter
{

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

        Train train = new Train();
        Bus bus = new Bus();
        Taxi taxi = new Taxi();

        Transaction t = new Transaction();

        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)
                    {
                        trainTickets(train, t);
                        break;
                    }
                    else if (transportSelect == 2)
                    {
                        busTickets(bus, t);
                        break;
                    }
                    else if (transportSelect == 3)
                    {
                        taxiTickets(taxi, t);
                        break;
                    }
                    break;

                case 2:
                    transaction(t);
                    break;
            }
        } while (numSelect != 3);
    }

    public static void trainTickets(Train train, Transaction t)
    {
        Scanner scan = new Scanner (System.in);
        int numTrainTickets; // Number of train tickets to print

        System.out.println("Price: $" + train.getPrice() + "/person");
        System.out.println("Number of seats remaining: " + train.getSeatsRemaining());
        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 seats remaining.");
            System.out.print("Number of tickets: ");
            numTrainTickets = scan.nextInt();
        }

        System.out.println("\nThe total charge is $" + train.getPrice()*numTrainTickets );
        train.numTrainTickets = numTrainTickets;
        train.printTickets();
        t.trTickets = numTrainTickets;
    }

    public static void busTickets(Bus bus, Transaction t)
    {
        Scanner scan = new Scanner (System.in);
        int numBusTickets; // Number of bus tickets to print

        System.out.println("Price: $" + bus.getPrice() + "/person");
        System.out.println("Number of seats remaining: " + bus.getSeatsRemaining());
        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 seats remaining.");
            System.out.print("Number of tickets: ");
            numBusTickets = scan.nextInt();
        }

        System.out.println("\nThe total charge is $" + bus.getPrice()*numBusTickets );
        bus.numBusTickets = numBusTickets;
        bus.printTickets();
        t.bTickets = numBusTickets;
    }

    public static void taxiTickets(Taxi taxi, Transaction t)
    {
        Scanner scan = new Scanner (System.in);
        int numTaxiTickets; // Number of bus tickets to print

        System.out.println("Price: $" + taxi.getPrice() + "/person");
        System.out.println("Number of seats remaining: " + taxi.getSeatsRemaining());
        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 seats remaining.");
            System.out.print("Number of tickets: ");
            numTaxiTickets = scan.nextInt();
        }

        System.out.println("\nThe total charge is $" + taxi.getPrice()*numTaxiTickets );
        taxi.printTickets();
        t.txTickets = numTaxiTickets;
    }

    public static void transaction(Transaction t)
    {
        System.out.println();
        System.out.println("TODAY'S TRANSACTIONS");
        System.out.println("======================================");
        System.out.println("                T.QTY          T.PRICE");
        System.out.println("======================================");
        System.out.println("Train tickets\t: " + t.addTrainTickets() + " x $38.00 = $" + t.totalTR());
        System.out.println("Bus tickets\t: " + t.addBusTickets() + " x $25.00 = $" + t.totalB());
        System.out.println("Taxi tickets\t: " + t.addTaxiTickets() + " x $60.00 = $" + t.totalTX());
        System.out.println("\t\t  TOTAL PRICE: $" + t.totalP());
    }




}
2
Contributors
1
Reply
16
Views
2 Years
Discussion Span
Last Post by JamesCherrill
0

You have methods called printXXX() that change the data values and print nothing. That's baffling. No surprise that the numbers change each time you try to print them.
You repeat number of tickets in each subclass instead of implementing it once in the superclass.
You have repeated identical blocks of code for train, bus etc but this should be a single piece of code that deals with any kind of Transport (this is a basic idea of onject oriented inheritance).
You have methods that return values that you never use.
(etc)

Fix those and we can get on to the next set of problems.

(That may seem harsh, but better to get those comments from here for free and be able to fix them than to wait until your prof marks your homework)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.