diff --git a/src/main.rs b/src/main.rs index 5ff0fd4..7e657cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,13 +6,11 @@ mod integers; mod ownership; mod referencing_and_borrowing; mod statements_and_expressions; +mod student_registry_project; + +// pub mod student_registry_project; -pub mod student_registry_project; -use student_registry_project::{ - types::basic_types::{Sex, Student, StudentRegistry}, - utils::convert_to_string, -}; fn main() { // exercises::ex_1::unused_var(); @@ -74,19 +72,23 @@ fn main() { // compound_types::ex_10::main(); // compound_types::ex_11::main(); // compound_types::ex_12::main(); + student_registry_project::ex_1::main(); // 09-08-2024 session // Student Registry - let mut rust_cohort = StudentRegistry::new_session(); - let stephanie = rust_cohort.register( - convert_to_string("Stephanie"), - convert_to_string("Nwankwo"), - 20, - 5.8, - Sex::Female, - ); - // println!("stephanie as student: {:#?} ", stephanie); - // println!("stephanie as student: {stephanie:#?} "); - let st_1 = rust_cohort.get_student_by_id(0); - println!("returned student: {st_1:#?}"); + // let mut rust_cohort = StudentRegistry::new_session(); + // let stephanie = rust_cohort.register( + // convert_to_string("Stephanie"), + // convert_to_string("Nwankwo"), + // 20, + // 5.8, + // Sex::Female, + // ); + // // println!("stephanie as student: {:#?} ", stephanie); + // // println!("stephanie as student: {stephanie:#?} "); + // let st_1 = rust_cohort.get_student_by_id(0); + // println!("returned student: {st_1:#?}"); + + } + diff --git a/src/student_registry_project/ex_1.rs b/src/student_registry_project/ex_1.rs new file mode 100644 index 0000000..8924caa --- /dev/null +++ b/src/student_registry_project/ex_1.rs @@ -0,0 +1,111 @@ +use crate::student_registry_project::types::basic_types::{Sex, StudentRegistry}; + +use crate::student_registry_project::utils::{ input_any, input_f32, input_u32, input_u8}; + + + +fn get_courses(rust_cohort: &mut StudentRegistry) -> u32 { + println!("{:#?}", rust_cohort.course_registry); + + let course_id = input_u32("Enter course id: "); + + course_id +} + +fn student_flow(rust_cohort: &mut StudentRegistry) { + + loop { + println!("1. Add student\n2. Get student\n3.Enroll student in course\n4.Return"); + + let student_state = input_u8("Enter choice: "); + + match student_state { + 1 => { + let (first_name, last_name, age, height, sex) = get_student_info(); + let student = rust_cohort.register(first_name, last_name, age, height, sex); + println!("student: {:#?}", student); + let course_id = get_courses(rust_cohort); + rust_cohort.enroll_student_in_course(student.id, course_id); + } + 2 => { + if rust_cohort.total_students.is_empty() { + println!("No students in the cohort"); + } else { + let len = rust_cohort.total_students.len() - 1; + let id = input_u32(format!("Enter student id(Range 0 to {}): ", len).as_str()); + if let Some(student) = rust_cohort.get_student_by_id(id) { + println!("student: {:#?}", student); + } else { + println!("Student not found"); + } + } + } + 3 => { + if rust_cohort.total_students.is_empty() { + println!("No students in the cohort"); + } else { + let len = rust_cohort.total_students.len() - 1; + let id = input_u32(format!("Enter student id(Range 0 to {}): ", len).as_str()); + let course_id = get_courses(rust_cohort); + rust_cohort.enroll_student_in_course(id, course_id); + } + } + 4 => break, + _ => continue, + } + } +} + +fn get_student_info() -> (String, String, u8, f32, Sex) { + let first_name = input_any("Enter first name: "); + + let last_name = input_any("Enter last name: "); + + let age = input_u8("Enter age: "); + + let height = input_f32( "Enter height: "); + + let sex = input_u8("Enter sex: 1 for male, 2 for female: "); + let sex = match sex { + 1 => Sex::Male, + 2 => Sex::Female, + _ => panic!("Invalid input. Please enter 1 for male or 2 for female."), + }; + + (first_name, last_name, age, height, sex) +} + +fn create_course(rust_cohort: &mut StudentRegistry) { + + let course_name = input_any("Enter course name: "); + + let course_capacity = input_u32("Enter course capacity: "); + + rust_cohort.add_course(course_name, course_capacity); + +} + +pub fn main() { + let mut rust_cohort = StudentRegistry::new_session(); + + loop { + + if rust_cohort.course_registry.is_empty() { + create_course(&mut rust_cohort); + } + + println!("1. Add course\n2. Manage students\n3. Exit"); + + let main_state = input_u8("Enter choice: "); + + match main_state { + 1 => create_course(&mut rust_cohort), + 2 => student_flow(&mut rust_cohort), + 3 => break, + _ => continue, + } + } + + println!("Bye!"); +} + diff --git a/src/student_registry_project/implementations/student_registry.rs b/src/student_registry_project/implementations/student_registry.rs index d1cc943..88a8bf9 100644 --- a/src/student_registry_project/implementations/student_registry.rs +++ b/src/student_registry_project/implementations/student_registry.rs @@ -1,4 +1,5 @@ -use crate::student_registry_project::types::basic_types::{Sex, Student, StudentRegistry}; +use crate::student_registry_project::types::basic_types::{Sex, Course, Student, StudentRegistry}; +use std::collections::HashMap; impl StudentRegistry { // this initializes a new StudentRegistry @@ -6,9 +7,25 @@ impl StudentRegistry { pub fn new_session() -> StudentRegistry { StudentRegistry { total_students: Vec::new(), + course_registry: Vec::new(), } } + // add course + pub fn add_course(&mut self, _name: String, _capacity: u32) { + let course_id: u32 = self.course_registry.len() as u32 + 1; + // let _enrolled_student_ids: Vec = Vec::with_capacity(_capacity as usize); + let _enrolled_student_ids: HashMap = HashMap::with_capacity(_capacity as usize); + + + self.course_registry.push(Course { + id: course_id, + name: _name, + capacity: _capacity, + enrolled_student_ids: _enrolled_student_ids, + }); + } + // register student pub fn register( &mut self, @@ -20,6 +37,7 @@ impl StudentRegistry { ) -> Student { // generate a unique ID let student_id: u32 = self.total_students.len() as u32 + 1; + let student = Student { first_name: f_name, last_name: l_name, @@ -27,6 +45,7 @@ impl StudentRegistry { age: _age, height: _height, sex: _sex, + enrolled_course_ids: HashMap::new(), }; self.total_students.push(student.clone()); student @@ -37,4 +56,52 @@ impl StudentRegistry { pub fn get_student_by_id(&self, id: u32) -> Option<&Student> { self.total_students.get(id as usize) } + + + // Method to enroll a student in a course + pub fn enroll_student_in_course(&mut self, student_id: u32, course_id: u32) { + // Find the student and course + + let student_index: usize = (student_id - 1).try_into().unwrap(); + let course_index: usize = (course_id - 1).try_into().unwrap(); + + // Check if the student exists + if student_index >= self.total_students.len() { + println!("Student not found."); + return; + } + + // Check if the course exists + if course_index >= self.course_registry.len() { + println!("Course not found."); + return; + } + + // Check if the student is already enrolled in the course + if self.total_students[student_index] + .enrolled_course_ids + .get(&course_id) + .is_some() + { + println!("Student is already enrolled in the course."); + return; + } + + + // Check if the course has available capacity + if self.course_registry[course_index].enrolled_student_ids.len() < self.course_registry[course_index].capacity as usize { + // Add the student to the course's enrolled students + self.course_registry[course_index].enrolled_student_ids + .insert(student_id, self.total_students[student_index].first_name.clone()); + + // Add the course to the student's enrolled courses + self.total_students[student_index].enrolled_course_ids + .insert(course_id, self.course_registry[course_index].name.clone()); + + println!("Student enrolled in course."); + + } else { + println!("Course is full. Cannot enroll student."); + } + } } diff --git a/src/student_registry_project/mod.rs b/src/student_registry_project/mod.rs index d0b6963..076c4d8 100644 --- a/src/student_registry_project/mod.rs +++ b/src/student_registry_project/mod.rs @@ -1,3 +1,4 @@ pub mod implementations; pub mod types; -pub mod utils; \ No newline at end of file +pub mod utils; +pub mod ex_1; \ No newline at end of file diff --git a/src/student_registry_project/types/basic_types.rs b/src/student_registry_project/types/basic_types.rs index e02d6b4..c054122 100644 --- a/src/student_registry_project/types/basic_types.rs +++ b/src/student_registry_project/types/basic_types.rs @@ -1,3 +1,5 @@ +use std::collections::HashMap; + #[derive(Debug, Clone)] pub struct Student { pub first_name: String, @@ -6,6 +8,7 @@ pub struct Student { pub age: u8, pub height: f32, pub sex: Sex, + pub enrolled_course_ids: HashMap, } #[derive(Debug, Clone)] @@ -14,9 +17,19 @@ pub enum Sex { Female, } +#[derive(Debug, Clone)] +pub struct Course { + pub id: u32, + pub name: String, + pub capacity: u32, + pub enrolled_student_ids: HashMap, +} + + #[derive(Debug, Clone)] pub struct StudentRegistry { pub total_students: Vec, + pub course_registry: Vec, } /*** diff --git a/src/student_registry_project/utils/mod.rs b/src/student_registry_project/utils/mod.rs index 0c3d9fe..8b21f5d 100644 --- a/src/student_registry_project/utils/mod.rs +++ b/src/student_registry_project/utils/mod.rs @@ -1,3 +1,45 @@ +use std::io; +use std::io::Write; // for flush + pub fn convert_to_string(x: &str) -> String { x.to_string() } + +pub fn convert_to_u32(input: &str) -> u32 { + input + .trim() + .parse::() + .expect("Invalid input. Please enter a number.") +} + +pub fn input_u32(prompt: &str) -> u32 { + input_any(prompt) + .trim() + .parse::() + .expect("Invalid input. Please enter a number.") +} + +pub fn input_u8(prompt: &str) -> u8 { + input_any(prompt) + .trim() + .parse::() + .expect("Invalid input. Please enter a number.") +} + +pub fn input_f32(prompt: &str) -> f32 { + input_any(prompt) + .trim() + .parse::() + .expect("Invalid input. Please enter a number.") + +} + +pub fn input_any(prompt: &str) -> String { + let mut input = String::new(); + print!("{}", prompt); + io::stdout().flush().unwrap(); + io::stdin().read_line(&mut input).expect("Failed to read line"); + input.trim().to_string() + +} +// } \ No newline at end of file