Created
March 3, 2014 04:30
-
-
Save Vox1oot/9318393 to your computer and use it in GitHub Desktop.
com.javarush.test.level17.lesson10.home08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level17.lesson10.home08; | |
import java.math.BigDecimal; | |
public class BankAccount { | |
private BigDecimal balance; | |
private String owner; | |
public BankAccount(String owner) { | |
this(BigDecimal.ZERO, owner); | |
} | |
public BankAccount(BigDecimal balance, String owner) { | |
this.balance = balance; | |
this.owner = owner; | |
} | |
public synchronized void deposit(BigDecimal money) { | |
BigDecimal newBalance = balance.add(money); | |
System.out.println("Добавляем " + money + ", на счету " + newBalance); | |
balance = newBalance; | |
} | |
public synchronized void withdraw(BigDecimal money) throws NotEnoughMoneyException { | |
BigDecimal newBalance = balance.subtract(money); | |
if (newBalance.compareTo(BigDecimal.ZERO) < 0) throw new NotEnoughMoneyException(); | |
balance = newBalance; | |
System.out.println("Тратим " + money + ", на счету " + balance); | |
} | |
public void deposit(String money) { | |
deposit(new BigDecimal(money)); | |
} | |
public void withdraw(String money) throws NotEnoughMoneyException { | |
withdraw(new BigDecimal(money)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level17.lesson10.home08; | |
/* Банкомат | |
Разберись, как работает программа | |
Во время тестирования лог содержит следующее: | |
..... | |
Добавляем 100, на счету 1100 | |
Добавляем 100, на счету 1200 | |
Тратим 1000, на счету 100 | |
Недостаточно денег | |
..... | |
Создан баг: При списании денег со счета теряются деньги | |
Найти и исправить ошибку | |
*/ | |
public class Bankomat { | |
static BankAccount account = new BankAccount("Amigo"); | |
public static volatile boolean isStopped; | |
public static void main(String[] args) throws InterruptedException { | |
addMoney.start(); | |
new SpendThread(); | |
new SpendThread(); | |
new SpendThread(); | |
Thread.sleep(4000); | |
isStopped = true; | |
} | |
private static Thread addMoney = new Thread() { | |
@Override | |
public void run() { | |
while (!isStopped) { | |
account.deposit("1000"); //кладем на счет | |
account.deposit("100"); | |
account.deposit("100"); | |
try { | |
Thread.sleep(1000); | |
} catch (InterruptedException e) { | |
break; | |
} | |
} | |
} | |
}; | |
public static class SpendThread extends Thread { | |
public SpendThread() { | |
start(); | |
} | |
@Override | |
public void run() { | |
while (!isStopped) { | |
try { | |
account.withdraw("1000"); //снимаем со счета | |
} catch (NotEnoughMoneyException e) { | |
System.out.println("Недостаточно денег"); | |
} | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
break; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level17.lesson10.home08; | |
public class NotEnoughMoneyException extends Exception { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment