import java.util.Scanner;
public class ValidIntegerExample2 {
    public static void main(String[] args) {
        Scanner myScanner = new Scanner(System.in);
        
        int money = 50;
        while (true) {
            System.out.println("You have $" + money + ".");
            System.out.println("What would you like to do?");
            System.out.println("1. Send money to a friend");
            System.out.println("2. Take a loan");
            System.out.println("3. Use simple calculator");
            System.out.println("4. Buy a cake");
            System.out.println("5. Exit program");
            
            switch (nextValidInteger(myScanner, 1, 5)) {
                case 1: //send money to friend
                    System.out.println("How much money would like to send (1-" + money + ")?");
                    int send = nextValidInteger(myScanner, 1, money);
                    money -= send;
                    System.out.println("You just sent $" + send + " to a random friend.");
                    
                    break;
                case 2:
                    System.out.println("How big load would you like to take?");
                    int loan = nextValidInteger(myScanner, 1, false);
                    money += loan;
                    System.out.println("You just took a loan of $" + loan + ".");
                    
                    break;
                case 3:
                    System.out.println("Please enter the two integer you want to sum");
                    
                    int number1 = nextValidInteger(myScanner);
                    int number2 = nextValidInteger(myScanner);
                    
                    System.out.println(number1 + " + " + number2 + " = " + (number1 + number2));
                    
                    break;
                case 4:
                    System.out.println("Time for a cake");
                    if (money >= 15) {
                        System.out.println("Nice cake");
                        money -= 15;
                    }else{
                        System.out.println("You can't afford a cake");
                    }
                    
                    break;                  
                case 5:
                    return;
            }
        }
    }
    
    static int nextValidInteger(Scanner scanner) {
        return nextValidInteger(scanner, Integer.MIN_VALUE, Integer.MAX_VALUE);
    }
    
    static int nextValidInteger(Scanner scanner, int limit, boolean useUpperLimit) {
        if (useUpperLimit) {
            return nextValidInteger(scanner, Integer.MIN_VALUE, limit);
        }else{
            return nextValidInteger(scanner, limit, Integer.MAX_VALUE);
        }       
    }
  
    
    static int nextValidInteger(Scanner scanner, int min, int max) {
        int integer;
        
        do {
            //remove anything that's not an integer
            while(!scanner.hasNextInt()) {
                scanner.next();
            }
            
            //get the integer
            integer = scanner.nextInt();
            
            //check if it's in the range
        }while(integer < min || integer > max);
        
        return integer;
    }
}