From 6085370702550989c0d03cc2e20530d7d474be20 Mon Sep 17 00:00:00 2001 From: Mousaber00 Date: Fri, 11 Apr 2025 17:43:49 +0200 Subject: [PATCH] improve add new transaction implementation --- src/main/kotlin/Main.kt | 4 ---- .../dataSource/FinanceTrackerDataSourceImpl.kt | 18 +++++++++--------- src/main/kotlin/logic/models/Category.kt | 8 ++------ src/main/kotlin/logic/models/Transaction.kt | 16 +++++++--------- .../logic/transactions/AddNewTransaction.kt | 7 +------ src/main/kotlin/testCases/TestCasesA.kt | 4 +--- 6 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index edd75ff..c4739a3 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,10 +1,6 @@ package org.qudus.squad -import org.qudus.squad.testCases.testDeleteCategoryFunction -import org.qudus.squad.testCases.testMonthlyTransactionsCases - - fun main() { /*TODO*/ diff --git a/src/main/kotlin/dataSource/FinanceTrackerDataSourceImpl.kt b/src/main/kotlin/dataSource/FinanceTrackerDataSourceImpl.kt index 8d2e10a..f9ff34b 100644 --- a/src/main/kotlin/dataSource/FinanceTrackerDataSourceImpl.kt +++ b/src/main/kotlin/dataSource/FinanceTrackerDataSourceImpl.kt @@ -7,37 +7,37 @@ import org.qudus.squad.logic.models.TransactionType class FinanceTrackerDataSourceImpl : FinanceTrackerDataSource { - private val categories: MutableList = emptyList().toMutableList() + private val _categories: MutableList = + mutableListOf(Category(name = "Rent") , Category(name = "Salary") , Category(name = "Food")) private val transactions: MutableList = emptyList().toMutableList() + val categories get() = this._categories - val _categories - get() = categories override fun addCategory(category: Category): Boolean { if (category.name.isNotEmpty()) { - categories.add(category) + this._categories.add(category) return true } return false } override fun removeCategory(categoryId: Int): Boolean { - return categories.removeIf { + return this._categories.removeIf { it.id == categoryId } } override fun getCategories(): List { - return categories + return this._categories } override fun getCategoryById(categoryId: Int): Category? { - return categories.find { it.id == categoryId } + return this._categories.find { it.id == categoryId } } override fun updateCategory(category: Category): Boolean { - val index = categories.indexOfFirst { it.id == category.id } + val index = this._categories.indexOfFirst { it.id == category.id } return if (index != -1) { - categories[index] = category + this._categories[index] = category true } else { false diff --git a/src/main/kotlin/logic/models/Category.kt b/src/main/kotlin/logic/models/Category.kt index c45b644..be4e1a1 100644 --- a/src/main/kotlin/logic/models/Category.kt +++ b/src/main/kotlin/logic/models/Category.kt @@ -2,14 +2,10 @@ package org.qudus.squad.logic.models data class Category( val id: Int = CategoryIdGenerator.nextId(), - var name: String - + val name: String ) - object CategoryIdGenerator { private var counter = 0 fun nextId(): Int = ++counter -} - - +} \ No newline at end of file diff --git a/src/main/kotlin/logic/models/Transaction.kt b/src/main/kotlin/logic/models/Transaction.kt index 4aa1ed1..4fb7f3c 100644 --- a/src/main/kotlin/logic/models/Transaction.kt +++ b/src/main/kotlin/logic/models/Transaction.kt @@ -1,11 +1,11 @@ package org.qudus.squad.logic.models data class Transaction( - val id: Int? = TransactionIdGenerator.nextId(), - var type: TransactionType, - var amount: Double, - var timeStamp: Long, - var category: Category + val id: Int = TransactionIdGenerator.nextId(), //not nullable + val type: TransactionType, + val amount: Double, + val timeStamp: Long, + val category: Category ) object TransactionIdGenerator { @@ -14,7 +14,5 @@ object TransactionIdGenerator { } enum class TransactionType { - Deposit , Withdraw -} - - + Deposit, Withdraw +} \ No newline at end of file diff --git a/src/main/kotlin/logic/transactions/AddNewTransaction.kt b/src/main/kotlin/logic/transactions/AddNewTransaction.kt index 6a65416..a24f457 100644 --- a/src/main/kotlin/logic/transactions/AddNewTransaction.kt +++ b/src/main/kotlin/logic/transactions/AddNewTransaction.kt @@ -7,17 +7,12 @@ import org.qudus.squad.logic.models.Transaction class AddNewTransaction( private val dataSource: FinanceTrackerDataSource ) { - - - - - fun CompleteTransaction(transaction: Transaction) { + fun addNewTransaction(transaction: Transaction) { if (isAmountValid(transaction)) { dataSource.addTransaction(transaction) } } fun isAmountValid(transaction: Transaction): Boolean = transaction.amount < 0 - } diff --git a/src/main/kotlin/testCases/TestCasesA.kt b/src/main/kotlin/testCases/TestCasesA.kt index 3c545f3..50fb33b 100644 --- a/src/main/kotlin/testCases/TestCasesA.kt +++ b/src/main/kotlin/testCases/TestCasesA.kt @@ -1,6 +1,4 @@ -/* -package org.qudus.squad.testCases - +package org.qudus.squad.testCases/* fun main() { */