|
A Ticket to Success
Represent theater seats in a Java class to read and print tickets as well as display the results on screen
by James W. Cooper
May 2003 Issue
Sometimes a problem presents itself in a way you don't expect so that it is not obvious that a simple Java program is the quickest solution. For example, I belong to a community theater group that puts on one comic opera a year for five performances. There are not enough performances to justify complicated ticketing software, but the problem of selling reserved seats and producing tickets at the lowest cost continues to bedevil us.
One possible solution: we could buy laser printer blank ticket forms. I bought ours from perforatedpaper.com. The problem is converting a list of seats in a theater into tickets and printing them out. One of the problems that we have to solve is that most theaters are not square: rows have different numbers of seats, and often the seat number system is not exactly linear. For example, many theaters number their prime center block of seats with three digits, and the less desirable side blocks with single digits, with the odd seats on one side and the even seats on the other. Therefore, it is not productive to spend time on an analytic algorithm for computing seat numbers. Instead, we need to start with a list of seats. Figure 1 shows a section of the seat layout for the auditorium our company is using.
As you may deduce from the borders, this layout was typed into an Excel spreadsheet. It is probably possible to write a program to read from the Excel file in Java, perhaps using JDBC, but it is far easier to simply export the data into a comma-separated text file and write a program to read it in. You can see a portion of such a file where we represent aisles and missing seats with a blank instead of a seat number (see Figure 2).
We can design a Java class to represent a seat (and whether it is sold) simply enough (see Listing 1). Similarly, we can design a SeatRow class to present rows of seats as a Vector of seat objects (see Listing 2). It is of course quite simple to read in the seat rows from this file using a StringTokenizer (see Listing 3), but how do we print the tickets? Printing them is the hard part in Java, because printing has the somewhat deserved reputation for innate complexity.
Since Java 1.2, printing has become a lot friendlier, however, and it is this newer set of classes we'll use to print out our tickets. In Java 1.2, you can print a set of pages all having the same format, as we'll be doing here, or you can establish a Book class where different pages have different formats.
Back to top
|