// @topic S-0314-14-01-10 Java Memento Design pattern
// @brief Withdrawals from checking account<br />See also <a href="http://www.c-jump.com/bcc/c260c/c260samples15S/Patterns/memento/memento.png" target="_blank">sequence diagram</a>

package memento;

public class MainApp {

    public static void main(String[] args) {

        // create an object that requires state
        // preservation (aka state originator:):
        CheckingAccount account = new CheckingAccount(100.0);

        // Prepare to save states:
        AccountCaretaker caretaker = new AccountCaretaker();

        // Begin a series of withdrawals:
        caretaker.store(account.createMemento());
        System.out.println("\nAttempting to withdraw $20.0");
        if (account.withdraw(20.0)) {
            System.out.println("Success! New balance: $" + account.getBalance());
        } else {
            caretaker.undo(account);
            System.out.println(
                    "Overdraft! Original balance of $"
                    + account.getBalance()
                    + " is restored.");
            return;
        }

        System.out.println("\nAttempting to withdraw $50.0");
        caretaker.store(account.createMemento());
        if (account.withdraw(50.0)) {
            System.out.println("Success! New balance: $" + account.getBalance());
        } else {
            caretaker.undo(account);
            System.out.println(
                    "Overdraft! Original balance of $"
                    + account.getBalance()
                    + " is restored.");
            return;
        }

        System.out.println("\nAttempting to withdraw $50.0");
        caretaker.store(account.createMemento());
        if (account.withdraw(50.0)) {
            System.out.println("Success! New balance: $" + account.getBalance());
        } else {
            caretaker.undo(account);
            System.out.println(
                    "Overdraft! Original balance of $"
                    + account.getBalance()
                    + " is restored.");
            return;
        }
    }//main

}//class MainApp