package fruitbasket;
import java.util.Scanner;
import java.util.Stack;
public class FruitBasket {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Stack fruitStack = new Stack();
System.out.println("Catch and eat any of these fruits : ('apple', 'orange', " + "'mango', 'guava')");
System.out.print("How many of these fruits would you like to catch? ");
int qty = in.nextInt();
System.out.println("Choose a fruit to catch. Press A, O, M or G.");
for(int i = 1; i <= qty; i++){
System.out.print("Fruit " + i + " of " + qty + " : ");
char select = in.next().charAt(0);
if(select == 'a')
fruitStack.push("'apple', ");
else if (select == 'o')
fruitStack.push("'orange', ");
else if (select == 'm')
fruitStack.push("'mango', ");
else
fruitStack.push("'guava', ");
}
how to last in first out?
System.out.println("Your basket now has " + "[" + fruitStack + "]");
System.out.println("Press E to eat a fruit: ");
how to repeat the press e down?
while(true){
char e = in.next().charAt(0);
if(e == 'e'){
fruitStack.pop();
System.out.println("Fruit(s) in the basket : " + "[" + fruitStack + "]" );
}
if (fruitStack.empty())
System.out.println("No more fruits");
break;
}
}
}