From 35687c40f7c8c401cbc9a2ec87717617f02cb4c6 Mon Sep 17 00:00:00 2001 From: Reviem Date: Mon, 16 Sep 2024 23:00:03 +0100 Subject: [PATCH] Final --- .gitignore | 1 + Cargo.lock | 289 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 ++ src/args.rs | 135 +++++++++++++++++++++ src/budget.rs | 105 ++++++++++++++++ src/file_manager.rs | 47 +++++++ src/main.rs | 25 ++++ src/manager.rs | 160 ++++++++++++++++++++++++ src/transaction.rs | 54 +++++++++ 9 files changed, 825 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/args.rs create mode 100644 src/budget.rs create mode 100644 src/file_manager.rs create mode 100644 src/main.rs create mode 100644 src/manager.rs create mode 100644 src/transaction.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..4804620 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,289 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "budget-manager" +version = "0.1.0" +dependencies = [ + "clap", + "serde", + "serde_json", +] + +[[package]] +name = "clap" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501d359d5f3dcaf6ecdeee48833ae73ec6e42723a1e52419c79abf9507eec0a0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.210" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.76" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cfb9a14 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "budget-manager" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5.17", features = ["derive"] } +serde = { version = "1.0.210", features = ["derive"] } +serde_json = "1.0.128" diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..b11911d --- /dev/null +++ b/src/args.rs @@ -0,0 +1,135 @@ +use clap::{ + Args, Parser, Subcommand +}; + +#[derive(Parser,Debug)] +#[clap(author, version, about)] +pub struct CLIArgs{ + #[clap(subcommand)] + pub entity_type: EntityType, +} + +#[derive(Debug, Subcommand)] +pub enum EntityType { + /// Add, remove, edit, display a budget + Budget(BudgetCommand), + + // Add, remove, edit transactions associated with a budget + Transaction(TransactionCommand) +} + +#[derive(Debug, Args)] +pub struct BudgetCommand { + #[clap(subcommand)] + pub command: BudgetSubCommand, +} + +#[derive(Debug, Subcommand)] +pub enum BudgetSubCommand { + /// Create a Budget + Create(CreateBudget), + + /// Update a Budget + Update(UpdateBudget), + + /// Delete a Budget + Delete(DeleteBudget), + + /// Display a Budget + Display(DisplayBudget), + + /// List all budgets + List, +} + +#[derive(Debug, Args)] +pub struct CreateBudget { + /// Name of the Budget + pub budget_name: String, + + /// Total Amount in Budget + pub budget_amount: f32 +} + +#[derive(Debug, Args)] +pub struct UpdateBudget{ + /// Name of the Budget + pub budget_id: u32, + + /// Total Amount in Budget + pub budget_amount: f32 +} + +#[derive(Debug, Args)] +pub struct DeleteBudget{ + /// Name of the Budget + pub budget_id: u32, +} + +#[derive(Debug, Args)] +pub struct DisplayBudget{ + /// Name of the Budget + pub budget_id: u32, +} + +#[derive(Debug, Args)] +pub struct TransactionCommand { + #[clap(subcommand)] + pub command: TransactionSubCommand, +} + +#[derive(Debug, Subcommand)] +pub enum TransactionSubCommand{ + /// Create a Transaction + Create(CreateTransaction), + + /// Deposit Amount + Deposit(DepositTransaction), + + /// Update a Transaction + Update(UpdateTransaction), + + /// Delete a Transaction + Delete(DeleteTransaction), + +} + +#[derive(Debug, Args)] +pub struct CreateTransaction { + /// Name of the Budget + pub budget_id: u32, + + /// Total Amount in Budget + pub amount: f32 +} + +#[derive(Debug, Args)] +pub struct DepositTransaction { + /// Name of the Budget + pub budget_id: u32, + + /// Total Amount of the Transaction + pub amount: f32 +} + +#[derive(Debug, Args)] +pub struct UpdateTransaction{ + /// Name of the Budget + pub budget_id: u32, + + /// Unique ID of the Transaction + pub transaction_id: u32, + + // Total Amount of the Transaction + pub amount: f32 + +} + +#[derive(Debug, Args)] +pub struct DeleteTransaction{ + /// Name of the Budget + pub budget_id: u32, + + /// Unique ID of the Transaction + pub transaction_id: u32 +} diff --git a/src/budget.rs b/src/budget.rs new file mode 100644 index 0000000..87c85d4 --- /dev/null +++ b/src/budget.rs @@ -0,0 +1,105 @@ +use crate::transaction::{Transaction, TransactionType}; + +use std::collections::HashMap; +use serde::{Deserialize, Serialize}; + + +#[derive(Serialize, Deserialize)] +pub struct Budget { + id: u32, + name: String, + budget: f32, + transaction_list: HashMap +} + +impl Budget { + pub fn new(new_name: &String, new_budget: f32, new_id: u32) -> Budget { + Self { + id: new_id, + name: new_name.to_owned(), + budget: new_budget, + transaction_list: HashMap::new() + } + } + + pub fn get_id(&self) -> u32 { + self.id + } + + pub fn get_name(&self) -> &String { + &self.name + } + + pub fn get_budget(&self) -> f32 { + self.budget + } + + pub fn get_transaction(&mut self, id: u32) -> Option<&mut Transaction> { + self.transaction_list.get_mut(&id) + } + + pub fn update(&mut self, new_budget: f32) { + self.budget = new_budget; + } + + pub fn add_transaction(&mut self, t: Transaction) { + let count: u32 = self.transaction_list.len().try_into().unwrap(); + self.transaction_list.insert(count, t); + + } + + pub fn display(&self) { + println!("id: {} name: {} budget: {}", self.id, self.name, self.budget); + println!("Transactions:"); + println!( + "{0: <10} | {1: <10} | {2: <13} | {3: <10}", + "id", "amount", "Type", "date", + ); + println!( + "{0: <10} | {1: <10} | {2: <13} | {3: <10}", + "-", "-", "-", "-" + ); + + // Collect transactions into a vector and sort by index + let mut transactions: Vec<_> = self.transaction_list.iter().collect(); + transactions.sort_by_key(|&(index, _)| index); + + for (index, transaction) in transactions { + match transaction.amount() { + TransactionType::Transaction(amount) => { + println!("{0: <10} | {1: <10} | {2: <13} | {3: <10}", + index, amount, "Transaction", transaction.date().elapsed().unwrap().as_secs()); + } + TransactionType::Deposit(amount) => { + println!("{0: <10} | {1: <10} | {2: <13} | {3: <10}", + index, amount, "Deposit", transaction.date().elapsed().unwrap().as_secs()); + } + } + } + } + + pub fn calculate_total(&self) -> f32 { + let mut total: f32 = self.budget; + for (_, transaction) in self.transaction_list.iter() { + match transaction.amount() { + TransactionType::Transaction(amount) => { + total -= amount + } + TransactionType::Deposit(amount) => { + total+= amount + } + } + } + total + } + + pub fn delete_transaction(&mut self, id: u32) { + match self.transaction_list.get(&id) { + Some(_) => { + self.transaction_list.remove(&id); + }, + None => println!("Invalid ID") + } + } + +} \ No newline at end of file diff --git a/src/file_manager.rs b/src/file_manager.rs new file mode 100644 index 0000000..4db9441 --- /dev/null +++ b/src/file_manager.rs @@ -0,0 +1,47 @@ +use crate::manager::BudgetManager; +use core::panic; +use std::{fs::File, io::{BufReader, Write}}; + + + +pub struct FileManager { + save_path: String +} + +impl FileManager { + pub fn new(path: String) -> FileManager { + Self { + save_path: path + } + } + + pub fn save(&self, budget_manager: &BudgetManager) { + let serialized = serde_json::to_string(budget_manager).unwrap(); + let mut file = File::create(&self.save_path).unwrap(); + file.write_all(serialized.as_bytes()).unwrap(); + } + + pub fn load(&self) -> BudgetManager { + match File::open(&self.save_path) { + Ok(file) => { + let reader = BufReader::new(file); + let budget_manager = serde_json::from_reader(reader); + match budget_manager { + Ok(budget_manager) => budget_manager, + Err(_) => { + println!("Error: Could not read file, it may be corrupted"); + println!("Would you like to create a new file? (y/n)"); + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + if input.trim() == "y" { + BudgetManager::new() + } else { + panic!("Exiting program") + } + } + } + }, + Err(_) => BudgetManager::new() + } + } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2425d87 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,25 @@ +mod args; +mod budget; +mod manager; +mod transaction; +mod file_manager; + +use args::CLIArgs; +use clap::Parser; +use file_manager::FileManager; + + +fn main() { + let args = CLIArgs::parse(); + + let file_manager = FileManager::new("budget.json".to_string()); + let mut budgetmanager = file_manager.load(); + + match &args.entity_type { + args::EntityType::Budget(command) => budgetmanager.budget_commands(command), + + args::EntityType::Transaction(command) => budgetmanager.transaction_commands(command), + } + + file_manager.save(&budgetmanager); +} \ No newline at end of file diff --git a/src/manager.rs b/src/manager.rs new file mode 100644 index 0000000..5bd88f7 --- /dev/null +++ b/src/manager.rs @@ -0,0 +1,160 @@ +use crate::{ + args::{self, BudgetCommand, TransactionCommand}, + transaction::{Transaction, TransactionType}, + budget::Budget +}; + +use std::collections::HashMap; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize)] +pub struct BudgetManager { + budget_list: HashMap, + count: u32 +} + +impl BudgetManager { + pub fn new() -> BudgetManager { + Self { + budget_list: HashMap::new(), + count: 0 + } + } + + pub fn get_budget(&mut self, id: u32) -> Option<&mut Budget> { + self.budget_list.get_mut(&id) + } + + + fn create_budget(&mut self, new_name: &String, new_budget: f32) { + if self.budget_list.contains_key(&self.count) { + self.budget_list.iter().count(); + } + self.budget_list.insert(self.count, Budget::new(new_name, new_budget, self.count)); + self.count += 1; + } + + fn list_budgets(&self) { + println!( + "{0: <10} | {1: <10} | {2: <10} | {3: <10}", + "id", "name", "budget", "%Δ" + ); + println!( + "{0: <10} | {1: <10} | {2: <10} | {3: <10}", + "-", "-", "-", "-" + ); + + // Collect budgets into a vector and sort by index + let mut budgets: Vec<_> = self.budget_list.iter().collect(); + budgets.sort_by_key(|&(index, _)| index); + + for (_, budget) in budgets { + let total = budget.calculate_total(); + println!( + "{0: <10} | {1: <10} | {2: <10} | {3: <10}", + budget.get_id(), + budget.get_name(), + total, + ((total / budget.get_budget()) * 100.0).round() as i32 + ); + } + } + + fn update_budget(&mut self, id: u32, new_budget: f32) { + match self.budget_list.get_mut(&id) { + Some(budget) => budget.update(new_budget), + None => println!("Invalid id {}", id) + } + } + + fn delete_budget(&mut self, id:u32) { + match self.budget_list.get_mut(&id) { + Some(_) => { + self.budget_list.remove(&id); + }, + None => println!("Invalid id {}", id) + } + } + + pub fn add_transaction(&mut self, id:u32, transaction: Transaction) { + match self.budget_list.get_mut(&id) { + Some(budget) => budget.add_transaction(transaction), + None => println!("Erorr: Invalid ID"), + } + } + + pub fn display_budget(&self, id: u32) { + match self.budget_list.get(&id) { + Some(budget) => budget.display(), + None => println!("Error: Invalid ID"), + } + } + + pub fn create_transaction(&mut self, id: u32, amount: f32) { + let transaction = Transaction::new(id, TransactionType::Transaction(amount)); + self.add_transaction(id, transaction); + } + + pub fn place_deposit(&mut self, id: u32, amount: f32) { + let transaction = Transaction::new(id, TransactionType::Deposit(amount)); + self.add_transaction(id, transaction); + } + + pub fn update_transaction(&mut self, id: u32, transaction_id: u32, new_amount: f32) { + match self.get_budget(id) { + Some(budget) => { + match budget.get_transaction(transaction_id) { + Some(transaction) => transaction.update(new_amount), + None => println!("Invalid transaction id {}", transaction_id) + } + }, + None => println!("Invalid budget id {}", id) + } + } + + pub fn delete_transaction(&mut self, id:u32, transaction_id:u32) { + match self.get_budget(id) { + Some(budget) => { + budget.delete_transaction(transaction_id) + }, + None => println!("Invalid budget id {}", id) + } + } + + + pub fn budget_commands(&mut self,budget_command: &BudgetCommand ) { + match &budget_command.command { + args::BudgetSubCommand::Create(command) => { + println!("Creating Budget with name {} and budget {}", command.budget_name, command.budget_amount); + self.create_budget(&command.budget_name, command.budget_amount); + self.list_budgets(); + } + args::BudgetSubCommand::Update(command) => { + self.update_budget(command.budget_id, command.budget_amount) + }, + args::BudgetSubCommand::Delete(command) => { + self.delete_budget(command.budget_id) + }, + args::BudgetSubCommand::Display(command) => { + self.display_budget(command.budget_id) + }, + args::BudgetSubCommand::List => self.list_budgets(), + } + } + + pub fn transaction_commands(&mut self, transaction_command: &TransactionCommand) { + match &transaction_command.command { + args::TransactionSubCommand::Create(command) => { + self.create_transaction(command.budget_id, command.amount); + } + + args::TransactionSubCommand::Update(command) => { + self.update_transaction(command.budget_id, command.transaction_id, command.amount) + }, + args::TransactionSubCommand::Delete(command) => self.delete_transaction(command.budget_id, command.transaction_id), + args::TransactionSubCommand::Deposit(command) => { + self.place_deposit(command.budget_id ,command.amount) + }, + } + } +} \ No newline at end of file diff --git a/src/transaction.rs b/src/transaction.rs new file mode 100644 index 0000000..790a03d --- /dev/null +++ b/src/transaction.rs @@ -0,0 +1,54 @@ +#![allow(unused)] +use std::time::{SystemTime, UNIX_EPOCH}; +use serde::{Deserialize, Serialize}; + +use crate::manager::{self, BudgetManager}; + +#[derive(Serialize, Deserialize)] +pub enum TransactionType { + Transaction(f32), + Deposit(f32) +} + +#[derive(Serialize, Deserialize)] +pub struct Transaction { + budget_id: u32, + transaction_id: u32, + amount: TransactionType, + date: SystemTime +} + +impl Transaction { + pub fn new(new_id: u32, new_amount: TransactionType) -> Transaction { + Self { + budget_id: new_id, + transaction_id: 0, + amount: new_amount, + date: SystemTime::now() + } + } + + pub fn transaction_id(&self) -> u32 { + self.transaction_id + } + + pub fn amount(&self) -> &TransactionType { + &self.amount + } + + pub fn date(&self) -> SystemTime { + self.date + } + + pub fn update(&mut self, new_amount: f32) { + match self.amount { + TransactionType::Transaction(_) => { + self.amount = TransactionType::Transaction(new_amount) + } + + TransactionType::Deposit(_) => { + self.amount = TransactionType::Deposit(new_amount) + } + } + } +} \ No newline at end of file