Skip to content

Commit

Permalink
Initial update
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramil Mammadov committed Oct 23, 2024
0 parents commit ca094cb
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 0 deletions.
1 change: 1 addition & 0 deletions .devcontainer/.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FROM rust:latest
15 changes: 15 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Sample Rust",
"build": {
"dockerfile": "Dockerfile"
},
"customizations": {
"vscode": {
"extensions": [
"rust-lang.rust-analyzer", // Updated to use rust-analyzer
"ms-azuretools.vscode-docker" // Added Docker extension
]
}
},
"postCreateCommand": "rustup update && rustup component add clippy && rustup component add rustfmt"
}
41 changes: 41 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI/CD

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

- name: Check
run: make check

- name: Format
run: make format

- name: Test
run: make test

- name: Release
run: cargo build --release

- name: Upload Binary Artifact
uses: actions/upload-artifact@v4 #v3 is scheduled for deprecation Nov 30 2024 check marketplace
with:
name: Rust Artificat Binary
path: target/release/loan_payment
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
237 changes: 237 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "loan_payment"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = { version = "4.1", features = ["derive"] }
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
format:
rustfmt src/main.rs
rustfmt src/lib.rs
check:
cargo check
test:
cargo test
lint:
cargo clippy
Empty file added README.md
Empty file.
Binary file added data/check.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/cli_run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/test_run.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// src/lib.rs

// Function to calculate the monthly interest rate
pub fn calculate_monthly_rate(annual_rate: f64) -> f64 {
annual_rate / 12.0
}

// Function to calculate the discount factor for the loan
pub fn calculate_discount_factor(monthly_rate: f64, periods: i32) -> f64 {
if monthly_rate == 0.0 {
1.0 // If there's no interest rate, the discount factor doesn't apply
} else {
1.0 - (1.0 + monthly_rate).powi(-periods)
}
}

// Function to calculate the monthly loan payment
pub fn get_monthly_payment(principal: f64, rate: f64, periods: i32) -> f64 {
let monthly_rate = calculate_monthly_rate(rate);

// Handle the case where interest rate is zero
if rate == 0.0 {
principal / periods as f64
} else {
let discount_factor = calculate_discount_factor(monthly_rate, periods);
(principal * monthly_rate) / discount_factor
}
}

// Unit tests for the above functions
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_calculate_monthly_rate() {
// Test with a typical annual rate
assert_eq!(calculate_monthly_rate(0.12), 0.01);

// Test with a 0% annual rate
assert_eq!(calculate_monthly_rate(0.0), 0.0);

// Test with a negative annual rate
assert_eq!(calculate_monthly_rate(-0.12), -0.01);
}

#[test]
fn test_calculate_discount_factor() {
// Test with a typical input
let discount_factor = calculate_discount_factor(0.01, 12);

// Test with zero monthly rate (discount factor should be 1.0)
assert_eq!(calculate_discount_factor(0.0, 12), 1.0);
}

#[test]
fn test_get_monthly_payment() {
// Test with typical inputs
let payment = get_monthly_payment(100000.0, 0.05, 360);
assert!((payment - 536.82).abs() < 0.01); // Margin of error

// Test with zero interest rate (adjusted logic)
assert_eq!(get_monthly_payment(100000.0, 0.0, 360), 277.77777777777777); // Principal / periods
}
}
Loading

0 comments on commit ca094cb

Please sign in to comment.