Join Peggy Fisher for an in-depth discussion in this video Solution: Hospital stay, part of Learning Java.
- I hope you had fun with this last challenge. The challenge was meant to get you started with adding decision-making logic, loops, and optionally, methods. Let's take a look at my solution. Remember that your solution might be very different. There are a few lines of code that will be new, but I think it will help you understand how you can expand on the information I've already provided. Let's walk through the code. In the main method, I start by declaring variables that I'll need for both an in-hospital and outpatient stay.
I have medication charges, lab service charges, hospital charges, and then total cost. I added a string variable called "response." This will allow me to ask the user if they have another patient. If they do, I'll run through the loop again. Otherwise, I'll exit. In my solution, I use a do-while loop. I say, "Do." The first thing I do is check to see if it's an overnight stay. I created a method for this, but you wouldn't have to. Let's take a look at the method real quick. It returns a Boolean variable and it does not take any parameters.
It simply asks the user, "Is this an overnight stay?" And I kinda give the user a hint as to what I'm looking for, a Y or an N. I read in their response. If their response equals a Y, I wanna make sure that I don't worry about whether they enter a capital Y or a lower case Y, so I have equalsIgnoreCase. If they did say Y, I return true, otherwise, I return false. So in my decision-logic, on line 29, it says, "if(checkForOvernight() == true)." Don't forget, two equals signs are important because if you only put one, it would always set it to true.
Two equals signs compare. One, assigns. If it is an overnight stay, I ask the user to enter the cost of the hospital stay. I read that into my hospital charges. Otherwise, I wanna reset the hospital charges to 0. Remember, this loop is going to allow us to calculate charges for more than one patient. Next, I prompt the user to enter the information about the medication charges and the lab service cost. Now I have all the information I need to calculate the total cost.
Total cost is equal to hospital charges, which is zero if there was not an overnight stay, medication charges and lab service charges. I call a method, printTotal, and I give it the total cost so it can print out the total cost for this patient. Now I need to prompt the user to see if they wanna enter information about another patient. Again, I'm looking for a Y or an N. I read in the response, now I down to the While part of my do-while. While the response is equal to yes then I'm gonna go back up top.
As soon as this is false, it'll stop the program, and since this is the last line of the program, my program will end. I do wanna point out, do you see the While condition has a semicolon at the end? The only time you'll have that semicolon is when it's a do-while statement. Okay, if you didn't get a chance to try this first, go back and try again. It doesn't have to be exactly like this solution. Create your own! Before we end, let's go ahead and run the program so you can see how it works.
I'll click on the green arrow. The first thing is asking us if it's an overnight stay. Let's start by saying yes. Now it wants to know the cost of the hospital stay. Let's put in 1200 dollars. Medication charges, 400. And lab services, 350. The total cost for this patient is $1950. Now you can see I don't have to start the program over again, because I am inside my do-while loop. I'm being prompted to ask if there's another patient.
Let's say yes. This time, let's say, "No," to overnight stay. We'll enter our medication charges and our lab service, and that's all we have. So our total should be 1250, and it is. Okay, that's it for this program. I hope you enjoyed this challenge.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs