https://github.com/shivankk26/rust-banking-system
This Rust program implements a basic banking system using Traits. It allows users to create accounts, deposit and withdraw money, and view their account balance.
https://github.com/shivankk26/rust-banking-system
cargo cargo-generate dev git rust rust-lang
Last synced: 3 months ago
JSON representation
This Rust program implements a basic banking system using Traits. It allows users to create accounts, deposit and withdraw money, and view their account balance.
- Host: GitHub
- URL: https://github.com/shivankk26/rust-banking-system
- Owner: ShivankK26
- Created: 2024-01-30T10:03:36.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-31T13:59:02.000Z (over 1 year ago)
- Last Synced: 2025-01-09T06:22:19.386Z (4 months ago)
- Topics: cargo, cargo-generate, dev, git, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rust Banking System
This Rust program implements a basic banking system using Traits. It allows users to create accounts, deposit and withdraw money, and view their account balance.
## Overview
The program consists of the following components:
- **BankAccount Trait**: Defines operations for bank accounts such as deposit, withdraw, and balance.
- **Account Struct**: Represents a bank account with fields for account number and balance.
- **Implementation**: Implements the BankAccount trait for the Account struct, providing functionality for depositing, withdrawing, and checking balance.
- **Main Function**: Demonstrates the usage of the Account struct by creating an account, performing deposit and withdrawal operations, and checking the balance.## Code Snippet
```rust
// Define a trait for bank operations
trait BankAccount {
fn deposit(&mut self, amount: f64);
fn withdraw(&mut self, amount: f64) -> Result<(), &'static str>;
fn balance(&self) -> f64;
}// Define a struct to represent a bank account
struct Account {
account_number: u32,
balance: f64,
}// Implement the BankAccount trait for the Account struct
impl BankAccount for Account {
fn deposit(&mut self, amount: f64) {
self.balance += amount;
println!("Deposited {:.2} into account {}. New balance: {:.2}", amount, self.account_number, self.balance);
}fn withdraw(&mut self, amount: f64) -> Result<(), &'static str> {
if self.balance >= amount {
self.balance -= amount;
println!("Withdrawn {:.2} from account {}. New balance: {:.2}", amount, self.account_number, self.balance);
Ok(())
} else {
Err("Insufficient funds")
}
}fn balance(&self) -> f64 {
self.balance
}
}fn main() {
// Create a new account
let mut account = Account {
account_number: 123456,
balance: 1000.0,
};println!("Account {} balance: {:.2}", account.account_number, account.balance());
// Deposit some money
account.deposit(500.0);// Withdraw some money
match account.withdraw(200.0) {
Ok(()) => println!("Withdrawal successful"),
Err(err) => println!("Error: {}", err),
}// Attempt to withdraw more money than available
match account.withdraw(2000.0) {
Ok(()) => println!("Withdrawal successful"),
Err(err) => println!("Error: {}", err),
}// Check balance
println!("Account {} balance: {:.2}", account.account_number, account.balance());
}
```## Usage
To use the program, simply run the provided Rust code. The main function demonstrates the functionality of the banking system by creating an account, depositing and withdrawing money, and checking the account balance.
## Cargo Commands
- **Running the Application**: Use `cargo run` to compile and run the Rust application.
- **Checking for Errors**: Use `cargo check` to check for errors in the code without building the application.
- **Building the Application**: Use `cargo build` to build the Rust application without running it.