/// <summary> /// Result is how much deposit was really returned. Checks inside. Just wouldn't give money if can't /// Can return less than was prompted /// </summary> public MoneyView ReturnDeposit(Agent toWhom, MoneyView howMuchWants) { if (toWhom.Country.Science.IsInvented(Invention.Banking))// find money in bank? //todo remove checks, make bank==null if uninvented { var maxReturnLimit = HowMuchDepositCanReturn(toWhom); if (maxReturnLimit.isBiggerOrEqual(howMuchWants)) { MoneyView returnMoney; if (howMuchWants.isBiggerThan(maxReturnLimit)) { returnMoney = maxReturnLimit; } else { returnMoney = howMuchWants; } if (returnMoney.isNotZero())// return deposit { //giveMoney(toWhom, moneyToReturn); toWhom.deposits.Subtract(returnMoney); PayWithoutRecord(toWhom, returnMoney, Register.Account.BankOperation); } return(returnMoney); } } return(MoneyView.Zero); }
/// <summary> /// Gives money in credit or returns deposit, if possible. /// Gives whole sum or gives nothing. /// Checks inside. Return false if didn't give credit. /// </summary> public bool GiveCredit(Agent taker, MoneyView desiredCredit) // todo check { if (taker.deposits.isNotZero()) // has deposit (meaning, has no loans) { if (desiredCredit.isBiggerThan(taker.deposits)) // loan is bigger than this deposit { MoneyView returnedDeposit = ReturnDeposit(taker, taker.deposits); if (returnedDeposit.isSmallerThan(taker.deposits)) { return(false);// if can't return deposit than can't give credit for sure } //returnedMoney = new ReadOnlyValue(0f); MoneyView restOfTheSum = desiredCredit.Copy().Subtract(returnedDeposit); if (CanGiveCredit(taker, restOfTheSum)) { taker.loans.Set(restOfTheSum);//important givenCredits.Add(restOfTheSum); PayWithoutRecord(taker, restOfTheSum, Register.Account.BankOperation); return(true); } else { return(false); } } else // no need for credit, just return deposit { // if can't return deposit than can't give credit for sure if (CanReturnDeposit(taker, desiredCredit)) { ReturnDeposit(taker, desiredCredit); return(true); } else { return(false); } } } else { if (CanGiveCredit(taker, desiredCredit)) { taker.loans.Add(desiredCredit); givenCredits.Add(desiredCredit); PayWithoutRecord(taker, desiredCredit, Register.Account.BankOperation); return(true); } else { return(false); } } }