/**
 * This class represents a bank account whose current balance is a nonnegative
 * amount in US dollars.
 */
public class Account {

    private int balance;
    Account parentAccount;

    /**
     * Initialize an account with the given balance.
     */
    public Account(int balance) {
        this.balance = balance;
        this.parentAccount = null;
    }

    public Account(int balance1, Account parentAccount) {

        balance1 = balance;
        this.parentAccount = parentAccount;
    }

    /**
     * Returns the balance for the current account.
     */
    public int getBalance() {
        return this.balance;
    }

    /**
     * Deposits amount into the current account.
     */
    public void deposit(int amount) {
        if (amount < 0) {
            System.out.println("Cannot deposit negative amount.");
        } else {
            balance += amount;
        }
    }

    /**
     * Subtract amount from the account if possible. If subtracting amount
     * would leave a negative balance, print an error message and leave the
     * balance unchanged.
     */
    public boolean withdraw(int amount) {

        if (amount < 0) {
            if (parentAccount.getBalance() < 0) {
                System.out.println("Cannot withdraw negative amount.");

            }
            if (parentAccount.getBalance() < amount) {
                System.out.println("Insufficient funds");

            }
            if (parentAccount.getBalance() >= amount) {
                balance -= amount;

            }
        }
        if ((balance < amount) && (amount >= 0))
            if (parentAccount.getBalance() < 0) {
                System.out.println("Cannot withdraw negative amount.");

            }
        if (parentAccount.getBalance() < amount) {
            System.out.println("Insufficient funds");
        }
        if (parentAccount.getBalance() >= amount) {
            balance -= amount;

        }
        if ((balance >= amount) && (amount >= 0)) {
            balance -= amount;

        }
        return true;
    }

    /**
     * Merge account other into this account by removing all money from other
     * and depositing it into this account.
     */
    public void merge(Account other) {

        other.balance = balance;
        balance = 0;
    }

    public static void main(String[] args) {

        Account zephyr = new Account(500);
        Account max = new Account(100, zephyr);
        max.withdraw(50);
    }
}