• Tidak ada hasil yang ditemukan

DESIGN

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 163-167)

10 FINAL EXAMPLE: LOTTO

10.1 DESIGN

The above can be seen as a specification of requirements for the program to be written. The program can be written in many ways, and from the one to use the program it plays no great role, as long as the program meets the requirements, but in practice, programs must be maintained, and here it is very important how the program is written. It is important that the program is written in such a way that the code is easy to read and understand, and it is important that the code is written so that a change in one place does not mean that large parts of the program must be rewritten. To meet these goals the program must be organized into good classes, and the question is what it is and how to find and write these classes. It is not simple, and there is not a unique solution, and this is largely what the following books is about, but below will explain a little about how this program is organized into classes, and thus the program’s architecture.

If one considers the concept of a lottery row, then it is a sequence of different integers (lottery numbers) within a range. The lotto number is nothing more than an int, but I have nevertheless chosen to define a class to represent a lottery number. The reason is that the class must be able to validate a lottery number and check that it is legal in relation to the current lotto game, and the program should therefore have the following class:

Often you will in system development illustrate the classes that way. It says that the program must have a class called LottoNumber, and this class must have a private int variable called number. In this case, this number represents the value of a lottery number. The figure also shows that the class should have two constructors, where the first is a default constructor that should create a LottoNumber object with a random legal value, while the other has the value as a parameter. It should be used when the program creates the week’s lottery.

In addition the figure shows that the class must have a method getNumber() to returns the lottery number’s value.

The class LottoNumber, must as mentioned validate a lottery number and should therefore know what is legal values. In this case, it is solved by defining the following class

The class should have a method init() with three int parameters which indicates the minimum legal lottery number, the largest legal lottery number and the number of lottery numbers in a row. The class has methods that can return these values. In the figure, the methods are shown underlined, which means that they are static methods and can be used without having an object. The solution was chosen because the three values concerning a concrete lotto game must be available throughout the program. It should be emphasized that it is not the best solution, but it is the solution that is feasible with what until this place is said about C#.

With this class available, it is clear that the class LottoNumber and its methods can validate whether a lottery number is correct.

The two classes LottoNumber and LottoGame is both simple, but the next class is more complex:

The class represents a lottery row, and thus a sequence of lottery numbers. The class has two constructors, where the first must create a lottery row with random lottery numbers, while the other creates a lottery row with specific numbers and should be used to represent the week’s lottery. Both constructors must ensure that the lottery row created is legal in relation to the current lottery game, and therefore have to use the class LottoGame. In particular, is the first constructor complex since it must ensure the creation of a lottery row where all the numbers are different, and that the row’s numbers are sorted in ascending order. The class should have a method validate() with a parameter that is a LottoRow. This parameter represents the week’s lottery, and the method should be used to determine how many correct numbers there are on the current row. The result is that the method updates the result variable, and when it is private, there is also a method that can return the value.

The class LottoRow must consist of a number of LottoNumber objects, but instead of showing it as a variable in the class figure, we show it as a link between the two classes:

Here the * tells that a LottoRow object refers to several LottoNumber objects.

There is also a need for a class that may represent a number of lottery rows and thus a particular lottery game. Although I above has mentioned that the program not uses a concept similar to a lottery ticket, I have decided to call the next class for LottoCoupon, and one can think of the class as a concept similar to a lottery ticket of any size:

The class has only two methods, but they are, in turn, complex both. The first must create a new lottery game, and it has five parameters, where the first three are the game parameters (minimum lottery number, biggest lottery number and the number of lottery numbers in a row), next is the number of rows to be played, and finally the name of the file that will contain the result. With the class LottoRow available is in principle simple enough to create the wished number lotto rows, but the method must ensure not to create two identical rows, and the rows must be sorted before being written to the file. Finally, the method create() will write the rows to the file, and here it was decided that the first line should be the game’s parameters. The method validate() is used to validate a lotto game against the weekly lottery. The method has three parameters, the first are the numbers for the week’s lottery, while the next is the name of the file with the game to be validated, and the last is the name of the file to the result. The method then should both read data from a file and write data to a file. The most difficult is the of course validating of the individual rows.

The class LottoCoupon is definitely the program’s most complex class.

As the last is the class Lotto, which is the class with the Main() method. In principle, it is a simple method because it simply must create a LottoCoupon object and call one of the methods. Yet fills the code much, because the main() method must parse the command line, and this time there are many possibilities for the arguments. Finally, it is also the Main() method that has to print an error message in case of errors.

The program consists of a total of 5 classes, and the relationship can be illustrated by the following figure:

If you consider the above design you will notice that the design does not utilize that the language is C#. For example, method names are written with a lowercase letter as the first character and I have also not utilized that C# supports properties. In fact, at the time of design, it is intended to work independently of current programming languages.

Dalam dokumen C# 1: Basic Syntax and Semantics (Halaman 163-167)