-
Notifications
You must be signed in to change notification settings - Fork 7
chore: St registry assignment #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RichoKD
wants to merge
4
commits into
BlockheaderWeb3-Community:st-registry-assignment
Choose a base branch
from
RichoKD:st-registry-assignment
base: st-registry-assignment
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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!"); | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
|
|
||
| } | ||
| // } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.