import java.util.Scanner;

/*
 * Uppgift4, Simple atm.
 * author: Joel Curtis, joecur-6
 */

public class Uppgift6 {

	private static Scanner in = new Scanner(System.in);

	public static void main(String[] args) {
		// declare variables
		int[] accountTransactions = new int[10];
		int accountBalance = 0;
		boolean run = true;

		// menu variables to make it easier to read the menu options.
		int deposit = 1;
		int withdrawl = 2;
		int balance = 3;
		int exit = 4;

		while (run) { // keep running menu until exit is given.
			int choice = menu();
			if (choice == deposit) {
				int d = input("\nHow much would you like to deposit? : ");
				if (d > 0) {
					accountBalance += d;
					makeTransaction(accountTransactions, d);
					System.out.println("You have added " + d + "$ to your account.");
				} else {
					System.out.println("You can only deposit a positive amount.\n");
				}
			} else if (choice == withdrawl) {
				int w = input("\nHow much would you like to withdraw? : ");
				if (w > 0 && w <= accountBalance) {
					accountBalance -= w;
					makeTransaction(accountTransactions, -w);
					System.out.println("You have withdrawn " + w + "$ from your account.");
				} else {
					System.out.println("You can only withdraw a positive amount that is lesser than your account balance.\n");
				}
			} else if (choice == balance) {
				showTransactions(accountTransactions, accountBalance);
			} else if (choice == exit) {
				System.out.println("Exiting ATM...\nThank you for using Joels ATM.\nHave a good day.");
				run = false;
			} else {
				System.out.println("That menu option does not exist.\nPlease try again.\n");
			}
		}
		
		in.close();
		System.exit(0); // exits if the while loop finishes.
	}

	// shows message and gets integer input from scanner
	public static int input(String msg) {
		int returnInt;
		System.out.print(msg); // print message.
		while (true) { // asks for an integer until it gets one.
			if (in.hasNextInt()) {
				returnInt = in.nextInt();
				break;
			} else {
				System.out.println("Please enter an integer.");
				in.next();
			}
		}
		return returnInt; // returns integer from scanner buffer.
	}

	// shows transactions and balance
	public static void showTransactions(int[] trans, int balance) {
		for (int x = 0; x < trans.length; x++) {
			if (trans[x] == 0) { // don't print the transactions that doesn't exist.
				x = trans.length;
			} else {
				if (trans[x] > 0) {
					System.out.println(x + 1 + ": +" + trans[x]); // adds a + to deposits
				} else {
					System.out.println(x + 1 + ": " + trans[x]);
				}
			}
		}
		System.out.println("You have an account balance of " + balance + "$");
	}

	// adds value(amount) into the trans array at the right spot
	public static void makeTransaction(int[] trans, int amount) {
		if (findNr(trans) == -1) { // array is full
			moveTrans(trans);
			trans[trans.length - 1] = amount;
		} else { // array is not full
			trans[findNr(trans)] = amount; //get first empty spot and enter value there.
		}
	}

	// finds an empty spot in the trans array
	private static int findNr(int[] trans) {
		int z = -1; // default value of -1 so it returns -1 if it doesn't find an empty space in the array.
		for (int x = 0; x < trans.length; x++) {
			if (trans[x] == 0) {
				z = x; x = trans.length; //position of empty space in array and exits out of the for loop. we don't need to know what is in the rest of the array.
			}
		}
		return z; // returns position or -1
	}

	// moves values in trans array
	private static void moveTrans(int[] trans) {
		for (int x = 0; x < trans.length; x++) {
			if (x == trans.length - 1) { // checks to see if we are at the final spot in the array
				trans[x] = 0;
			} else {
				trans[x] = trans[x + 1]; // takes the nest value in the array and moves it one step.
			}
		}
	}

	// prints the menu and gets an input
	public static int menu() {
		System.out.println("1. Deposit\n2. Withdrawl\n3. Balance\n4. Exit\n");
		int menu = input("Your choice: ");
		if (menu == 1 || menu == 2 || menu == 3 || menu == 4) { //verifies menuchoice
			return menu;
		} else {
			return -1;
		}
	}

}
