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
36 changes: 19 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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:#?}");


}

111 changes: 111 additions & 0 deletions src/student_registry_project/ex_1.rs
Original file line number Diff line number Diff line change
@@ -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!");
}

69 changes: 68 additions & 1 deletion src/student_registry_project/implementations/student_registry.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
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
// new array of students
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<u32> = Vec::with_capacity(_capacity as usize);
let _enrolled_student_ids: HashMap<u32, String> = 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,
Expand All @@ -20,13 +37,15 @@ 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,
id: student_id,
age: _age,
height: _height,
sex: _sex,
enrolled_course_ids: HashMap::new(),
};
self.total_students.push(student.clone());
student
Expand All @@ -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.");
}
}
}
3 changes: 2 additions & 1 deletion src/student_registry_project/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod implementations;
pub mod types;
pub mod utils;
pub mod utils;
pub mod ex_1;
13 changes: 13 additions & 0 deletions src/student_registry_project/types/basic_types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct Student {
pub first_name: String,
Expand All @@ -6,6 +8,7 @@ pub struct Student {
pub age: u8,
pub height: f32,
pub sex: Sex,
pub enrolled_course_ids: HashMap<u32, String>,
}

#[derive(Debug, Clone)]
Expand All @@ -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<u32, String>,
}


#[derive(Debug, Clone)]
pub struct StudentRegistry {
pub total_students: Vec<Student>,
pub course_registry: Vec<Course>,
}

/***
Expand Down
42 changes: 42 additions & 0 deletions src/student_registry_project/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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::<u32>()
.expect("Invalid input. Please enter a number.")
}

pub fn input_u32(prompt: &str) -> u32 {
input_any(prompt)
.trim()
.parse::<u32>()
.expect("Invalid input. Please enter a number.")
}

pub fn input_u8(prompt: &str) -> u8 {
input_any(prompt)
.trim()
.parse::<u8>()
.expect("Invalid input. Please enter a number.")
}

pub fn input_f32(prompt: &str) -> f32 {
input_any(prompt)
.trim()
.parse::<f32>()
.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()

}
// }