Results 1 to 8 of 8
  1. #1
    deantmcg is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Bus Ticket OOPs Assignment

    I have 3 classes created for a bus ticket machine for 4 different routes (Red, Green, Yellow & Blue Route)

    One class processes cash inserted
    One processes the tickets if they are available
    One is the application program

    I am a little stuck now and don't know what direction to go.

    I am on the application program and I have to implement the following:

    Allow the user to choose and pay for a ticket
    (If there is a ticket still available)
    Display the price
    Keep receiving the money and telling the user if more is required until enough money has been given
    Give ticket and any change
    Update appropriate counter variables
    If no ticket is available, tell the user.

    I would love some guidance in what to do next, keep in mind the encouraged use of arrays.

    Java Code:
    public class CashHandler
    {
        private double cashInside;
        
        public CashHandler()
        {
        	this.cashInside = 0;
        } 
        	
        public CashHandler (double cashInside)
        {
        	this.cashInside = cashInside;
        }
        
        public double Deposit (double moneyIn)
        {
        	cashInside = cashInside + moneyIn;
        	return cashInside;
        }
        
        public double Change (double price)
        {
        	double change = cashInside - price;
        	return change;
        }
        
        public void setCashInside (double cashInside)
    	{
    		this.cashInside=cashInside;
    	}
    	
    	public double getCashInside()
    	{
    		return cashInside;
    	}
    }

    Java Code:
    public class TicketHandler
    {
        private double ticketPrice;
        private int ticketsLeft;
        
        public TicketHandler()
        {
        	this.ticketPrice = 0;
        	this.ticketsLeft = 0;
        }
        
        public TicketHandler(double ticketPrice, int ticketsLeft)
        {
        	this.ticketPrice = ticketPrice;
        	this.ticketsLeft = ticketsLeft;
        }
        
        public int sellTicket (int ticketsLeft)
        {
        	ticketsLeft = ticketsLeft - 1;
        	return ticketsLeft;
        }
        
        public void setTicketPrice (double ticketPrice)
        {
        	this.ticketPrice = ticketPrice;
        }
        
        public void setTicketsLeft (int ticketsLeft)
        {
        	this.ticketsLeft = ticketsLeft;
        }
        
        public double getTicketPrice()
        {
        	return ticketPrice;
        }
        
        public int getTicketsLeft()
        {
        	return ticketsLeft;
        }
    }

    Java Code:
    import java.util.Scanner;
    public class UseHandlers
    {
        public static void main (String [] args)
        {
        	System.out.println("Welcome to the DTMG Express Line!");
    		System.out.println("Follow Instructions to Book Tickets!");
    		System.out.println("------------------------------------");
        	
        	
        	Scanner keyboard = new Scanner(System.in);
        	double price; // price of route ticket
        	int tickets; // tickets available (seats)
        	
        	// Creates route objects, number array size is seats available
        	TicketHandler [] redRoute = new TicketHandler[50]; // Price is 15
        	TicketHandler [] greenRoute = new TicketHandler[28]; // Price is 4
        	TicketHandler [] blueRoute = new TicketHandler[35]; // Price is 8.50
        	TicketHandler [] yellowRoute = new TicketHandler[50]; // Price is 16.50
        	
        	int choice;
        	
        	do
        	{
        		choice = menu();
    
        		if (choice == 1)
        		{
        			price = 15;
        			tickets = redRoute.length;
        		}
    
        		else if (choice == 2)
        		{
        			price = 4;
        			tickets = greenRoute.length;
        		}
    
        		else if (choice == 3)
        		{
        			price = 8.5;
        			tickets = blueRoute.length;
        		}
    
        		else if (choice == 4)
        		{
        			price = 16.5;
        			tickets = yellowRoute.length;
        		}
    
        		else if (choice == 5)
        		{
        		}
    
        		else
        		{
        			System.out.println("Error");
        		}
        	}
    
        	while (choice != 5);
        }
        	
        public static int menu()
        {
        	Scanner keyboard = new Scanner(System.in);
    
        	int choice;
    
    		System.out.println();
        	System.out.println("1. Red Route");
        	System.out.println("2. Green Route");
        	System.out.println("3. Blue Route");
        	System.out.println("4. Yellow Route");
        	System.out.println("5. Exit");
        	System.out.println();
    
        	System.out.print("Enter your choice - ");
        	choice = keyboard.nextInt();
        	System.out.println();
    
        	return choice;
        }
    }

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Java OOPs Help

    Login: if you have logged in and you do not have the option to enter a comment, click refresh and see whether this solves the issue.

    Concerning the code:
    - creating one ticketHandler instance for each ticket seems odd and does not match the code idea in that class. I would create a ticketHandler for each route, so the array holds 4 elements. Use the constructor that sets price and available tickets. Add the ticketHandler to the array afterwards.
    - in the UseHandler class the functionality in the do while loop is crap. Think about the process that you have to go through: you have to get the price of the tour from the ticketHandler instance -> you have to check whether seats are available -> you have to check whether the customer has entered enough money -> then you have to give the customer a ticket, return change and reduce the number of available seats by one. Good news is that you need this functionality only once because the choice of the customer will somehow relate to the ticketHandler instance (and its location in the array).
    - for choice 5 there is still nothing there. Maybe System.exit(0); ?
    - you are missing some sanity checks, example: what happens if the customer gives you less money than the price is? How much change will the program return?
    - erase lines 12 and 13 in the UseHandler class. This is info you get from the ticketHandler instances.
    Last edited by benji2505; 03-29-2017 at 05:05 AM.

  3. #3
    deantmcg is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: Bus Ticket OOPs Assignment

    Quote Originally Posted by benji2505 View Post
    Login: if you have logged in and you do not have the option to enter a comment, click refresh and see whether this solves the issue.

    Concerning the code:
    - creating one ticketHandler instance for each ticket seems odd and does not match the code idea in that class. I would create a ticketHandler for each route, so the array holds 4 elements. Use the constructor that sets price and available tickets. Add the ticketHandler to the array afterwards.
    - in the UseHandler class the functionality in the do while loop is crap. Think about the process that you have to go through: you have to get the price of the tour from the ticketHandler instance -> you have to check whether seats are available -> you have to check whether the customer has entered enough money -> then you have to give the customer a ticket, return change and reduce the number of available seats by one. Good news is that you need this functionality only once because the choice of the customer will somehow relate to the ticketHandler instance (and its location in the array).
    - for choice 5 there is still nothing there. Maybe System.exit(0); ?
    - you are missing some sanity checks, example: what happens if the customer gives you less money than the price is? How much change will the program return?
    - erase lines 12 and 13 in the UseHandler class. This is info you get from the ticketHandler instances.


    I'm currently implementing some of your ideas, I'm very grateful, thanks.
    I'm getting the following message when running the program:

    --------------------Configuration: <Default>--------------------
    Welcome to the DTMG Express Line!
    Follow Instructions to Book Tickets!
    ------------------------------------
    Exception in thread "main" java.lang.NullPointerException
    at UseHandlers.main(UseHandlers.java:16)

    Process completed.


    Here is the updated code:
    Java Code:
    import java.util.Scanner;
    public class UseHandlers
    {
        public static void main (String [] args)
        {
        	System.out.println("Welcome to the DTMG Express Line!");
    		System.out.println("Follow Instructions to Book Tickets!");
    		System.out.println("------------------------------------");
        	
        	
        	Scanner keyboard = new Scanner(System.in);
        	
        	// Creates route objects, number array size is seats available
        	TicketHandler [] Route = new TicketHandler[4];
        	
        	Route[0].setTicketPrice(15);
        	Route[0].setTicketsLeft(50);
        	
        	Route[1].setTicketPrice(9);
        	Route[1].setTicketsLeft(35);
        	
        	Route[2].setTicketPrice(8);
        	Route[2].setTicketsLeft(28);
        	
        	Route[3].setTicketPrice(8.5);
        	Route[3].setTicketsLeft(50);
        
        	int choice;
        	
        	do
        	{
        		choice = menu();
    
        		if (choice == 1)
        		{
        			if (Route[0].getTicketsLeft() > 0)
        			{
        				System.out.println("The cost of a ticket on the Red route is 15.00.");
        				System.out.println("Please insert money into the machine to begin purchase.");
        			}
        		}
    
        		else if (choice == 2)
        		{
        			
        		}
    
        		else if (choice == 3)
        		{
        			
        		}
    
        		else if (choice == 4)
        		{
        			
        		}
    
        		else if (choice == 5)
        		{
        		}
    
        		else
        		{
        			System.out.println("Error");
        		}
        	}
    
        	while (choice != 5);
        }
        	
        public static int menu()
        {
        	Scanner keyboard = new Scanner(System.in);
    
        	int choice;
    
    		System.out.println();
        	System.out.println("1. Red Route");
        	System.out.println("2. Green Route");
        	System.out.println("3. Blue Route");
        	System.out.println("4. Yellow Route");
        	System.out.println("5. Exit");
        	System.out.println();
    
        	System.out.print("Enter your choice - ");
        	choice = keyboard.nextInt();
        	System.out.println();
    
        	return choice;
        }
    }

  4. #4
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Bus Ticket OOPs Assignment

    Looks good so far. You get the NPE, because there is no object in the array (or somewhere else) yet.
    - run the TicketHandler constructor: TicketHandler redRoute = new TicketHandler(15,50); repeat for other routes.
    - add each ticketHandler instance to the array afterwards.
    - in the do while loop, remove the if else statements, use the array with (choice-1) as index.

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Bus Ticket OOPs Assignment

    Afterwards you have to take care of business:
    - ask the customer to pay an amount and have him/her pay an amount.
    - compare the amount to the price: what happens if the customer paid too few, what happens if he/she paid too much, etc.

    In order to do that you create a new instance of CashHandler: CashHandler transaction = new CashHandler(0.0);
    Then you use the instance for all your cash exchange, for example
    transaction.deposit(amountReceivedAsDouble);
    transaction.payout(changeYouHaveCalculatedAsDouble ); <-- create this new method, maybe just a sysout of the amount tb returned and a reset of CashInside.
    transaction.getCashInside(); if you need to know how what the cash status in the machine is.

    I'd say get rid of the change() method, that is something you want to do in the main algorithm. Please use lowercase letters for methods.
    Last edited by benji2505; 03-30-2017 at 04:10 AM.

  6. #6
    deantmcg is offline Member
    Join Date
    Dec 2016
    Posts
    8
    Rep Power
    0

    Default Re: Bus Ticket OOPs Assignment

    Quote Originally Posted by benji2505 View Post
    Afterwards you have to take care of business:
    - ask the customer to pay an amount and have him/her pay an amount.
    - compare the amount to the price: what happens if the customer paid too few, what happens if he/she paid too much, etc.

    In order to do that you create a new instance of CashHandler: CashHandler transaction = new CashHandler(0.0);
    Then you use the instance for all your cash exchange, for example
    transaction.deposit(amountReceivedAsDouble);
    transaction.payout(changeYouHaveCalculatedAsDouble ); <-- create this new method, maybe just a sysout of the amount tb returned and a reset of CashInside.
    transaction.getCashInside(); if you need to know how what the cash status in the machine is.

    I'd say get rid of the change() method, that is something you want to do in the main algorithm. Please use lowercase letters for methods.
    Thanks a lot Benji. I now have the program but with a minor problem.

    When I enter the money and complete a ticket transaction for one person - the program uses the first persons total amount inserted for all the next customers.

    Also, I am aware the code for each choice is almost identical and this isn't the most efficient way of doing things I just wanted to get the code working. I'm not sure how to condense it down


    My code below
    Java Code:
    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class UseHandlers
    {
        public static void main (String [] args)
        {
        	Scanner keyboard = new Scanner(System.in);
    
        	TicketHandler redRoute = new TicketHandler(15, 50);
        	TicketHandler greenRoute = new TicketHandler(9, 35);
        	TicketHandler blueRoute = new TicketHandler(8, 28);
        	TicketHandler yellowRoute = new TicketHandler(8.5, 50);
    
        	// Creates route objects, number array size is seats available
        	TicketHandler [] Route = new TicketHandler[4];
    
        	Route[0] = redRoute;
        	Route[1] = greenRoute;
        	Route[2] = blueRoute;
        	Route[3] = yellowRoute;
    
        	CashHandler transaction = new CashHandler(0.0);
        	DecimalFormat moneyFormat = new DecimalFormat("0.00");
    
        	int choice;
        	double inserted;
    
        	do
        	{
        		choice = menu();
    
        		if (choice == 1)
        		{
        			if (Route[0].getTicketsLeft() > 0)
        			{
        				System.out.println("The cost of a ticket on the Red route is "+moneyFormat.format(Route[0].getTicketPrice()));
        				System.out.println("Please insert money into the machine to begin purchase.");
        				System.out.println();
    
        				while (transaction.getCashInside() < Route[0].getTicketPrice())
        				{
        					double owed = Route[0].getTicketPrice() - transaction.getCashInside();
        					System.out.println("Money owed: "+moneyFormat.format(owed));
        					System.out.print("Money inserted: ");
        					inserted = keyboard.nextDouble();
        					transaction.setCashInside(transaction.getCashInside()+inserted);
        				}
    
        				Route[0].setTicketsLeft(Route[0].getTicketsLeft() -1);
    
        				if (transaction.getCashInside() > Route[0].getTicketPrice())
        				{
    						double change;
    						change = transaction.getCashInside() - Route[0].getTicketPrice();
    						System.out.println(transaction.Payout(change));
    
        				}
    
        					System.out.println();
        					System.out.println("Thank You!");
        					System.out.println("Your ticket will now be printed. \nPlease wait...");
    
        					System.out.println("\n");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println("...... DTMG Express Line ......");
        					System.out.println("...............................");
        					System.out.println("... 1 Ticket for Red Route ....");
        					System.out.println("..... "+moneyFormat.format(Route[0].getTicketPrice())+" Paid in full .....");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println(".......... Thank you ..........");
        					System.out.println("....... Enjoy your trip .......");
        					System.out.println("...............................");
        					System.out.println("...............................");
    
        			}
    
        			else
        			{
        				System.out.println("There are no tickets left for this route.");
        				System.out.println("Please contact the front helpdesk for assistance.");
        			}
        		}
    
        		else if (choice == 2)
        		{
    				if (Route[1].getTicketsLeft() > 0)
        			{
        				System.out.println("The cost of a ticket on the Green route is "+moneyFormat.format(Route[1].getTicketPrice()));
        				System.out.println("Please insert money into the machine to begin purchase.");
        				System.out.println();
    
        				while (transaction.getCashInside() < Route[1].getTicketPrice())
        				{
        					double owed = Route[1].getTicketPrice() - transaction.getCashInside();
        					System.out.println("Money owed: "+moneyFormat.format(owed));
        					System.out.print("Money inserted: ");
        					inserted = keyboard.nextDouble();
        					transaction.setCashInside(transaction.getCashInside()+inserted);
        				}
    
        				Route[1].setTicketsLeft(Route[1].getTicketsLeft() -1);
    
        				if (transaction.getCashInside() > Route[1].getTicketPrice())
        				{
    						double change;
    						change = transaction.getCashInside() - Route[1].getTicketPrice();
    						System.out.println(transaction.Payout(change));
    
        				}
    
        					System.out.println();
        					System.out.println("Thank You!");
        					System.out.println("Your ticket will now be printed. \nPlease wait...");
    
        					System.out.println("\n");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println("...... DTMG Express Line ......");
        					System.out.println("...............................");
        					System.out.println(".. 1 Ticket for Green Route ...");
        					System.out.println("..... "+moneyFormat.format(Route[1].getTicketPrice())+" Paid in full ......");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println(".......... Thank you ..........");
        					System.out.println("....... Enjoy your trip .......");
        					System.out.println("...............................");
        					System.out.println("...............................");
    
        			}
    
        			else
        			{
        				System.out.println("There are no tickets left for this route.");
        				System.out.println("Please contact the front helpdesk for assistance.");
        			}
        		}
    
        		else if (choice == 3)
        		{
    				if (Route[2].getTicketsLeft() > 0)
        			{
        				System.out.println("The cost of a ticket on the Blue route is "+moneyFormat.format(Route[2].getTicketPrice()));
        				System.out.println("Please insert money into the machine to begin purchase.");
        				System.out.println();
    
        				while (transaction.getCashInside() < Route[2].getTicketPrice())
        				{
        					double owed = Route[2].getTicketPrice() - transaction.getCashInside();
        					System.out.println("Money owed: "+moneyFormat.format(owed));
        					System.out.print("Money inserted: ");
        					inserted = keyboard.nextDouble();
        					transaction.setCashInside(transaction.getCashInside()+inserted);
        				}
    
        				Route[2].setTicketsLeft(Route[2].getTicketsLeft() -1);
    
        				if (transaction.getCashInside() > Route[2].getTicketPrice())
        				{
    						double change;
    						change = transaction.getCashInside() - Route[2].getTicketPrice();
    						System.out.println(transaction.Payout(change));
    
        				}
    
        					System.out.println();
        					System.out.println("Thank You!");
        					System.out.println("Your ticket will now be printed. \nPlease wait...");
    
        					System.out.println("\n");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println("...... DTMG Express Line ......");
        					System.out.println("...............................");
        					System.out.println("... 1 Ticket for Blue Route ...");
        					System.out.println("...... "+moneyFormat.format(Route[2].getTicketPrice())+" Paid in full ......");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println(".......... Thank you ..........");
        					System.out.println("....... Enjoy your trip .......");
        					System.out.println("...............................");
        					System.out.println("...............................");
    
        			}
    
        			else
        			{
        				System.out.println("There are no tickets left for this route.");
        				System.out.println("Please contact the front helpdesk for assistance.");
        			}
        		}
    
        		else if (choice == 4)
        		{
    				if (Route[3].getTicketsLeft() > 0)
        			{
        				System.out.println("The cost of a ticket on the Yellow route is "+moneyFormat.format(Route[3].getTicketPrice()));
        				System.out.println("Please insert money into the machine to begin purchase.");
        				System.out.println();
    
        				while (transaction.getCashInside() < Route[3].getTicketPrice())
        				{
        					double owed = Route[3].getTicketPrice() - transaction.getCashInside();
        					System.out.println("Money owed: "+moneyFormat.format(owed));
        					System.out.print("Money inserted: ");
        					inserted = keyboard.nextDouble();
        					transaction.setCashInside(transaction.getCashInside()+inserted);
        				}
    
        				Route[3].setTicketsLeft(Route[3].getTicketsLeft() -1);
    
        				if (transaction.getCashInside() > Route[3].getTicketPrice())
        				{
    						double change;
    						change = transaction.getCashInside() - Route[3].getTicketPrice();
    						System.out.println(transaction.Payout(change));
    
        				}
    
        					System.out.println();
        					System.out.println("Thank You!");
        					System.out.println("Your ticket will now be printed. \nPlease wait...");
    
        					System.out.println("\n");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println("...... DTMG Express Line ......");
        					System.out.println("...............................");
        					System.out.println(".. 1 Ticket for Yellow Route ..");
        					System.out.println("...... "+moneyFormat.format(Route[3].getTicketPrice())+" Paid in full ......");
        					System.out.println("...............................");
        					System.out.println("...............................");
        					System.out.println(".......... Thank you ..........");
        					System.out.println("....... Enjoy your trip .......");
        					System.out.println("...............................");
        					System.out.println("...............................\n");
    
        			}
    
        			else
        			{
        				System.out.println("There are no tickets left for this route.");
        				System.out.println("Please contact the front helpdesk for assistance.");
        			}
        		}
    
        		else if (choice == 5)
        		{
        		}
    
        		else
        		{
        			System.out.println("Error");
        		}
        	}
    
        	while (choice != 5);
        }
    
        public static int menu()
        {
        	Scanner keyboard = new Scanner(System.in);
    
        	int choice;
    
    		System.out.println();
    		System.out.println("Welcome to the DTMG Express Line!");
    		System.out.println("Follow Instructions to Book Tickets!");
    		System.out.println();
    		System.out.println("------------------------------------");
    		System.out.println();
        	System.out.println("1. Red Route");
        	System.out.println("2. Green Route");
        	System.out.println("3. Blue Route");
        	System.out.println("4. Yellow Route");
        	System.out.println("5. Exit");
        	System.out.println();
    
        	System.out.print("Enter your choice - ");
        	choice = keyboard.nextInt();
        	System.out.println();
    
        	return choice;
        }
    }

  7. #7
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    398
    Rep Power
    5

    Default Re: Bus Ticket OOPs Assignment

    You have to reset the value after the transaction has been completed.

    As for condensation of code: The whole idea behind storing the routes in an array is to condense the actual algorithm code. Let me reiterate a hint I gave you earlier: you can erase all if clauses that refer to choices. You can address the specific routes with something like route[choice-1]. You have to adjust all ticketHandler-instance specific methods with getters and you might have to add a property like route name in the TicketHandler class.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Bus Ticket OOPs Assignment

    You've put a lot of work into this so I wouldn't necessarily make too many changes. But conceptually,
    all you need from the passengers view is a ticket machine class. An instance of this class would call a method
    to set the route, number of tickets and price.

    You would also use the ticket machine to buy a ticket. When it prompts you for the route, you simply use an instance
    of the ticket handler associated with that route to handle the transactions. By doing it this way you could essentially
    add a whole new route with a single line and not change any other code.

    Now what I would recommend you change is any code that repeats itself. Line 59 to 75 are essentially repeated
    in three other places. Why not create a method to display that and call the method with the values that change?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. oops
    By jpenta in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2016, 08:30 PM
  2. Need Help with my ticket Program
    By Gruff in forum New To Java
    Replies: 4
    Last Post: 05-02-2013, 05:59 AM
  3. ebooks for oops better oops concept
    By puneetnegi1986 in forum New To Java
    Replies: 1
    Last Post: 06-16-2010, 03:53 AM
  4. OOPS example
    By MarkWilson in forum New To Java
    Replies: 2
    Last Post: 09-22-2008, 01:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •