...

/

An Introduction to Testing

An Introduction to Testing

In this lesson, we will develop a program and show how testing discovers logical errors.

A Problem solved: The café sign

The cafés in a certain franchise all operate on the same meal schedule but have their own names. We have been employed to write the software for an electronic sign that displays the name of the café, whether it is open or closed, and the current meal served, if any. The meal schedule is as follows:

  • 6 a.m. to 10 a.m. Breakfast
  • 11 a.m. to 2 p.m. Lunch
  • 5 p.m. to 8 p.m.   Dinner
  • Any other time   Closed

Discussion

We must be able to access the current time so that we can create a suitable message for the sign. Recall from an earlier chapter, we introduced the standard class LocalTime, which is in the package java.time. If the statement:

LocalTime time = LocalTime.now(ZoneId.of("America/New_York"));

executes, the expression time() will return a string such as "16:19:38".

During talks with the franchise owner, we have designed the sign’s message. When Café Java is open for lunch, for example, its sign will display the following message:
  Café Java
  OPEN
  Serving Lunch until 2 p.m.
  The time now is 11:45

A few hours later, when the café is closed, the sign will display the following message:
  Café Java
  CLOSED
  See you at 5 p.m.
  The time now is 15:00

Since LocalTime uses a 24-hour notation for the time of day, we will use the same notation for now on the last line of our sign. To simplify our first solution even further, we can omit the third line, ...

Access this course and 1400+ top-rated courses and projects.