diff --git a/.gitignore b/.gitignore index 13275f1..7709730 100644 --- a/.gitignore +++ b/.gitignore @@ -27,4 +27,8 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +/target/ +.idea/ +*.iml \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 26d3352..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 6f29fee..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 38130cf..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/EasyMoney.iml b/EasyMoney.iml deleted file mode 100644 index c90834f..0000000 --- a/EasyMoney.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..100ad54 --- /dev/null +++ b/pom.xml @@ -0,0 +1,36 @@ + + + + 4.0.0 + + net.sytes.kashey + easy-money + 1.0-SNAPSHOT + + + 21 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter + 5.11.4 + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.5.2 + + + + + \ No newline at end of file diff --git a/src/Main.java b/src/main/java/Main.java similarity index 100% rename from src/Main.java rename to src/main/java/Main.java diff --git a/src/model/Account.java b/src/main/java/model/Account.java similarity index 100% rename from src/model/Account.java rename to src/main/java/model/Account.java diff --git a/src/model/Category.java b/src/main/java/model/Category.java similarity index 100% rename from src/model/Category.java rename to src/main/java/model/Category.java diff --git a/src/model/LedgerEntry.java b/src/main/java/model/LedgerEntry.java similarity index 100% rename from src/model/LedgerEntry.java rename to src/main/java/model/LedgerEntry.java diff --git a/src/model/Operation.java b/src/main/java/model/Operation.java similarity index 100% rename from src/model/Operation.java rename to src/main/java/model/Operation.java diff --git a/src/model/OperationJournal.java b/src/main/java/model/OperationJournal.java similarity index 100% rename from src/model/OperationJournal.java rename to src/main/java/model/OperationJournal.java diff --git a/src/model/OperationLine.java b/src/main/java/model/OperationLine.java similarity index 100% rename from src/model/OperationLine.java rename to src/main/java/model/OperationLine.java diff --git a/src/model/OperationType.java b/src/main/java/model/OperationType.java similarity index 100% rename from src/model/OperationType.java rename to src/main/java/model/OperationType.java diff --git a/src/service/Ledger.java b/src/main/java/service/Ledger.java similarity index 100% rename from src/service/Ledger.java rename to src/main/java/service/Ledger.java diff --git a/src/service/OperationService.java b/src/main/java/service/OperationService.java similarity index 100% rename from src/service/OperationService.java rename to src/main/java/service/OperationService.java diff --git a/src/test/java/service/LedgerTest.java b/src/test/java/service/LedgerTest.java new file mode 100644 index 0000000..e1d0e07 --- /dev/null +++ b/src/test/java/service/LedgerTest.java @@ -0,0 +1,262 @@ +package service; + +import model.Account; +import model.Category; +import model.Operation; +import model.OperationLine; +import org.junit.jupiter.api.Test; + +import java.math.BigDecimal; +import java.time.LocalDate; + +import static org.junit.jupiter.api.Assertions.*; + +class LedgerTest { + + @Test + void openingBalanceSetsAccountBalance() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + Operation operation = Operation.openingBalance(date, 101L, card, new BigDecimal(15000)); + + // When + ledger.post(operation); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(15000).compareTo(actual), + "Остаток счёта должен совпадать с начальным остатком"); + } + + @Test + void incomeIncreasesAccountBalance() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + + Operation income = Operation.income(date, 102L, card); + OperationLine operationLine1 = new OperationLine(new Category(1L, "Salary"), new BigDecimal(2000)); + income.addLine(operationLine1); + OperationLine operationLine2 = new OperationLine(new Category(2L, "Present"), new BigDecimal(500)); + income.addLine(operationLine2); + + // When + ledger.post(income); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(17500).compareTo(actual), + "Остаток счёта должен увеличиться на сумму документа прихода"); + } + + @Test + void expenseDecreasesAccountBalance() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + + Operation expense = Operation.expense(date, 102L, card); + OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100)); + expense.addLine(operationLine1); + OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200)); + expense.addLine(operationLine2); + + // When + ledger.post(expense); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(14700).compareTo(actual), + "Остаток счёта должен уменьшиться на сумму документа расхода"); + } + + @Test + void transferMovesAmountBetweenAccounts() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + Account cash = new Account(2L, "Наличные"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + ledger.post(Operation.openingBalance(date, 102L, cash, new BigDecimal(5000))); + + Operation transfer = Operation.transfer(date, 103L, card, cash, new BigDecimal(3000)); + + // When + ledger.post(transfer); + + // Then + BigDecimal actualCash = ledger.balance(cash, date); + BigDecimal actualCard = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(8000).compareTo(actualCash), + "Остаток наличных должен увеличиться на сумму документа перемещения"); + assertEquals(0, new BigDecimal(12000).compareTo(actualCard), + "Остаток на карте должен уменьшиться на сумму документа перемещения"); + } + + @Test + void postingSameOpeningBalanceDoesNotChangeBalanceAgain() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + Operation openingBalance = Operation.openingBalance(date, 101L, card, new BigDecimal(15000)); + + // When + ledger.post(openingBalance); + ledger.post(openingBalance); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(15000).compareTo(actual), + "Остаток счёта должен совпадать с начальным остатком независимо от количества проведений документа"); + } + + @Test + void postingSameIncomeTwiceDoesNotChangeBalanceAgain() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + + Operation income = Operation.income(date, 102L, card); + OperationLine operationLine1 = new OperationLine(new Category(1L, "Salary"), new BigDecimal(2000)); + income.addLine(operationLine1); + OperationLine operationLine2 = new OperationLine(new Category(2L, "Present"), new BigDecimal(500)); + income.addLine(operationLine2); + + // When + ledger.post(income); + ledger.post(income); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(17500).compareTo(actual), + "Остаток счёта должен увеличиться на сумму документа прихода единожды"); + } + + @Test + void postingSameExpenseTwiceDoesNotChangeBalanceAgain() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + + Operation expense = Operation.expense(date, 102L, card); + OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100)); + expense.addLine(operationLine1); + OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200)); + expense.addLine(operationLine2); + + // When + ledger.post(expense); + ledger.post(expense); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(14700).compareTo(actual), + "Остаток счёта должен уменьшиться на сумму документа расхода единожды"); + } + + @Test + void postingSameTransferTwiceDoesNotChangeBalanceAgain() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + Account cash = new Account(2L, "Наличные"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + ledger.post(Operation.openingBalance(date, 102L, cash, new BigDecimal(5000))); + + Operation transfer = Operation.transfer(date, 103L, card, cash, new BigDecimal(3000)); + + // When + ledger.post(transfer); + ledger.post(transfer); + + // Then + BigDecimal actualCash = ledger.balance(cash, date); + BigDecimal actualCard = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(8000).compareTo(actualCash), + "Остаток наличных должен увеличиться на сумму документа перемещения единожды"); + assertEquals(0, new BigDecimal(12000).compareTo(actualCard), + "Остаток на карте должен уменьшиться на сумму документа перемещения единожды"); + } + + @Test + void changingLineAmountDoesNotAffectBalanceBeforeReposting() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + + Operation expense = Operation.expense(date, 102L, card); + OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100)); + expense.addLine(operationLine1); + OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200)); + expense.addLine(operationLine2); + ledger.post(expense); + + // When + expense.replaceLine(1, new OperationLine(new Category(2L, "Pizza"), new BigDecimal(250))); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(14700).compareTo(actual), + "Остаток счёта не меняется если изменённый документ не проведён"); + } + + @Test + void repostingOperationAfterLineAmountChangeUpdatesBalance() { + // Given + Ledger ledger = new Ledger(); + Account card = new Account(1L, "Карта"); + LocalDate date = LocalDate.of(2026, 10, 18); + + ledger.post(Operation.openingBalance(date, 101L, card, new BigDecimal(15000))); + + Operation expense = Operation.expense(date, 102L, card); + OperationLine operationLine1 = new OperationLine(new Category(1L, "Mineral water"), new BigDecimal(100)); + expense.addLine(operationLine1); + OperationLine operationLine2 = new OperationLine(new Category(2L, "Pizza"), new BigDecimal(200)); + expense.addLine(operationLine2); + ledger.post(expense); + expense.replaceLine(1, new OperationLine(new Category(2L, "Pizza"), new BigDecimal(250))); + + // When + ledger.post(expense); + + // Then + BigDecimal actual = ledger.balance(card, date); + + assertEquals(0, new BigDecimal(14650).compareTo(actual), + "Остаток счёта меняется в соответствии с новыми значениями в изменённом и проведённом документе"); + } +} \ No newline at end of file