r/javahelp • • Nov 25 '15

Ticket prices calculator help

Hi guys I'm trying to write a ticket calculator for a Christmas fair, I need to be able to let the user write in how many adult tickets are needed and also how many child tickets. Then have it be calculated and the total price be displayed. The thing is that I can't see how I'm meant to lay it out to do this. I know what I have to do but I just can't lay it out properly so it will work. Does anyone have any examples of anything like this or could help walk me through it. Would be greatly appreciated thanks guys

0 Upvotes

5 comments sorted by

5

u/aqua_regis Nov 25 '15

What have you tried?


Does anyone have any examples of anything like this

Come on, this program is fairly simple and you shouldn't need an example. If you start looking for examples already on that level, you will face huge difficulties very soon.


But think about the following structure (since the code is so minimal, you can do all inside the main method unless your assignment calls differently):

First, a very coarse structure design (which is applicable to most simpler console based programs):

  • Constant and Variable declaration

  • Gather User input

  • Program logic

  • Final output

Now, look at each of the bullets above and refine them into a detailed plan:

  • Constant and Variable declaration

    • Declare constants for the ticket price of the child/adult tickets (use static final variables)

    • Declare necessary variables (amount of child/adult tickets, gross price child tickets, gross price adult tickets, gross total all tickets)

    • Declare a Scanner for the keyboard input (or use JOptionPane InputDialog if your course calls for it)

  • Gather User input

    • Print a title

    • Ask the user how many adult tickets they want

    • Ask the user how many child tickets they want

  • Program logic

    • calculate the gross price for the adult tickets

    • calculate the gross price for the child tickets

    • calculate the totals

  • Final output

    • Print number of adult tickets - price per adult ticket - gross price for adult tickets

    • Repeat above for child tickets

    • Print gross total


As a programmer, you have to learn to use your own creativity as early as possible. Bulleted lists are a great tool to visualize the individual steps/parts of a program.

Don't immediately think about how you would write the code, try to do some pseudocode, like I did, before even going anywhere near the computer.

Once you have refined the pseudocode, you can start to program.

4

u/millerlit Intermediate Brewer Nov 25 '15

Print to screen for asking question. Save information from user in a variable. Use variable information in a formula. Print to screen formula answer. This looks like homework so please make an attempt at some code.

2

u/desrtfx Out of Coffee error - System halted Nov 25 '15

I know what I have to do but I just can't lay it out properly so it will work.

So, start by dissecting your assignment:

I'm trying to write a ticket calculator for a Christmas fair

That could already be the title printout for your program (and also a possible program name)

I need to be able to let the user write in how many adult tickets are needed

This means that you need:

  • A means to accept user input => a Scanner

  • A means to store a number of adult tickets - a value => a variable

also how many child tickets

Again, you need:

  • the keyboard input method from above

  • A means to store a number of child tickets => another variable

Then have it be calculated

Here, you need to analyse what you need:

  • Do you also need the total prices for the individual categories (adult/child)?

Also, you will need more information:

  • price of a child ticket => should be a constant since the ticket price is unlikely to change

  • price of an adult ticket => again a constant

Here, you will need to do some calculation

  • calculate the total price of adult tickets => store in a variable

  • calculate the total price of child tickets => store in another variable

  • calculate the total price for all tickets => store in another variable

and the total price be displayed.

  • Print the information that you have calculated above.


Now, by just dissecting your assignment, you have a very detailed program execution sequence that can nearly directly be converted into code.

A small program like this doesn't need much structure (laying out), all the code can reside in the main method.

Don't overthink assignments. First dissect them like above and then worry about the layout.

If there were repetitive tasks (collecting several inputs, processing more data, etc.) methods would be a necessity, but here they are not needed.


When designing such simple programs, always try to follow the following pattern:

  • Constant declaration

  • Variable declaration

  • Introduction (title, instructions, etc.)

  • Retrieve user input

  • Process the calculations / logic

  • Output results

1

u/causalNondeterminism Nov 25 '15

The thing is that I can't see how I'm meant to lay it out to do this. I know what I have to do but I just can't lay it out properly so it will work.

what does layout have to do with it? are you trying to make a GUI?

1

u/desrtfx Out of Coffee error - System halted Nov 25 '15

Think that OP wants to know how to lay out the code. => How to organize the code...

Basically, what /u/aqua_regis did.