1

Hello I am trying to write a code that satisfies the following output:

Here are the booths at the start:
   Ticket booth with 5 passes and 50 tickets
   Ticket booth with 1 passes and 10 tickets
Booth 1 has made $43.0
Booth 2 has made $20.5
Here are the booths at the end:
   Ticket booth with 3 passes and 30 tickets
   Ticket booth with 0 passes and 2 tickets 

I was able to do it for the first three lines but I am having trouble writing my methods for sellPass() ,sellTickets() and moneyMade() . Here is the following tester code that is supposed to output my code:

 public class TicketBoothTestProgram 
{
    public static void main(String args[]) 
    {
        TicketBooth  booth1, booth2;

        booth1 = new TicketBooth(5, 50);
        booth2 = new TicketBooth(1, 10);

        System.out.println("Here are the booths at the start:");
        System.out.println("   " + booth1);
        System.out.println("   " + booth2);

        // Simulate selling 2 passes, 5 tickets, 12 tickets and 3 tickets from booth1
        booth1.sellPass(); 
        booth1.sellPass(); 
        booth1.sellTickets(5); 
        booth1.sellTickets(12); 
        booth1.sellTickets(3); 

        // Simulate selling 2 passes, 5 tickets, 12 tickets and 3 tickets from booth2
        booth2.sellPass(); 
        booth2.sellPass(); 
        booth2.sellTickets(5); 
        booth2.sellTickets(12); 
        booth2.sellTickets(3); 

        // Make sure that it all worked
        System.out.println("\nBooth 1 has made $" + booth1.moneyMade);
        System.out.println("Booth 2 has made $" + booth2.moneyMade);
        System.out.println("\nHere are the booths at the end:");
        System.out.println("   " + booth1);
        System.out.println("   " + booth2);
    }
} 

and here is the code in which I am trying to write my methods:

 public class TicketBooth 
    {
      float moneyMade;
      int availablePasses;
      int availableTickets;

      static float TICKET_PRICE = 0.50f;
      static float PASS_PRICE = 16.50f;

      public TicketBooth() 
      {
        moneyMade = 0.0f;
        availablePasses = 0;
        availableTickets = 0;
      }

      public TicketBooth(int p)
      {
        moneyMade = 0.0f;
        availablePasses = p;
        availableTickets = 0;
      }

      public TicketBooth(int p, int t) 
      {
        moneyMade = 0.0f;
        availablePasses = p;
        availableTickets = t;
      }

        public String toString() 
        {
          return("Ticket booth with " + this.availablePasses + " passes and " + this.availableTickets + 
        " tickets");
        }

        public sellPass() 
        {
          //INSERT CODE HERE FOR SELLING A PASS
        }

        public sellTickets()
        {
            //INSERT CODE HERE FOR SELLING A TICKET
        }


    }

Any help is appreciated thank you!

Alex Chavez
  • 185
  • 1
  • 10
  • 2
    What are you having trouble with specifically? – neminem Jan 30 '14 at 23:26
  • 1
    I would imagine that your `sellPass/Tickets` methods are going to need a quantity to start with. You will need to check to see if you have the required quantity on hand, possibly raising some kind of error if you don't. Reduce the "number on hand" value by the quantity and add `price x quantity` to money made... – MadProgrammer Jan 30 '14 at 23:28
  • How do you know how much a ticket and how much a pass? – RP- Jan 30 '14 at 23:28
  • @Rp- i declared it, a ticket price is $0.50 and a pass is $16.50 – Alex Chavez Jan 30 '14 at 23:30
  • @MadProgrammer it starts (for booth1 ) with 5 passes and 50 tickets and for booth2 it starts with 1 passes and 10 tickets. In booth1 it ends up with 3 passes and 30 tickets and for booth to it ends with 0 passes and 2 tickets – Alex Chavez Jan 30 '14 at 23:33
  • Got to ask again: what's the problem you are having? Your code indicates you haven't tried anything yet so it's not obvious to us what your trouble is unless you tell us. – Radiodef Jan 30 '14 at 23:37
  • @AlexChavez But HOW do you know how many tickets/passes you need to process in each method, particularly if you are calling the methods multiple times? – MadProgrammer Jan 30 '14 at 23:37

1 Answers1

1

Your sellPass() method is fairly simple. I have added boolean return types which will retun false if you run out of tickets or passes.

public boolean sellPass() {
   //Check if you have a pass to sell
   if (availablePasses > 0) {
       //you have passes to sell
       moneyMade += moneyPerPass; // add the money
       availablePass--; //decrement pass counter 
       return true;
   }
   return false; // not enough passes to sell
}

Same way your sellTickets(int noOfTickets), your method should allow how many tickets you want to sell.

public boolean sellTickets(int noOfTickets) {
    if(availableTickets >= noOfTickets) {
        moneyMade += noOfTickets * pricePerTicket;
        availableTickets -= noOfTickets;
        return true;
    }
    return false;
}
RP-
  • 5,827
  • 2
  • 27
  • 46
  • I assume the compile time error is there on purpose so the OP has something to figure out on their own. ; ) – Radiodef Jan 30 '14 at 23:42