CSE 5910 - Sample programming test 1

Phase 0

Study the API of the class Product which can be found here. The jar sample1.jar contains the code of the class Product.

Phase 1

Present the user with the following prompt.

Enter product id: 
The entry has to be made on the same line. If entered id is smaller than the minimal ID of a product or greater than the maximal ID of a product, your program should crash with the message "Invalid ID.

Here are two sample runs:

Enter product id: 123456
and
Enter product id: 1234567
Exception in thread "main" java.lang.RuntimeException: Invalid ID
  at type.lib.ToolBox.crash(ToolBox.java:25)
  at Test1.main(Test1.java:20)

Phase 2

Next, expand your program by printing the price of the product.

Here are two sample runs:

Enter product id: 123456
Price: 754.59
and
Enter product id: 234567
Price: 589.15

Phase 3

Next, adjust your program so that the user is reprompted whether he or she wants to look up the price of another product. In particular, after the first product has been handled, prompt the user More products to look up? (y/n). The entry has to be made on the same line. If the user enters the string y, then reprompt the user. Otherwise the program terminates.

Here are two sample runs:

Enter product id: 123456
Price: 754.59
More products to look up? (y/n) y
Enter product id: 234567
Price: 589.15
More products to look up? (y/n) n
and
Enter product id: 123456
Price: 754.59
More products to look up? (y/n) y
Enter product id: 234567
Price: 589.15
More products to look up? (y/n) y
Enter product id: 123456
Price: 754.59
More products to look up? (y/n) n

Phase 4

Finally, adjust your program so that it prints the average of the price of the products at the end. In particular, print at the end Average price: followed by the average stock price (two digits precision as in the sample runs below).

Here are two sample runs:

Enter product id: 123456
Price: 754.59
More products to look up? (y/n) y
Enter product id: 234567
Price: 589.15
More products to look up? (y/n) n
Average price: 671.87
and
Enter product id: 123456
Price: 754.59
More products to look up? (y/n) y
Enter product id: 234567
Price: 589.15
More products to look up? (y/n) y
Enter product id: 123456
Price: 754.59
More products to look up? (y/n) n
Average price: 699.44