added more exercise code

This commit is contained in:
hendrik 2024-04-29 15:59:47 +02:00
parent 88317e9ff5
commit 5f9d37fad8
2 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,69 @@
//Problem 2:
/* The code below contains a student struct with three fields.
Your taks is to create a hashmap called student_database in the main function,
which will store instances of the Student structure. The keys of the hashmap
should be unique student IDs, represented as integers while the values will be
instances of the student struct.
Implement a function called add_student() that takes the student's ID, name, age, and grade as parameters.
The function should add a new entry to the student_database hashmap if the student ID doesn't already exist.
If the student ID already exists in the hashmap, the function should not add a new entry.
Use the skeleton of the function given below.
*/
use std::collections::HashMap;
struct Student {
name: String,
age: i32,
grade: String,
}
fn add_student(
student_database: &mut HashMap<i32, Student>,
id: i32,
name: String,
age: i32,
grade: String,
) {
if !student_database.contains_key(&id) {
let student = Student { name: name, age: age, grade: grade };
student_database.insert(id, student);
} else {
println!("Student for {} already exists" , id);
}
}
fn main() {
let mut student_database: HashMap<i32, Student> = HashMap::new();
add_student(
&mut student_database,
1,
String::from("John"),
17,
String::from("Grade 11"),
);
add_student(
&mut student_database,
2,
String::from("Sarah"),
16,
String::from("Grade 10"),
);
// Printing the student database
for (id, student) in &student_database {
println!("Student ID: {}", id);
println!("Name: {}", student.name);
println!("Age: {}", student.age);
println!("Grade: {}", student.grade);
println!("------------------");
}
}

View File

@ -0,0 +1,94 @@
// Problem 1:
/* In this exercise, you will be working on creating a student management system
using Rust. The system should allow you to store and retrieve student information
based on their unique ID. For ease of work, the student structure is already
created in the code below
Next, create a StudentManager structure containing a field of student, which
will essentially be a hashmap where the key part will be an integer representing
unique ID of student and the value part will be the complete details of the
students contained in the student structure.
The StudentManager should implement the following methods:
1. new() -> Self: A constructor that initializes an empty student manager.
2. add_student(&mut self, student: Student) -> Result<(), String>:
Adds a student to the manager.
If the student's ID already exists, return an error message.
Otherwise, add the student to the manager and return Ok.
3. get_student(&self, id: i32) -> Option<&Student>: Retrieves a student
from the manager based on their ID.
If the student is found, return Some(student). Otherwise, return None.
Your task is to implement the StudentManager structure, and the mentioned methods.
Additionally, provide a sample usage of the student management system by adding
a few students and retrieving their information using the get_student() method.
*/
use std::collections::HashMap;
struct Student {
id: i32,
name: String,
grade: String,
}
impl Student {
fn str( &self) {
println!("id: {}, name {}, grade {}", self.id, self.name, self.grade);
}
}
struct StudentManager {
student: HashMap<i32, Student>
}
impl StudentManager {
fn new() -> Self {
Self {
student: HashMap::new()
}
}
fn add_student(&mut self, student: Student) -> Result<(), String> {
if self.student.contains_key(&student.id) {
return Err(String::from("Already defined"));
}
self.student.insert(student.id, student);
return Ok(());
}
fn get_student(&self, id: i32) -> Option<&Student> {
if self.student.contains_key(&id) {
return Some(&self.student.get(&id).unwrap());
}
return None;
}
}
fn add_student(man : &mut StudentManager, student : Student ) {
match man.add_student(student) {
Err(e) => println!("{e}"),
Ok(()) => println!("added"),
};
}
fn main() {
let mut student_man = StudentManager::new();
let student = Student { id: 1, name: String::from("jjj"), grade: String::from("Kkk") };
let student2 = Student { id: 2, name: String::from("jsdsd"), grade: String::from("Ksdsdk") };
let student3 = Student { id: 1, name: String::from("jsdsd"), grade: String::from("Ksdsdk") };
// to centralize handle error
add_student(&mut student_man, student);
add_student(&mut student_man, student2);
add_student(&mut student_man, student3);
//student_man.add_student(student);
//student_man.add_student(student2);
//student_man.add_student(student3);
if let Some(stud) = student_man.get_student(1) {
stud.str();
}
}