Java Reference
In-Depth Information
A
switch
statement handles the possible choices. Note that, as required, there is a
break
at the end of every
case
block except for the last one. Remember that if the
break
statements are missing, then the code will execute several
case
statements at once, which is
undesirable behavior. Note that some of the
choice
blocks are surrounded by braces. The
reason is that we define the variable
amount
and we want this variable to be local for the
block. In other words, there are four variables
amount
that are declared. They happen to
have the same name, but they are different variables because they are defined in different
blocks and have different scopes. As a general rule, define every variable in the innermost
scope that you can.
If Choice 1 is selected, then the program prints the available cash and inventory. Note
that the
currencyFormatter
method is used to format the cash amount. Choice 2 prints the
prices of apples and pears, where the
currencyFormatter
method is used again to format
the prices. One may consider putting the code for Choice 1 or for Choice 2 in a separate
method. However, such a method will be custom designed and not reusable. In general, try
to create methods that will be called multiple times and will therefore make the program
shorter.
Choices 3-6 are similar to each other. First, a method is called to get the quantity of
product to be bought or sold. Then a method to do the transaction is called. The method
will modify some global variables (e.g.,
cash
,
appleInventory
, etc.) and will return
true
if
the transaction is successful. Note that it is a bad programming practice for methods that
perform sale transactions to print to the screen. Their job is to just perform the transaction
and report whether the transaction was successful or not. If they also print to the screen,
then this will significantly reduce their reusability. If one of the transaction methods returns
false
, then the
main
method prints the appropriate error message.
Note that the code for Choices 3-6 is pretty similar. The astute reader may wonder
if we can put this code in a method and just call the method 4 times. This is certainly
a possibility. However, such a method will have an
if
statement that checks the input
parameters and will call different methods based on the value of an input parameter. In
other words, there will be no significant reduction in the number of lines of code. In general,
a method that executes different code based on the value of an input parameter is considered
bad programming practice. It is much simpler to just create a separate method for every
possibility.
The code for the
printMenu
method is shown next. It simply prints the menu.
{
System. out . println (
"1. Print cash balance and inventory"
);
System. out . println (
"2. Print today's prices"
);
System. out . println (
"3. Buy apples"
);
System. out . println (
"4. Sell apples"
);
System. out . println (
"5. Buy pears"
);
System. out . println (
"6. Sell pears"
);
System. out . println (
"7. I am done for today"
);
public static void
printMenu()
}
The
getChoice
method is shown next.
public static int
getChoice()
{
Scanner keyboard =
new
Scanner(System. in) ;
int
choice ;
do
System. out . print (
"Your choice: "
);
choice = keyboard. nextInt() ;
}
while
(choice
>
7
||
choice
<
1) ;
return
choice ;