Suppose you write a program that models the express checkout aisle at the supermarket.

A customer is allowed to purchase a maximum number of items from each department.

At your favorite market, this limit is five. Your program prints a message informing the shopper of the maximum number of items he or she can buy from each department.

 

Listing 1 - ExpressCheckout.java (version 1)

public class ExpressCheckout
{
    public static void main(String[] args)
    {
        System.out.println("Limit of " + 5 + " items from produce.");
        System.out.println("Limit of " + 5 + " items from meats.");
        System.out.println("Limit of " + 5 + " items from seafood.");
        System.out.println("Limit of " + 5 + " items from bakery.");
    }
}

 

Suppose another supermarket has longer checkout aisles, and uses a purchase limit of seven. You rewrite your program but make a mistake while changing each five to seven.

 

Listing 2 - ExpressCheckout.java (version 2 - incorrect)

public class ExpressCheckout
{
    public static void main(String[] args)
    {
        System.out.println("Limit of " + 7 + " items from produce.");
        System.out.println("Limit of " + 7 + " items from meats.");
        System.out.println("Limit of " + 5 + " items from seafood.");
        System.out.println("Limit of " + 7 + " items from bakery.");
    }
}

 

This mistake could be avoided by using a constant for the purchase limit.

 

 

 

 

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.