Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aziz Emir ~ Enhance Bank System with Modular Structure, User Management, and Transfer Functionality #3

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.vscode
41 changes: 41 additions & 0 deletions src/bank.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// src/bank.rs

use crate::transaction::Transaction;
use crate::user::User;

#[derive(Clone)] // Derive Clone trait
pub struct Bank {
pub users: Vec<User>,
pub balance: u128,
pub fee: u8,
pub transactions: Vec<Transaction>,
}

impl Bank {
pub fn new(fee: u8) -> Self {
Self {
users: Vec::new(),
balance: 0,
fee,
transactions: Vec::new(),
}
}

// Method to add a user to the bank
pub fn add_user(&mut self, user: User) {
self.users.push(user);
}

// Method to list all users in the bank
pub fn list_users(&self) -> Vec<String> {
self.users
.iter()
.map(|user| {
format!(
"Name: {}, Account ID: {}, Balance: {}",
user.name, user.account_id, user.balance
)
})
.collect()
}
}
76 changes: 47 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,56 @@
struct Bank {
users: Vec<User>,
safe: u128,
fee: u8,
transactions: Vec<Transaction>,
}
// src/main.rs

mod bank;
mod transaction;
mod user;

use bank::Bank;
use transaction::Transaction;
use user::User;

struct User {
name: String,
account_id: String,
balance: u128,
struct PaymentProcessor {
banks: Vec<Bank>,
}

impl User {
pub fn new(name: String, account_id: String, balance: u128) -> Self {
Self {
name,
account_id,
balance,
}
impl PaymentProcessor {
pub fn new() -> Self {
Self { banks: Vec::new() }
}
pub fn add_balance(&mut self, amount: u32) {
self.balance = self.balance + amount as u128;

pub fn add_bank(&mut self, bank: Bank) {
self.banks.push(bank);
}
}

struct Transaction {
sender: User,
receiver: User,
amount: u32,
bank: Bank,
}
fn main() {
let mut processor = PaymentProcessor::new();
let mut aziz_bank = Bank::new(1);
processor.add_bank(aziz_bank.clone()); // Use clone to avoid moving

struct PaymentProcessor {
banks: Vec<Bank>,
}
println!("Bank added to the payment processor.");

// Create users
let mut alice = User::new("Alice".to_string(), "alice123".to_string(), 1000);
let mut bob = User::new("Bob".to_string(), "bob123".to_string(), 500);

// Add users to the bank
aziz_bank.add_user(alice.clone());
aziz_bank.add_user(bob.clone());

// List users in the bank
println!("-------start USER LIST-------");
for user_info in aziz_bank.list_users() {
println!("{}", user_info);
}
println!("-------end USER LIST-------");

// Perform a transfer
match alice.transfer(&mut bob, 200) {
Ok(_) => println!("Transfer successful!"),
Err(err) => println!("Transfer failed: {}", err),
}

fn main() {}
// Print the balances
println!("Alice's balance: {}", alice.balance);
println!("Bob's balance: {}", bob.balance);
}
12 changes: 12 additions & 0 deletions src/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// src/transaction.rs

use crate::bank::Bank;
use crate::user::User;

#[derive(Clone)]
pub struct Transaction {
pub sender: User,
pub receiver: User,
pub amount: u32,
pub bank: Bank,
}
31 changes: 31 additions & 0 deletions src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// src/user.rs

#[derive(Clone)]
pub struct User {
pub name: String,
pub account_id: String,
pub balance: u128,
}

impl User {
pub fn new(name: String, account_id: String, balance: u128) -> Self {
Self {
name,
account_id,
balance,
}
}

pub fn add_balance(&mut self, amount: u32) {
self.balance += amount as u128;
}

pub fn transfer(&mut self, receiver: &mut User, amount: u32) -> Result<(), &'static str> {
if amount as u128 > self.balance {
return Err("Insufficient balance");
}
self.balance -= amount as u128;
receiver.add_balance(amount);
Ok(())
}
}