From 2fc568711ecc7a423e94f605c198d687478a928c Mon Sep 17 00:00:00 2001 From: RichoKD Date: Sat, 10 Aug 2024 18:40:41 +0100 Subject: [PATCH 1/4] Cli state machine implementation for student registry --- src/main.rs | 36 ++++---- src/student_registry_project/ex_1.rs | 124 +++++++++++++++++++++++++++ src/student_registry_project/mod.rs | 3 +- 3 files changed, 145 insertions(+), 18 deletions(-) create mode 100644 src/student_registry_project/ex_1.rs 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..5b6d54c --- /dev/null +++ b/src/student_registry_project/ex_1.rs @@ -0,0 +1,124 @@ +// use student_registry_project::{ +use crate::student_registry_project::types::basic_types::{Sex, Student, StudentRegistry}; + +// use types::basic_types::{Sex, Student, StudentRegistry}; +// use utils::convert_to_string; +// }; + +use std::io; +use std::io::Write; // for flush + + +pub fn main() { + // Student Registry + let mut rust_cohort = StudentRegistry::new_session(); + + let mut state: i32 = 0; + + // Cli state machine + loop { + let mut input = String::new(); + + print!("1. Add student\n2. Get student\n3. Exit\n"); + io::stdout().flush().unwrap(); + + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + state = input + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + // Add student + if state == 1 { + print!("Enter first name: "); + io::stdout().flush().unwrap(); + let mut first_name = String::new(); + io::stdin() + .read_line(&mut first_name) + .expect("Failed to read line"); + let first_name = first_name.trim().to_string(); + + print!("Enter last name: "); + io::stdout().flush().unwrap(); + let mut last_name = String::new(); + io::stdin() + .read_line(&mut last_name) + .expect("Failed to read line"); + let last_name = last_name.trim().to_string(); + + print!("Enter age: "); + io::stdout().flush().unwrap(); + let mut age = String::new(); + io::stdin() + .read_line(&mut age) + .expect("Failed to read line"); + let age = age + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + print!("Enter height: "); + io::stdout().flush().unwrap(); + let mut height = String::new(); + io::stdin() + .read_line(&mut height) + .expect("Failed to read line"); + let height = height + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + print!("Enter sex: 1 for male, 2 for female: "); + let mut sex = String::new(); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut sex) + .expect("Failed to read line"); + let sex = sex.trim().parse::().unwrap(); + + if sex == 1 { + let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Male); + println!("student: {student:#?}"); + } else if sex == 2 { + let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Female); + println!("student: {student:#?}"); + } else { + println!("Invalid input"); + io::stdout().flush().unwrap(); + + continue; + } + + // println!("Student {first_name} {last_name} added successfully"); + // io::stdout().flush().unwrap(); + + } + + // Get student + if state == 2 { + let length = rust_cohort.total_students.len() as u32; + if length == 0 { + println!("No students in the cohort"); + io::stdout().flush().unwrap(); + + } else { + let len = length - 1; + print!("Enter student id(Range 0 to {len}): "); + io::stdout().flush().unwrap(); + + let mut id = String::new(); + io::stdin().read_line(&mut id).expect("Failed to read line"); + let id = id.trim().parse::().unwrap(); + let student = rust_cohort.get_student_by_id(id); + println!("student: {student:#?}"); + } + } + // Exit + if state == 3 { + println!("Bye!"); + break; + } + } +} \ No newline at end of file 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 From 8cd2001d578f43bef096bee8796ba7efa4f4caf4 Mon Sep 17 00:00:00 2001 From: RichoKD Date: Thu, 15 Aug 2024 16:08:51 +0100 Subject: [PATCH 2/4] Implemented courses --- src/student_registry_project/ex_1.rs | 262 +++++++++++++++++- .../implementations/student_registry.rs | 57 +++- .../types/basic_types.rs | 11 + 3 files changed, 328 insertions(+), 2 deletions(-) diff --git a/src/student_registry_project/ex_1.rs b/src/student_registry_project/ex_1.rs index 5b6d54c..1b93b3a 100644 --- a/src/student_registry_project/ex_1.rs +++ b/src/student_registry_project/ex_1.rs @@ -1,5 +1,5 @@ // use student_registry_project::{ -use crate::student_registry_project::types::basic_types::{Sex, Student, StudentRegistry}; +use crate::student_registry_project::types::basic_types::{Sex,Course, Student, StudentRegistry}; // use types::basic_types::{Sex, Student, StudentRegistry}; // use utils::convert_to_string; @@ -8,13 +8,273 @@ use crate::student_registry_project::types::basic_types::{Sex, Student, StudentR use std::io; use std::io::Write; // for flush +// fn create_course(name: String, capacity: u32, mut _courses: Vec) -> Vec { +// let course_id: u32 = _courses.len() as u32 + 1; +// let new_course = Course { +// id: course_id, +// name: name, +// capacity: capacity, +// enrolled_student_ids: Vec::new(), +// }; +// _courses.push(new_course); +// _courses +// } + +// fn prompt(input: &str) { +// print!("{input}"); +// io::stdout().flush().unwrap(); + +// } +fn get_courses(rust_cohort: &mut StudentRegistry) -> u32 { + print!("{:#?}", rust_cohort.course_registry); + io::stdout().flush().unwrap(); + + print!("Enter course id: \n"); + io::stdout().flush().unwrap(); + let mut course_id = String::new(); + io::stdin() + .read_line(&mut course_id) + .expect("Failed to read line"); + let course_id = course_id + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + // let course = rust_cohort.get_course_by_id(course_id); + // println!("course: {course:#?}"); + course_id +} + +fn student_flow(rust_cohort: &mut StudentRegistry) { + let mut student_state: i32 = 0; + loop { + let mut input = String::new(); + + print!("1. Add student\n2. Get student\n3.Enroll student in course\n4.Return\n"); + io::stdout().flush().unwrap(); + + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + student_state = input + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + // Add student + if student_state == 1 { + print!("Enter first name: "); + io::stdout().flush().unwrap(); + let mut first_name = String::new(); + io::stdin() + .read_line(&mut first_name) + .expect("Failed to read line"); + let first_name = first_name.trim().to_string(); + + print!("Enter last name: "); + io::stdout().flush().unwrap(); + let mut last_name = String::new(); + io::stdin() + .read_line(&mut last_name) + .expect("Failed to read line"); + let last_name = last_name.trim().to_string(); + + print!("Enter age: "); + io::stdout().flush().unwrap(); + let mut age = String::new(); + io::stdin() + .read_line(&mut age) + .expect("Failed to read line"); + let age = age + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + print!("Enter height: "); + io::stdout().flush().unwrap(); + let mut height = String::new(); + io::stdin() + .read_line(&mut height) + .expect("Failed to read line"); + let height = height + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + print!("Enter sex: 1 for male, 2 for female: "); + let mut sex = String::new(); + io::stdout().flush().unwrap(); + io::stdin() + .read_line(&mut sex) + .expect("Failed to read line"); + let sex = sex.trim().parse::().unwrap(); + + if sex == 1 { + let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Male); + println!("student: {student:#?}"); + io::stdout().flush().unwrap(); + // Get course_id and enroll student + let course_id = get_courses(rust_cohort); + rust_cohort.enroll_student_in_course(student.id, course_id); + } else if sex == 2 { + let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Female); + println!("student: {student:#?}"); + io::stdout().flush().unwrap(); + // Get course_id and enroll student + let course_id = get_courses(rust_cohort); + rust_cohort.enroll_student_in_course(student.id, course_id); + } else { + println!("Invalid input"); + io::stdout().flush().unwrap(); + + continue; + } + + // println!("Student {first_name} {last_name} added successfully"); + // io::stdout().flush().unwrap(); + + } + + // Get student + if student_state == 2 { + let length = rust_cohort.total_students.len() as u32; + if length == 0 { + println!("No students in the cohort"); + io::stdout().flush().unwrap(); + + } else { + let len = length - 1; + print!("Enter student id(Range 0 to {len}): "); + io::stdout().flush().unwrap(); + + let mut id = String::new(); + io::stdin().read_line(&mut id).expect("Failed to read line"); + let id = id.trim().parse::().unwrap(); + let student = rust_cohort.get_student_by_id(id); + println!("student: {student:#?}"); + } + } + // Enroll student in course + if student_state == 3{ + // print students + print!("{:#?}", rust_cohort.total_students); + io::stdout().flush().unwrap(); + + print!("Enter student id: "); + io::stdout().flush().unwrap(); + + let mut id = String::new(); + io::stdin().read_line(&mut id).expect("Failed to read line"); + let id = id.trim().parse::().unwrap(); + + let course_id = get_courses(rust_cohort); + rust_cohort.enroll_student_in_course(id, course_id); + } + // Exit + if student_state == 4 { + break; + } + } +} + +fn create_course(rust_cohort: &mut StudentRegistry) { + // create course + print!("Enter course name: "); + io::stdout().flush().unwrap(); + let mut course_name = String::new(); + io::stdin() + .read_line(&mut course_name) + .expect("Failed to read line"); + let course_name = course_name.trim().to_string(); + print!("Enter course capacity: "); + io::stdout().flush().unwrap(); + + let mut course_capacity = String::new(); + io::stdin() + .read_line(&mut course_capacity) + .expect("Failed to read line"); + let course_capacity = course_capacity + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + rust_cohort.add_course(course_name, course_capacity); + + // rust_cohort +} pub fn main() { + let mut rust_cohort = StudentRegistry::new_session(); + // let mut courses: Vec = Vec::new(); + // rust_cohort.add_course("Rust".to_string(), 2); + // let courses = rust_cohort.course_registry.clone(); + // println!("courses: {courses:#?}"); + + // print courses + // println!("courses: {rust_cohort:#?}"); + + + let mut main_state: i32 = 0; + + loop{ + if rust_cohort.course_registry.len() as u32 == 0{ + + print!("1. Add course\n"); + io::stdout().flush().unwrap(); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + + main_state = input + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + if main_state == 1 { + create_course(&mut rust_cohort); + // break; + }else{ + continue; + } + }else { + print!("1. Add course\n2. Manage students\n3. Exit\n"); + io::stdout().flush().unwrap(); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + + main_state = input + .trim() + .parse::() + .expect("Invalid input. Please enter a number."); + + if main_state == 1 { + create_course(&mut rust_cohort); + + }else if main_state == 2 { + student_flow(&mut rust_cohort); + + }else if main_state == 3 { + println!("Bye!"); + break; + }else{ + continue; + } + } + } +} + +pub fn mainz() { // Student Registry let mut rust_cohort = StudentRegistry::new_session(); + // let mut courses: Vec = Vec::new(); let mut state: i32 = 0; + // courses = create_course("Rust".to_string(), 2, courses); + // Cli state machine loop { let mut input = String::new(); diff --git a/src/student_registry_project/implementations/student_registry.rs b/src/student_registry_project/implementations/student_registry.rs index d1cc943..ce85197 100644 --- a/src/student_registry_project/implementations/student_registry.rs +++ b/src/student_registry_project/implementations/student_registry.rs @@ -1,4 +1,4 @@ -use crate::student_registry_project::types::basic_types::{Sex, Student, StudentRegistry}; +use crate::student_registry_project::types::basic_types::{Sex, Course, Student, StudentRegistry}; impl StudentRegistry { // this initializes a new StudentRegistry @@ -6,9 +6,21 @@ 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; + self.course_registry.push(Course { + id: course_id, + name: _name, + capacity: _capacity, + enrolled_student_ids: Vec::new(), + }); + } + // register student pub fn register( &mut self, @@ -27,6 +39,7 @@ impl StudentRegistry { age: _age, height: _height, sex: _sex, + enrolled_course_ids: Vec::new(), }; self.total_students.push(student.clone()); student @@ -37,4 +50,46 @@ impl StudentRegistry { pub fn get_student_by_id(&self, id: u32) -> Option<&Student> { self.total_students.get(id as usize) } + + // pub fn get_courses(&self) -> &Vec { + + // } + + // 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 = self.total_students.iter().position(|s| s.id == student_id).unwrap(); + // let course_index = self.course_registry.iter().position(|c| c.id == course_id).unwrap(); + let student_index: usize = (student_id - 1).try_into().unwrap(); + let course_index: usize = (course_id - 1).try_into().unwrap(); + + // Check if the student is already enrolled in the course + if self.total_students[student_index].enrolled_course_ids.contains(&course_id) { + println!("Student is already enrolled in the course."); + return; + } + + // Check if the course exists + if course_index >= self.course_registry.len() { + println!("Course not found."); + return; + } + + // Check if the student exists + if student_index >= self.total_students.len() { + println!("Student not found."); + 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.push(student_id); + + // Add the course to the student's enrolled courses + self.total_students[student_index].enrolled_course_ids.push(course_id); + } else { + println!("Course is full. Cannot enroll student."); + } + } } diff --git a/src/student_registry_project/types/basic_types.rs b/src/student_registry_project/types/basic_types.rs index e02d6b4..b661753 100644 --- a/src/student_registry_project/types/basic_types.rs +++ b/src/student_registry_project/types/basic_types.rs @@ -6,6 +6,7 @@ pub struct Student { pub age: u8, pub height: f32, pub sex: Sex, + pub enrolled_course_ids: Vec, } #[derive(Debug, Clone)] @@ -14,9 +15,19 @@ pub enum Sex { Female, } +#[derive(Debug, Clone)] +pub struct Course { + pub id: u32, + pub name: String, + pub capacity: u32, + pub enrolled_student_ids: Vec, +} + + #[derive(Debug, Clone)] pub struct StudentRegistry { pub total_students: Vec, + pub course_registry: Vec, } /*** From 8fd8c985b1545b28e230902a4ff6f57b914b478b Mon Sep 17 00:00:00 2001 From: RichoKD Date: Sat, 17 Aug 2024 21:21:55 +0100 Subject: [PATCH 3/4] add input util functions and did code cleanup --- src/student_registry_project/ex_1.rs | 411 +++--------------- .../implementations/student_registry.rs | 17 +- src/student_registry_project/utils/mod.rs | 42 ++ 3 files changed, 122 insertions(+), 348 deletions(-) diff --git a/src/student_registry_project/ex_1.rs b/src/student_registry_project/ex_1.rs index 1b93b3a..8eb75ee 100644 --- a/src/student_registry_project/ex_1.rs +++ b/src/student_registry_project/ex_1.rs @@ -1,201 +1,89 @@ -// use student_registry_project::{ -use crate::student_registry_project::types::basic_types::{Sex,Course, Student, StudentRegistry}; +use crate::student_registry_project::types::basic_types::{Sex, StudentRegistry}; -// use types::basic_types::{Sex, Student, StudentRegistry}; -// use utils::convert_to_string; -// }; +use crate::student_registry_project::utils::{ input_any, input_f32, input_u32, input_u8}; -use std::io; -use std::io::Write; // for flush -// fn create_course(name: String, capacity: u32, mut _courses: Vec) -> Vec { -// let course_id: u32 = _courses.len() as u32 + 1; -// let new_course = Course { -// id: course_id, -// name: name, -// capacity: capacity, -// enrolled_student_ids: Vec::new(), -// }; -// _courses.push(new_course); -// _courses -// } -// fn prompt(input: &str) { -// print!("{input}"); -// io::stdout().flush().unwrap(); - -// } fn get_courses(rust_cohort: &mut StudentRegistry) -> u32 { - print!("{:#?}", rust_cohort.course_registry); - io::stdout().flush().unwrap(); + println!("{:#?}", rust_cohort.course_registry); - print!("Enter course id: \n"); - io::stdout().flush().unwrap(); - let mut course_id = String::new(); - io::stdin() - .read_line(&mut course_id) - .expect("Failed to read line"); - let course_id = course_id - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - // let course = rust_cohort.get_course_by_id(course_id); - // println!("course: {course:#?}"); + // print!("Enter course id: \n"); + + let course_id = input_u32("Enter course id: "); + course_id } fn student_flow(rust_cohort: &mut StudentRegistry) { - let mut student_state: i32 = 0; - loop { - let mut input = String::new(); - - print!("1. Add student\n2. Get student\n3.Enroll student in course\n4.Return\n"); - io::stdout().flush().unwrap(); - - io::stdin() - .read_line(&mut input) - .expect("Failed to read line"); - student_state = input - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - - // Add student - if student_state == 1 { - print!("Enter first name: "); - io::stdout().flush().unwrap(); - let mut first_name = String::new(); - io::stdin() - .read_line(&mut first_name) - .expect("Failed to read line"); - let first_name = first_name.trim().to_string(); - - print!("Enter last name: "); - io::stdout().flush().unwrap(); - let mut last_name = String::new(); - io::stdin() - .read_line(&mut last_name) - .expect("Failed to read line"); - let last_name = last_name.trim().to_string(); - - print!("Enter age: "); - io::stdout().flush().unwrap(); - let mut age = String::new(); - io::stdin() - .read_line(&mut age) - .expect("Failed to read line"); - let age = age - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - print!("Enter height: "); - io::stdout().flush().unwrap(); - let mut height = String::new(); - io::stdin() - .read_line(&mut height) - .expect("Failed to read line"); - let height = height - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); + loop { + println!("1. Add student\n2. Get student\n3.Enroll student in course\n4.Return"); - print!("Enter sex: 1 for male, 2 for female: "); - let mut sex = String::new(); - io::stdout().flush().unwrap(); - io::stdin() - .read_line(&mut sex) - .expect("Failed to read line"); - let sex = sex.trim().parse::().unwrap(); + let student_state = input_u8("Enter choice: "); - if sex == 1 { - let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Male); - println!("student: {student:#?}"); - io::stdout().flush().unwrap(); - // Get course_id and enroll student - let course_id = get_courses(rust_cohort); - rust_cohort.enroll_student_in_course(student.id, course_id); - } else if sex == 2 { - let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Female); - println!("student: {student:#?}"); - io::stdout().flush().unwrap(); - // Get course_id and enroll student + 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); - } else { - println!("Invalid input"); - io::stdout().flush().unwrap(); - - continue; } - - // println!("Student {first_name} {last_name} added successfully"); - // io::stdout().flush().unwrap(); - + 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, } + } +} - // Get student - if student_state == 2 { - let length = rust_cohort.total_students.len() as u32; - if length == 0 { - println!("No students in the cohort"); - io::stdout().flush().unwrap(); +fn get_student_info() -> (String, String, u8, f32, Sex) { + let first_name = input_any("Enter first name: "); - } else { - let len = length - 1; - print!("Enter student id(Range 0 to {len}): "); - io::stdout().flush().unwrap(); + let last_name = input_any("Enter last name: "); - let mut id = String::new(); - io::stdin().read_line(&mut id).expect("Failed to read line"); - let id = id.trim().parse::().unwrap(); - let student = rust_cohort.get_student_by_id(id); - println!("student: {student:#?}"); - } - } - // Enroll student in course - if student_state == 3{ - // print students - print!("{:#?}", rust_cohort.total_students); - io::stdout().flush().unwrap(); + let age = input_u8("Enter age: "); - print!("Enter student id: "); - io::stdout().flush().unwrap(); + let height = input_f32( "Enter height: "); - let mut id = String::new(); - io::stdin().read_line(&mut id).expect("Failed to read line"); - let id = id.trim().parse::().unwrap(); + 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."), + }; - let course_id = get_courses(rust_cohort); - rust_cohort.enroll_student_in_course(id, course_id); - } - // Exit - if student_state == 4 { - break; - } - } + (first_name, last_name, age, height, sex) } fn create_course(rust_cohort: &mut StudentRegistry) { // create course - print!("Enter course name: "); - io::stdout().flush().unwrap(); - let mut course_name = String::new(); - io::stdin() - .read_line(&mut course_name) - .expect("Failed to read line"); - let course_name = course_name.trim().to_string(); - print!("Enter course capacity: "); - io::stdout().flush().unwrap(); + + let course_name = input_any("Enter course name: "); - let mut course_capacity = String::new(); - io::stdin() - .read_line(&mut course_capacity) - .expect("Failed to read line"); - let course_capacity = course_capacity - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); + let course_capacity = input_u32("Enter course capacity: "); + rust_cohort.add_course(course_name, course_capacity); // rust_cohort @@ -203,182 +91,25 @@ fn create_course(rust_cohort: &mut StudentRegistry) { pub fn main() { let mut rust_cohort = StudentRegistry::new_session(); - // let mut courses: Vec = Vec::new(); - // rust_cohort.add_course("Rust".to_string(), 2); - // let courses = rust_cohort.course_registry.clone(); - // println!("courses: {courses:#?}"); - - // print courses - // println!("courses: {rust_cohort:#?}"); - - - let mut main_state: i32 = 0; - - loop{ - if rust_cohort.course_registry.len() as u32 == 0{ - - print!("1. Add course\n"); - io::stdout().flush().unwrap(); - - let mut input = String::new(); - io::stdin() - .read_line(&mut input) - .expect("Failed to read line"); - - main_state = input - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - - if main_state == 1 { - create_course(&mut rust_cohort); - // break; - }else{ - continue; - } - }else { - print!("1. Add course\n2. Manage students\n3. Exit\n"); - io::stdout().flush().unwrap(); - - let mut input = String::new(); - io::stdin() - .read_line(&mut input) - .expect("Failed to read line"); - - main_state = input - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - - if main_state == 1 { - create_course(&mut rust_cohort); - - }else if main_state == 2 { - student_flow(&mut rust_cohort); - - }else if main_state == 3 { - println!("Bye!"); - break; - }else{ - continue; - } - } - } -} - -pub fn mainz() { - // Student Registry - let mut rust_cohort = StudentRegistry::new_session(); - // let mut courses: Vec = Vec::new(); - - let mut state: i32 = 0; - - // courses = create_course("Rust".to_string(), 2, courses); - // Cli state machine loop { - let mut input = String::new(); - print!("1. Add student\n2. Get student\n3. Exit\n"); - io::stdout().flush().unwrap(); - - io::stdin() - .read_line(&mut input) - .expect("Failed to read line"); - state = input - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - - // Add student - if state == 1 { - print!("Enter first name: "); - io::stdout().flush().unwrap(); - let mut first_name = String::new(); - io::stdin() - .read_line(&mut first_name) - .expect("Failed to read line"); - let first_name = first_name.trim().to_string(); - - print!("Enter last name: "); - io::stdout().flush().unwrap(); - let mut last_name = String::new(); - io::stdin() - .read_line(&mut last_name) - .expect("Failed to read line"); - let last_name = last_name.trim().to_string(); - - print!("Enter age: "); - io::stdout().flush().unwrap(); - let mut age = String::new(); - io::stdin() - .read_line(&mut age) - .expect("Failed to read line"); - let age = age - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - - print!("Enter height: "); - io::stdout().flush().unwrap(); - let mut height = String::new(); - io::stdin() - .read_line(&mut height) - .expect("Failed to read line"); - let height = height - .trim() - .parse::() - .expect("Invalid input. Please enter a number."); - - print!("Enter sex: 1 for male, 2 for female: "); - let mut sex = String::new(); - io::stdout().flush().unwrap(); - io::stdin() - .read_line(&mut sex) - .expect("Failed to read line"); - let sex = sex.trim().parse::().unwrap(); + if rust_cohort.course_registry.is_empty() { + create_course(&mut rust_cohort); + } - if sex == 1 { - let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Male); - println!("student: {student:#?}"); - } else if sex == 2 { - let student = rust_cohort.register(first_name.clone(), last_name.clone(), age, height, Sex::Female); - println!("student: {student:#?}"); - } else { - println!("Invalid input"); - io::stdout().flush().unwrap(); - - continue; - } + println!("1. Add course\n2. Manage students\n3. Exit"); - // println!("Student {first_name} {last_name} added successfully"); - // io::stdout().flush().unwrap(); + 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, } + } - // Get student - if state == 2 { - let length = rust_cohort.total_students.len() as u32; - if length == 0 { - println!("No students in the cohort"); - io::stdout().flush().unwrap(); - - } else { - let len = length - 1; - print!("Enter student id(Range 0 to {len}): "); - io::stdout().flush().unwrap(); + println!("Bye!"); +} - let mut id = String::new(); - io::stdin().read_line(&mut id).expect("Failed to read line"); - let id = id.trim().parse::().unwrap(); - let student = rust_cohort.get_student_by_id(id); - println!("student: {student:#?}"); - } - } - // Exit - if state == 3 { - println!("Bye!"); - break; - } - } -} \ No newline at end of file diff --git a/src/student_registry_project/implementations/student_registry.rs b/src/student_registry_project/implementations/student_registry.rs index ce85197..3f3da43 100644 --- a/src/student_registry_project/implementations/student_registry.rs +++ b/src/student_registry_project/implementations/student_registry.rs @@ -11,7 +11,7 @@ impl StudentRegistry { } // add course - pub fn add_course(&mut self, _name: String, _capacity: u32) { + pub fn add_course(&mut self, _name: String, _capacity: u32) { let course_id: u32 = self.course_registry.len() as u32 + 1; self.course_registry.push(Course { id: course_id, @@ -48,7 +48,7 @@ impl StudentRegistry { // util function to get student by id pub fn get_student_by_id(&self, id: u32) -> Option<&Student> { - self.total_students.get(id as usize) + self.total_students.get(id as usize) ////////////// } // pub fn get_courses(&self) -> &Vec { @@ -63,9 +63,9 @@ impl StudentRegistry { let student_index: usize = (student_id - 1).try_into().unwrap(); let course_index: usize = (course_id - 1).try_into().unwrap(); - // Check if the student is already enrolled in the course - if self.total_students[student_index].enrolled_course_ids.contains(&course_id) { - println!("Student is already enrolled in the course."); + // Check if the student exists + if student_index >= self.total_students.len() { + println!("Student not found."); return; } @@ -75,12 +75,13 @@ impl StudentRegistry { return; } - // Check if the student exists - if student_index >= self.total_students.len() { - println!("Student not found."); + // Check if the student is already enrolled in the course + if self.total_students[student_index].enrolled_course_ids.contains(&course_id) { + 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 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 From c15032bea88a6668079908766f0cf5a707a5c4bb Mon Sep 17 00:00:00 2001 From: RichoKD Date: Sun, 18 Aug 2024 23:23:31 +0100 Subject: [PATCH 4/4] added hashmaps to course and student structs --- src/student_registry_project/ex_1.rs | 4 --- .../implementations/student_registry.rs | 35 ++++++++++++------- .../types/basic_types.rs | 6 ++-- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/student_registry_project/ex_1.rs b/src/student_registry_project/ex_1.rs index 8eb75ee..8924caa 100644 --- a/src/student_registry_project/ex_1.rs +++ b/src/student_registry_project/ex_1.rs @@ -7,8 +7,6 @@ use crate::student_registry_project::utils::{ input_any, input_f32, input_u32, i fn get_courses(rust_cohort: &mut StudentRegistry) -> u32 { println!("{:#?}", rust_cohort.course_registry); - // print!("Enter course id: \n"); - let course_id = input_u32("Enter course id: "); course_id @@ -78,7 +76,6 @@ fn get_student_info() -> (String, String, u8, f32, Sex) { } fn create_course(rust_cohort: &mut StudentRegistry) { - // create course let course_name = input_any("Enter course name: "); @@ -86,7 +83,6 @@ fn create_course(rust_cohort: &mut StudentRegistry) { rust_cohort.add_course(course_name, course_capacity); - // rust_cohort } pub fn main() { diff --git a/src/student_registry_project/implementations/student_registry.rs b/src/student_registry_project/implementations/student_registry.rs index 3f3da43..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, Course, Student, StudentRegistry}; +use std::collections::HashMap; impl StudentRegistry { // this initializes a new StudentRegistry @@ -11,13 +12,17 @@ impl StudentRegistry { } // add course - pub fn add_course(&mut self, _name: String, _capacity: u32) { + 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: Vec::new(), + enrolled_student_ids: _enrolled_student_ids, }); } @@ -32,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, @@ -39,7 +45,7 @@ impl StudentRegistry { age: _age, height: _height, sex: _sex, - enrolled_course_ids: Vec::new(), + enrolled_course_ids: HashMap::new(), }; self.total_students.push(student.clone()); student @@ -48,18 +54,14 @@ impl StudentRegistry { // util function to get student by id pub fn get_student_by_id(&self, id: u32) -> Option<&Student> { - self.total_students.get(id as usize) ////////////// + self.total_students.get(id as usize) } - // pub fn get_courses(&self) -> &Vec { - - // } // 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 = self.total_students.iter().position(|s| s.id == student_id).unwrap(); - // let course_index = self.course_registry.iter().position(|c| c.id == course_id).unwrap(); + let student_index: usize = (student_id - 1).try_into().unwrap(); let course_index: usize = (course_id - 1).try_into().unwrap(); @@ -76,7 +78,11 @@ impl StudentRegistry { } // Check if the student is already enrolled in the course - if self.total_students[student_index].enrolled_course_ids.contains(&course_id) { + if self.total_students[student_index] + .enrolled_course_ids + .get(&course_id) + .is_some() + { println!("Student is already enrolled in the course."); return; } @@ -85,10 +91,15 @@ impl StudentRegistry { // 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.push(student_id); + 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.push(course_id); + 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/types/basic_types.rs b/src/student_registry_project/types/basic_types.rs index b661753..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,7 +8,7 @@ pub struct Student { pub age: u8, pub height: f32, pub sex: Sex, - pub enrolled_course_ids: Vec, + pub enrolled_course_ids: HashMap, } #[derive(Debug, Clone)] @@ -20,7 +22,7 @@ pub struct Course { pub id: u32, pub name: String, pub capacity: u32, - pub enrolled_student_ids: Vec, + pub enrolled_student_ids: HashMap, }