Test1
and save your class in a file named
Test1.java
.
Product
which can be found
here.
The jar sample1.jar
contains the code of the class Product
.
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: 123456and
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)
Next, expand your program by printing the price of the product.
Here are two sample runs:
Enter product id: 123456 Price: 754.59and
Enter product id: 234567 Price: 589.15
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) nand
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
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.87and
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