package edu.nyu.cs9053.concurrency.chapter14.bank;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * User: blangel
 * Date: 7/26/14
 * Time: 11:00 AM
 */
public class Account {

    private final String name;

    private final AtomicInteger money;

    public Account(String name, int money) {
        this.name = name;
        this.money = new AtomicInteger(money);
    }

    void increment(int amount) {
        money.addAndGet(amount);
    }

    // method not thread safe!
    void decrement(int amount) {
        if (money.get() >= amount) {
            money.addAndGet(-amount);
        } else {
            throw new IllegalArgumentException("Insufficient funds");
        }
    }

    public String getName() {
        return name;
    }

}
