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
2 changes: 1 addition & 1 deletion rust_session/student_registry/src/grade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Grade {
}
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Sex {
Male,
Female,
Expand Down
33 changes: 33 additions & 0 deletions rust_session/student_registry/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Registry {
}

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);
Expand Down Expand Up @@ -36,4 +37,36 @@ impl Registry {
);
}
}

pub fn delete_by_id(&mut self, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) {
if let Some(pos) = self.students.iter().position(|student| {
student.name == name
&& student.age == age
&& student.sex == sex
&& student.grade == grade
&& (student.score - score).abs() < f32::EPSILON
}) {
let student = self.students.remove(pos);
println!("Deleted: {} (ID {})", student.name, student.id);
} else {
println!("No matching student found to delete: {}", name);
}
}

pub fn update_by_id(&mut self, id: u32, name: &str, age: u8, sex: Sex, grade: Grade, score: f32) {
if let Some(student) = self.students.iter_mut().find(|student| student.id == id) {
student.name = name.to_string();
student.age = age;
student.sex = sex;
student.grade = grade;
student.score = score;
println!("Updated: {} (ID {})", student.name, student.id);
} else {
println!("No matching student found to update with ID: {}", id);
}
}

pub fn find_id(&self, id: u32) -> Option<&Student> {
self.students.iter().find(|student| student.id == id)
}
}
10 changes: 5 additions & 5 deletions rust_session/student_registry/src/registry_struct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::student_struct;
// 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
}
// pub struct Registry {
// students: Vec<student_struct::Student>, // Vec<Student> = "a list of Student values"
// next_id: u32, // auto-increment counter for IDs
// }
1 change: 1 addition & 0 deletions rust_session/student_registry/src/student_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Student {

// This is the implementation of the student struct with its corresponding methods
impl Student {

pub fn new(id: u32, name: String, age: u8, sex: Sex, grade: Grade, score: f32) -> Student {
Student {
id,
Expand Down
69 changes: 69 additions & 0 deletions rust_session/student_registry/src/uuid_registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::grade::{Grade, Sex};
use uuid::Uuid;

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

impl UuidStudent {


pub fn new(name: String, age: u8, sex: Sex, grade: Grade, score: f32) -> Self {
Self {
id: Uuid::new_v4(),
name,
age,
sex,
grade,
score,
}
}
}

pub struct UuidRegistry {
pub students: Vec<UuidStudent>,
}

impl UuidRegistry {
pub fn new() -> Self {
Self {
students: Vec::new(),
}
}

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

pub fn update(&mut self,id: Uuid,name: &str,age: u8,sex: Sex,grade: Grade,score: f32,) -> bool {
if let Some(student) = self.students.iter_mut().find(|student| student.id == id) {
student.name = name.to_string();
student.age = age;
student.sex = sex;
student.grade = grade;
student.score = score;
println!("Updated: {} (ID {})", student.name, student.id);
true
} else {
println!("No student found with ID: {}", id);
false
}
}

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



}
Loading