(whoosh) - Welcome back, how did you make out? I hope you were able to have some fun with that challenge. What I'd like to do is just walk through one possible solution. This is a solution that I came up with when I was attempting this challenge. So let's walk through my code. Since we're not using any files, our data division on line eight does not have anything in it. Let's go ahead down to line 10 where we start working storage. As you can see, starting on line 12, I've defined several variables that I'm going to use in my program.
For example, on line 15, I have the discount amount. I have the discount set here to 20%, .20 I added this variable to make it easier if the discount value changes in the future. We only have to change it in one place. I needed a variable to hold my item price, the quantity, et cetera. Notice that several of these fields have a v in the picture clause. Remember the v stands for an applied decimal. I do want to point out line 23, where I have a variable called user response.
And I have it defined as an alphanumeric character. This is the variable that will hold the response from the user when I ask them if they have any more items. Notice I'm using two 88 clauses here. One, more items is set to the value y and done is set to n. That way, I don't have to evaluate the WS-User Response field, I can just say if more items or if done. Let's scroll down just a little bit more. You'll see on line 33, I have my heading line.
On line 44, I have my detail line. The heading line only contains filler because it's only going to print the headings. The detail line, I need to specify what details I'm going to print. In this case, I have description, price, quantity, and total. Notice the formatting for the detail price and the detail item total. I'm using leading dollar signs so that it will automatically print a floating dollar sign in front of the price. I also use a z in the quantity. Do you remember what that stands for? If you said that it will suppress any zeros, you're correct.
Let's scroll down a little more, and you can see here is where I have my total lines so it will print out the total quantity, the total amount before the discount, and then if there was a discount, it will print that out as well. Okay, lets go to the procedure division. I have a couple display statements just to drop me down a few lines. Then I have my welcome message. I set my user's response to y. Then I perform the process items paragraph through the end of that paragraph until done.
Remember, done is an 88 level from my user response. After I process all the items, I could print out the totals and stop run. Let's scroll down so you can see all of the 200 paragraph. I ask the user for the item description, the item price, and the quantity, and I accept those values into those working storage fields. Then, I move them from working storage to my detail line so I can display them. And there's a couple calculations that I need to do. The first compute statement calculates the item total taking the item price times the item quantity.
Next, I need to add that amount to my running total. And finally, I want to add the quantity to my running total for quantity. I move my item total to my detail total and then I print out that detail line. On line 114, I ask the user if they have anymore items and I accept their answer into that WS-User-Response variable. Okay. Once the process items is done, let me scroll up a little bit. You can see the next thing I do, on line 89, is perform print total.
So now we'll scroll back down. In print total, I need to display detail total line one, which prints the equal signs across the screen. Then I move my working storage fields to my detail total line. If the total sale is greater than 100, then the user gets the discount. So I compute the total with discount taking the total sale minus the amount of total sale times the discount. If the user doesn't get a discount, then I can just go ahead and move WS Total Sale to the detail total discount field.
Finally, I display the last two lines. And then, I do stop run. Okay, let's run the program so you can see how it works. Welcome to pet supplies and more. Enter your item description. Well I can always use dog food. And the price, let's make it 24.99. And I only need one bag. Do you have more items? Well yeah, let's get some more items. Next, let's get some dog treats.
And each treat is only 79 cents but they're on sale, so I'm going to get 10 of them. I want to go ahead and say no this time so I'll see what happens when there is no discount applied. As you can see, I typed in a lowercase n so it did not recognize that as my do you have more items n. So let me go ahead and try it again. So do you have more items? I'll say no. There we go. And there's my total before the discount and my total after the discount. All right, let me clear the screen, and let me run it again.
This time, I'm going to make sure that I spend at least 100 dollars. So let's enter in dog food again and we'll say that the dog food is 24.99. We'll only get one bag, and let's say yes, we do have more items, and let's go ahead and enter cat food. And let's say cat food is 3.29 for a can and I want to get 20 cans. K. 65 and 24. I'm not quite there yet, let me keep going. So I'm going to say yes.
Let's purchase a puppy. And this time, the puppy is 300 dollars and I only want one puppy, I already have one, but one more is okay. And there we go. So now I'm definitely over the 100 dollars so I can get my discount, so I'll say no, no more items. And now you can see I bought a total of 22 items and before the discount, it would have been 390.79. With the discount it's only 312.63, so it looks like it's working.
Whether your program looked like mine, or totally different, as long as we both get the same results, we're good. Well I hope you had fun with the pet store example.
This course is designed to help new and experienced programmers alike add COBOL (or add COBOL back) to their skill set. Peggy Fisher shows how to get a COBOL development environment up and running and how to start programming. She reviews COBOL's data types and constants, control structures, file storage and processing methods, tables, and strings. Challenges issued along the way will help you practice what you've learned.
- Downloading and installing Cygwin and GNU COBOL
- Editing, compiling, linking, and running COBOL programs
- Describing data in COBOL
- Working with verbs and expressions
- Using branching
- Reading and writing sequential files
- Updating and deleting records
- Working with relative and indexed files
- Creating and searching tables
- Handling strings