FIT100 Winter 2002
Lab Activity 11
"My Ice Cream Parlor"
This activity will spread over two lab sections. In it, you will continue to get practice with VB features you already have seen (such as collections), and learn some new ones which will be useful for the last part of Project 2. In particular, the "for each" loop will be needed, and later on, new ways to manipulate strings.
0. Please read at least steps 1-3 before starting.
1. Play around with this program, until you figure out what it does.
2. Your next job is to build a VB program that works more or less like that one. To save time getting started, you can make local copy of this form, and upload it to your Dante account for safekeeping, too. Create a VB project that incorporates this form. Before making any changed, get the form running (it won't do anything but display).
3. Here's an important groundrule for this lab: you can't make any changes to the objects by using the VB property boxes! That means that to change captions, colors, fonts, etc. you have to write VB program statements, in some appropriate SUB.
Most of the initial setting up of captions, etc. should be done in the form load event handling. Begin with this event handler, and write as many statements as are needed to get the form looking approximately correct. You can add objects if you wish; it's not absolutely necessary. If you do: you must change their properties (except the object name) via VB statements, not with the property box.
You should define some variables at the top to hold values like the cost per scoop, the tax, etc. Set those values in the form load event. Then you can use those variables to display the information at the top.
Test as you go.
4. Once the form starts to look roughly OK, begin writing event handlers. Your final program doesn't have to be as fancy as the sample, but it must allow the user to specify how many scoops, and compute the correct total price.
Make your changes a few at a time, and test carefully after each few changes. Here's a suggested order for proceeding:
Now you are done with the first big step. Be sure to save your work as you go along. It is also wise to make additional copies for backup as you go.
5. Ready for the next big jump? Read through at least the next three steps carefully. The main addition will be the flavors. The owner of the store can add new flavors and the customer has to specify a flavor. Here is a sample to play with. Try different things. What happens if the customer types a flavor that is not in the list? or types something that is capitalized differently?
6. When you feel you understand what the program is supposed to do, start on your version of it. Do NOT modify the form you built earlier. Instead, make a copy of it, with a different name, and use that.
7. Store the flavors in a Collection. You can declare it at the top this way:
Dim colFlavors as new Collection
8. In the form load SUB, place at least one flavor initially in the collection. For this lab, the flavors don't need special keys. If you say
colFlavors.add("vanilla")
the item "vanilla" is added, and will have a default number key. The key for the 1st element you add is 0, the next is 1, etc. But you won't actually have to use these numbers.
9. Get the program running again, without the flavor choice by the customer or the add flavor commands.
10. Now, write a SUB like this:
private sub showFlavors ( )
' a bunch of stuff
end sub
The purpose of this sub is to find all the flavors in the collection, and write them into the flavors label (yellow in the sample). This involves a new type of VB statement. Here's the outline of what you need to do:
dim flavor
for each flavor in colFlavors ' required -- write it just like this
' write statements here to modify the flavors label.
' at this point in the program, the variable 'flavor' will contain exactly one of the items from the collection.
' you can treat it as a string. For example, you can concatenate it with another string
' these statements will be repeated automatically, once for each item in the collection
next flavor 'required
11. After your showFlavors is written, call it from the form load event handler, probably near the end. If everything is working, the initial flavors (the ones you add in the form load) will show up.
12. Next, add a text box to the form where the customer will type in the desired flavor. Change the event handlers to take this into account. Initially, don't do anything with what the customer types in, except maybe echo it back (the owner says: "OK, we have flavor X").
13. Now, add code to check whether what the user types in is in the collection. This will involve a "for each" loop like the one you used earlier. This time, you want to compare what the user typed in with each of the items in the collection. If there is a match, things are good and the sale continues. If there is not a match (what the customer wants is not in the table), then you have to make the customer type in again.
Preview of the next jump:
Suppose "Vanilla" is one of the flavors. As the program stands now, any of the following would be rejected: "vanilla", "Vanila", "vannila", "vanella", etc. Not very friendly! The goal of the next stage is this: if there is a match to at least the first two characters that the customer types in, regardless of case, then the input is considered correct.