Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rust_session/yahia008/minilib/Cargo.lock

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

6 changes: 6 additions & 0 deletions rust_session/yahia008/minilib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "minilib"
version = "0.1.0"
edition = "2024"

[dependencies]
106 changes: 106 additions & 0 deletions rust_session/yahia008/minilib/src/library.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use crate::member::Member;

#[derive(Debug, Clone)]
pub struct Book {
pub title: String,
pub author: String,
pub available: bool,
}

#[derive(Debug)]
pub struct Library {
pub books: Vec<Book>,
}

impl Library {
pub fn new() -> Self {
Library { books: Vec::new() }
}


pub fn add_book(&mut self, title: String, author: String, available: bool) {
let new_book = Book {
title,
author,
available,
};
self.books.push(new_book);
println!("Book added successfully!");
}


pub fn list_books(&self) {
if self.books.is_empty() {
println!(" No books in the library yet!");
return;
}

println!("\nLibrary Collection:");
println!("{:<30} {:<25} {:<10}", "Title", "Author", "Available");
println!("{}", "-".repeat(65));

for book in &self.books {
println!(
"{:<30} {:<25} {:<10}",
book.title,
book.author,
if book.available { "Yes" } else { "No" }
);
}
}

pub fn search_by_title(&self, title: &str) -> Option<&Book> {
self.books.iter().find(|book| book.title == title)
}


pub fn search_by_author(&self, author: &str) -> Vec<&Book> {
self.books.iter().filter(|book| book.author == author).collect()
}


pub fn checkout_book(&mut self, title: &str, member: &mut Member) -> bool {
if let Some(book) = self.books.iter_mut().find(|book| book.title == title) {
if book.available {
book.available = false;
member.borrowed_books.push(book.title.clone());
println!("'{}' has been checked out by {}", title, member.name);
return true;
} else {
println!(" '{}' is already checked out!", title);
return false;
}
}
println!(" Book '{}' not found!", title);
false
}


pub fn return_book(&mut self, title: &str, member: &mut Member) -> bool {
if let Some(book) = self.books.iter_mut().find(|book| book.title == title) {
if !book.available {
book.available = true;
member.borrowed_books.retain(|b| b != title);
println!(" '{}' has been returned by {}", title, member.name);
return true;
} else {
println!(" '{}' wasn't checked out!", title);
return false;
}
}
println!("Book '{}' not found!", title);
false
}


pub fn display_book_info(&self, title: &str) {
if let Some(book) = self.search_by_title(title) {
println!("\n📖 Book Details:");
println!(" Title: {}", book.title);
println!(" Author: {}", book.author);
println!(" Status: {}", if book.available { "Available" } else { "Checked Out" });
} else {
println!(" Book '{}' not found!", title);
}
}
}
51 changes: 51 additions & 0 deletions rust_session/yahia008/minilib/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
mod library;
mod member;

use library::Library;
use member::Member;

fn main() {
let mut library = Library::new();
let mut member = Member::new("rasputin".to_string());

library.add_book("48 laws of power".to_string(), "robert green".to_string(), true);
library.add_book("meditation".to_string(), "Machiavelli".to_string(), true);
library.add_book("thing fall apart".to_string(), "Chinua Achebe".to_string(), true);
library.add_book("romeo an julliet".to_string(), "William Shakespeare".to_string(), false);


library.list_books();

println!("\n--- Searching for 'Rust in Action' ---");
if let Some(book) = library.search_by_title("meditation") {
println!("Found: {} by {}", book.title, book.author);
}

println!("\n--- Searching for books by 'Machiavelli' ---");
let books = library.search_by_author("Machiavelli");
for book in books {
println!("Found: {}", book.title);
}

println!("\n--- Checkout Process ---");
library.checkout_book("48 laws of power", &mut member);
library.checkout_book("meditation", &mut member);
library.checkout_book("romeo an julliet", &mut member);

member.list_borrowed_books();

println!("\n--- Book Info ---");
library.display_book_info("48 laws of power");
library.display_book_info("romeo an julliet");

println!("\n--- Return Process ---");
library.return_book("48 laws of power", &mut member);

println!("\n--- Updated Library ---");
library.list_books();


println!("\n--- rasputins's Remaining Books ---");
member.list_borrowed_books();

}
25 changes: 25 additions & 0 deletions rust_session/yahia008/minilib/src/member.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#[derive(Debug)]
pub struct Member {
pub name: String,
pub borrowed_books: Vec<String>,
}

impl Member {
pub fn new(name: String) -> Self {
Member {
name,
borrowed_books: Vec::new(),
}
}

pub fn list_borrowed_books(&self) {
if self.borrowed_books.is_empty() {
println!("{} hasn't borrowed any books.", self.name);
} else {
println!("\n {}'s Borrowed Books:", self.name);
for book in &self.borrowed_books {
println!(" - {}", book);
}
}
}
}
7 changes: 7 additions & 0 deletions rust_session/yahia008/student_registry-V1/Cargo.lock

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

6 changes: 6 additions & 0 deletions rust_session/yahia008/student_registry-V1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "student_registry-V1"
version = "0.1.0"
edition = "2024"

[dependencies]
31 changes: 31 additions & 0 deletions rust_session/yahia008/student_registry-V1/src/grade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[derive(Debug, PartialEq)]
pub enum Grade {
First,
Second,
Third,
}

impl Grade {
pub fn as_str(&self) -> &str {
match self {
Grade::First => "Cohort 1",
Grade::Second => "Cohort 2",
Grade::Third => "Cohort 3",
}
}
}

#[derive(Debug)]
pub enum Sex {
Male,
Female,
}

impl Sex {
pub fn to_str(&self) {
match self {
Sex::Male => println!("male: 👨🏾"),
Sex::Female => println!("female: 👧🏾"),
}
}
}
47 changes: 47 additions & 0 deletions rust_session/yahia008/student_registry-V1/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
mod grade;
mod registry;
mod student_struct;


use grade::{Grade, Sex};
use registry::Registry;
use student_struct::Student;

fn main() {
let mut registry = Registry {
students: Vec::new(),
next_id: 0,
};

registry.add("yusrah", 20, Sex::Female, Grade::First, 85.5);
registry.add("Dave", 22, Sex::Male, Grade::Second, 92.0);
registry.add("basongs", 19, Sex::Male, Grade::Third, 78.5);

println!("\nAll students:");
registry.list_all();


println!("\nUpdating student with ID 1...");
if let Some(student) = registry.update(0, "jiggs", 21, Sex::Male, Grade::Second, 88.0) {
println!("Updated: {} (ID {})", student.name, student.id);
} else {
println!("Student not found");
}

println!("\nAfter update:");
registry.list_all();

println!("\nFinding student with ID 2...");
if let Some(student) = registry.find_by_id(2) {
println!("Found: {} (ID {}, Age: {})", student.name, student.id, student.age);
}

println!("\nDeleting student with ID 2...");
if let Some(deleted) = registry.delete(2) {
println!("Deleted: {} (ID {})", deleted.name, deleted.id);
}

println!("\nAfter deletion:");
registry.list_all();

}
69 changes: 69 additions & 0 deletions rust_session/yahia008/student_registry-V1/src/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::grade::{Grade, Sex};
use crate::student_struct::Student;

pub struct Registry {
pub students: Vec<Student>,
pub next_id: u32,
}

impl Registry {
pub fn add(&mut self, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) {
let id = self.next_id;
let student = Student::new(id, name.to_string(), age, sex, grade, score);
println!("Added: {} (ID {})", student.name, student.id);
self.students.push(student);
self.next_id += 1;
}

pub fn list_all(&self) {
if self.students.is_empty() {
println!(" (no students enrolled yet)");
return;
}
println!(
" {:>5} {:<20} {:<6} {:<10} {}",
"ID", "Name", "Age", "Grade", "Score"
);
println!(" {}", "-".repeat(55));
for student in &self.students {
println!(
" {:>5} {:<20} {:>6} {:<10} {:.1}",
student.id,
student.name,
student.age,
student.grade.as_str(),
student.score,
);
}
}



pub fn find_by_id(&mut self, id: u32) -> Option<&mut Student> {
self.students.iter_mut().find(|student| student.id == id)
}

pub fn update(&mut self, id:u32, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) -> Option<&mut Student> {
if let Some(student) = self.find_by_id(id) {
student.name = name.to_string();
student.age = age;
student.sex = sex;
student.grade = grade;
student.score = score;
Some(student)
} else {
None
}
}

pub fn delete(&mut self, id: u32) -> Option<Student> {
let position = self.students.iter().position(|student| student.id == id);

match position {
Some(index) => Some(self.students.remove(index)),
None => None,
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::student_struct;

pub struct Registry {
students: Vec<student_struct::Student>, // Vec<Student> = "a list of Student values"
next_id: u32, // auto-increment counter for IDs
}
Loading