CS1400 Lab #13: Implementing the Token Machine Class
Many of the programs that you will create from now on will contain two objects that work together to complete the work of the application:
It is important to note that each object has an important role to play, and the roles must not be confused. The domain object is so named because it contains data and methods that belong to the domain of the application. For example, in a bookstore application you would see domain objects that represent books. A book class would contain data members that describe the book, e.g. the book's author, its title, the publisher, and how much it costs. Methods in the Book Class are used to manipulate and manage the data in the book class. For example, you might have a method that sets the Price of the book. We sometimes use the term business logic to talk about the work that methods in a domain class do.
Domain objects know nothing about the user interface. It is the Form object that defines and manages the user interface. The Form object contains the GUI components (Buttons, TextBoxes, MenuItems, etc.), and event handlers. The Form class knows nothing about what is inside of the domain objects or how domain objects do their work. The responsibility of the Form is to show the user the graphical user interface and respond to events that are generated at the interface.
Before you proceeed, carefully study the code in the example provided here to make sure that you understand this important relationship.
Adding a Domain Class
In this lab you will learn how to add a domain class to a WindowsForms Application. Remember that the code in the Form class is responsible for managing the user interface. All of the business logic in your program will reside in the domain classes you add to your program. Take careful notes on how you write this code as any programs you write from now on that use a graphical user interface will follow this pattern.
Here is a design for a token dispensing machine class. You designed a similar class in your last lab. This class only has two data members; the number of tokens it contains and the number of quarters it contains. There are five operations you can do on this machine. These are represented by the following methods:
TokenMachine( ): The constructor that is used to initialize the data members of a TokenMachine object.
GetToken( ): You get a token by putting a quarter in the machine. The number of tokens in the machine is reduced by one, and the number of quarters in the machine is increased by one.
CountTokens( ): Returns the number of tokens that are in the machine.
CountQuarters( ): Returns the number of quarters that are in the machine.
Reset( ): The reset operation removes all of the quarters and fills the machine with tokens. For this class we will assume that the machine holds 100 tokens.
Designing the GUI
Start Visual Studio and create a new Windows Form Application. Name your project TokenMachine. Lay out a user interface for your project. It need not look like the example shown below, but it should have the same functionality and by intuitive to use.
Writing the Code
In this project we are going to create a domain class that represents a TokenMachine and connect it to the graphical user interface. To do this, carefully follow these steps: