Hello everybody, welcome to the Java

sample section in this section I’m going to set up a train ticket machine system.

So suppose you’re in a certain station and you want to travel to other …

places. So on a ticket machine, you’ll need to set the location, and

indicate whether you’re an adult or not And you have to tell the number of

tickets to buy to the machine. And then the machine will return the

total price of of your purchase of the tickets. And in my implementation

I would use the enum keyword and the switch-case keywords.

Okay let me start the implementation. I would like to set up the location first. So

an “enum” keyword here is simply helping me to set up a set of constants. Since

the place in a ticket system is a constant, I can treat this … places as

constant by using the “enum” keyword So suppose I have three places for the

ticket machine to choose The first one is SUNNYCITY, the second place for the

machine to let you choose is HAPPYTOWN The third place is called GLORIOUSLAKE. So let me save it. So you can imagine a ticket machine that shows you

that … these three places for you to choose. And then I can go to the ticket

system. Okay I want to set up a static method that helps me calculate the

charge for purchase of the tickets Okay I would return the charge according

to the location to choose, the adult identity for the buyers of the ticket, and

the number of such tickets to buy Here I need um two local variables. One is the

basicCharge, and the other is the totalCharge for the ticket purchase. The

basic charge would depend on the location to go. So according to the place,

I would set the basic charge as follows If the destination is SUNNYCITY, the

basic charge is 2.3 dollars And since I have finished the setup of the

basic charge, I would leave this switch condition, okay If the place is HAPPYTOWN, the basic

charge is 2.6 dollars. Of course, I will leave this switch condition and … when I

have set up the basic charge correctly If the place or the destination is

GLORIOUSLAKE, the basic charge will be three point four dollars and I will

break the switch condition after setting up the basic charge correctly. And now I

handle the situation of the adult identity Okay, if the passengers are not

adults, the basic charge would be simply 90% of

the original basic charge. So we can say that basic charge is for adults. If

the passengers are not adults, the charge will be a bit lower Okay the total charge is simply the

number of tickets multiplied by the basic charge for each ticket. And this

totalCharge will be returned as the return value of the getCharge method.

Okay so let me save it. Now I can go to go to the Driver class Okay I’m going to find out the charge

for a purchase as follows. I would like to go to SUNNYCITY. Because SUNNYCITY is a

constant declared in the enum declaration, I needed to use location

dot SUNNYCITY to indicate that particular constant in the enum class (type), okay.

Here, I would like to say that the passengers are not adults.

Okay and I would like to buy two tickets for non-adults. So I can find

out the charge which is saved as a double primitive coming from the static

method called getCharge Okay the price is 4.14 dollars, okay. The

reason is that I need to buy two tickets to happy … sunny … SUNNYCITY,

Okay so I would have a four point six basic charge, okay. But they are not

adults.Sso the basic charge would be multiplied by zero point nine to help me

get four point one four dollars Let me see another situation Okay suppose I want to buy three tickets

for … three adults. And the location is HAPPYTOWN. So let me see the

result. Yes, I’ll get this amount, zero point eight zero zero zero zero … one.

What does I mean by that? It literally means seven point eight dollars. And why

would I get so many zeros at the back? Because floating point numbers might

not be represented exactly in Java. So it is possible that for some numbers, you

need so many zeros together with the one finally to present it more exactly in the

floating point number system of Java Now I have the third situation.I would

like to go to GLORIOUSLAKE, and all four tickets are for adults. And let me

see the price. I will get thirteen point six dollars. The reason is that

for GLORIOUSLAKE as the destination, the basic

charge for each ticket is three point four dollars. So for four tickets, I

simply multiply four by three point four, which would return thirty point six

dollars for four adults, okay. So this completes the ticket machine system. My

code is intuitive and straightforward, but it may not be always the best or the

most efficient approach. If you can think about any idea to improve my code, please

do so on the comment section below the video. If you like this video, please give

me a like and please subscribe to my channel. Thank you for watching