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
500 changes: 500 additions & 0 deletions rust_session/Assignment/reg_Assgn2/Cargo.lock

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions rust_session/Assignment/reg_Assgn2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "student_registry"
version = "0.1.0"
edition = "2021"
[dependencies]
uuid = { version = "1", features = ["v4"] }
31 changes: 31 additions & 0 deletions rust_session/Assignment/reg_Assgn2/src/grade.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#[derive(Debug, Clone)]
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, Clone)]
pub enum Sex {
Male,
Female,
}

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

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

fn main() {
// let g = Grade::Second;
// println!("{}", g.as_str()); // "2nd Year"
// println!("{:?}", g);

// let ss = Student::new();
// println!("stund")

// let mut reg = Registry::new();

// reg.add("Victor", 20, Grade::First, 78.5);
// reg.add("Kosi", 22, Grade::Second, 64.0);
// reg.add("Yusrah", 21, Grade::First, 91.0);

// reg.list_all();]

// let sex = Sex::Male;
// println!("sex: {:?}", sex.to_str());

// let s: Student = Student::new(1, String::from("Testimony"), 16, Sex::Female, Grade::Third, 40.5);
// let s: Student = Student::new(
// 1,
// "Testimony".to_string(),
// 16,
// Sex::Female,
// Grade::Third,
// 40.5,
// );
// println!("student here: {:#?}", s);

// println!("student id: {}", s.id);
// println!("student name: {}", s.name);
// println!("student age: {}", s.age);


let mut reg = Registry::new(vec![]);

reg.add("Testimony", 20, Sex::Female, Grade::Second, 20.5);
reg.add("Basongo", 22, Sex::Male, Grade::First, 72.0);

reg.list_all();

// i cant get guess a UUID , so i need to grab the id first
if let Some(student) = reg.students.first() {
let id = student.id; // capture the Uuid

println!("\n--- Found by UUID ---");
reg.get_student(id);

println!("\n--- Update name ---");
reg.update_name(id, "Alice Updated");
reg.get_student(id);

println!("\n--- Delete ---");
reg.delete_student(id);
}

reg.list_all();

}
88 changes: 88 additions & 0 deletions rust_session/Assignment/reg_Assgn2/src/registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
use crate::grade::{Grade, Sex};
use crate::student_struct::Student;
use uuid::Uuid;

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

impl Registry {
pub fn new(students: Vec<Student>) -> Registry {
Registry{students}
}

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

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

pub fn find_by_id(&self, id: Uuid) -> Option<&Student> {
self.students.iter().find(|s| s.id == id)
}

pub fn get_student(&self, id: Uuid) {
match self.find_by_id(id) {
Some(s) => println!(
"ID: {}, Name: {}, Age: {}, Grade: {:?}, Score: {}",
s.id, s.name, s.age, s.grade, s.score
),
None => println!("Student with ID {} not found.", id),
}
}

pub fn update_name(&mut self, id: Uuid, new_name: &str) {
match self.students.iter_mut().find(|s| s.id == id) {
Some(s) => s.name = new_name.to_string(),
None => println!("Student with ID {} not found.", id),
}
}

pub fn update_age(&mut self, id: Uuid, new_age: u8) {
match self.students.iter_mut().find(|s| s.id == id) {
Some(s) => s.age = new_age,
None => println!("Student with ID {} not found.", id),
}
}

pub fn update_grade(&mut self, id: Uuid, input: &str) {
let new_grade = match input.to_lowercase().as_str() {
"first" => Some(Grade::First),
"second" => Some(Grade::Second),
"third" => Some(Grade::Third),
_ => { println!("Unknown grade: {}", input); None }
};
if let Some(grade) = new_grade {
match self.students.iter_mut().find(|s| s.id == id) {
Some(s) => s.grade = grade,
None => println!("Student with ID {} not found.", id),
}
}
}

pub fn delete_student(&mut self, id: Uuid) {
match self.students.iter().position(|s| s.id == id) {
Some(index) => {
self.students.remove(index);
println!("Deleted student {}", id);
}
None => println!("Student with ID {} not found.", id),
}
}
}
6 changes: 6 additions & 0 deletions rust_session/Assignment/reg_Assgn2/src/registry_struct.rs
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
}
19 changes: 19 additions & 0 deletions rust_session/Assignment/reg_Assgn2/src/student_struct.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use uuid::Uuid;
use crate::grade::{Grade, Sex};

#[derive(Debug, Clone)] // add Debug here
pub struct Student {
pub id: Uuid,
pub name: String,
pub age: u8,
pub sex: Sex,
pub grade: Grade,
pub score: f32,
}


impl Student {
pub fn new(id: Uuid, name: String, age: u8, sex: Sex, grade: Grade, score: f32) -> Student {
Student { id, name, age, sex, grade, score }
}
}
3 changes: 3 additions & 0 deletions rust_session/Assignment/reg_Assgn2/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// pub fn to_str(x: String) -> &'static str {
// x.as_str()
// }
Loading