Java Coding – Multiple Variables to create answer variable

  • A+
Category:Languages

long time reader, first time poster :)

Needing a guidance moment from the coding gods on how I could go about making my code do what I need it to.

So the situation is: I need to tell the program to run the calculation of the ticket price times the number of tickets purchased. Seems easy? BUT - There are three variables pre-luding to this.

See below:

    int TicketType = 0;     double ChildTicket = 18.00;     double AdultTicket = 36.00;     double SeniorTicket = 32.50;     int TicketQuantity = 0;     double TotalCost = ? 

Essentially - What i need to have happen from a Psuedocode level to make this easier:

Compute TotalCost = Result of ticket type (ChildTicket,AdultTicket or SeniorTicket) * TicketQuantity 

Any help would be amazing! :)

 


You can use a switch statement to find out the TicketType (Child, Adult, Senior) and thus the price of a single ticket:

double price; switch(TicketType){     case 1:         price = ChildTicket;         break;     case 2:         price = AdultTicket;         break;     case 3:         price = SeniorTicket;         break;        default:         // use default value for price or throw an error } 

And then multiply that price with the TicketQuantity to get the TotalCost:

double TotalCost = price * TicketQuantity; 

NOTE: Java naming conventions says, that variables should always start with a lowerCase letter, so instead of TotalCost prefer totalCost etc.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: