Java Reference
In-Depth Information
static final double VARIATION = 5 ;
static final double INITIAL CASH = 100;
static final int ITEM COUNT = 2 ;
static final String [] item = new String [] { "apples" , "pears" } ;
static double price [] = new double [ITEMCOUNT ] ;
static int inventory [] = new i n t [ITEMCOUNT ] ;
static double cash = INITIAL CASH ;
public static void main(String [] args) {
for ( int day = 1; day < = NUMBER OF DAYS ; d a y++)
{
for ( int i=0;i < ITEM COUNT ; i ++) {
price[i] = computePrice(BASEPRICE , VARIATION) ;
System. out . println ( "Day: " +day+ " out of 10" );
int choice ;
do
printMenu() ;
choice = getChoice() ;
switch (choice) {
case 1: // Print cash balance and inventory
System. out . println ( "Cash: " + currencyFormatter(cash)) ;
for ( int i=0;i < ITEM COUNT ; i ++) {
System. out . println ( item [ i ] + " inventory: " + inventory [ i
]+ " Price: " + currencyFormatter(price [ i ]) ) ;
break ;
case 2: { //Buy Product
int itemID = getProductID() ;
int amount = getQuantity(item[ itemID] , "buy" );
if ( ! buy( itemID , amount) ) {
System. out . println ( "You don't have enough money" );
break ;
case 3:
// Sell Product
int itemID = getProductID() ;
int amount = getQuantity(item[ itemID] , "sell" );
if ( ! s e l l ( itemID , amount) )
{
{
System. out . println ( "You don't have enough " +item[itemID
]) ;
break ;
}
}
} while (choice != 4);
System. out . println ( "You finished with:" + currencyFormatter(cash)) ;
}
Note that we have simplified the menu. The new printMenu method is shown next.
public static void printMenu()
{
 
Search WWH ::




Custom Search