completed 03/[00-07]

This commit is contained in:
2025-02-24 15:53:03 +05:30
parent 7910a25928
commit 9161d08b1c
8 changed files with 116 additions and 25 deletions

View File

@@ -1,6 +1,6 @@
fn intro() -> &'static str { fn intro() -> &'static str {
// TODO: fix me 👇 // TODO: fix me 👇
"I'm ready to __!" "I'm ready to start modelling a software ticket!"
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -5,6 +5,17 @@
// It should also have a method named `is_available` that returns a `true` if the quantity is // It should also have a method named `is_available` that returns a `true` if the quantity is
// greater than 0, otherwise `false`. // greater than 0, otherwise `false`.
struct Order{
price: u32,
quantity: u32,
}
impl Order{
fn is_available(self)-> bool{
self.quantity >0
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View File

@@ -18,7 +18,23 @@ impl Ticket {
// as well as some `String` methods. Use the documentation of Rust's standard library // as well as some `String` methods. Use the documentation of Rust's standard library
// to find the most appropriate options -> https://doc.rust-lang.org/std/string/struct.String.html // to find the most appropriate options -> https://doc.rust-lang.org/std/string/struct.String.html
fn new(title: String, description: String, status: String) -> Self { fn new(title: String, description: String, status: String) -> Self {
todo!(); if !(status.eq("To-Do") || status.eq("In Progress") || status.eq("Done")) {
panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
}
if title.is_empty() {
panic!("Title cannot be empty")
}
if description.is_empty() {
panic!("Description cannot be empty")
}
if description.len() > 500 {
panic!("Description cannot be longer than 500 bytes");
}
if title.len() > 50 {
panic!("Title cannot be longer than 50 bytes");
}
Self { Self {
title, title,
description, description,

View File

@@ -1,7 +1,7 @@
mod helpers { mod helpers {
// TODO: Make this code compile, either by adding a `use` statement or by using // TODO: Make this code compile, either by adding a `use` statement or by using
// the appropriate path to refer to the `Ticket` struct. // the appropriate path to refer to the `Ticket` struct.
use super::Ticket;
fn create_todo_ticket(title: String, description: String) -> Ticket { fn create_todo_ticket(title: String, description: String) -> Ticket {
Ticket::new(title, description, "To-Do".into()) Ticket::new(title, description, "To-Do".into())
} }

View File

@@ -1,12 +1,12 @@
mod ticket { mod ticket {
struct Ticket { pub struct Ticket {
title: String, title: String,
description: String, description: String,
status: String, status: String,
} }
impl Ticket { impl Ticket {
fn new(title: String, description: String, status: String) -> Ticket { pub fn new(title: String, description: String, status: String) -> Ticket {
if title.is_empty() { if title.is_empty() {
panic!("Title cannot be empty"); panic!("Title cannot be empty");
} }
@@ -55,7 +55,7 @@ mod tests {
// //
// TODO: Once you have verified that the below does not compile, // TODO: Once you have verified that the below does not compile,
// comment the line out to move on to the next exercise! // comment the line out to move on to the next exercise!
assert_eq!(ticket.description, "A description"); // assert_eq!(ticket.description, "A description");
} }
fn encapsulation_cannot_be_violated() { fn encapsulation_cannot_be_violated() {
@@ -68,10 +68,10 @@ mod tests {
// //
// TODO: Once you have verified that the below does not compile, // TODO: Once you have verified that the below does not compile,
// comment the lines out to move on to the next exercise! // comment the lines out to move on to the next exercise!
let ticket = Ticket { // let ticket = Ticket {
title: "A title".into(), // title: "A title".into(),
description: "A description".into(), // description: "A description".into(),
status: "To-Do".into(), // status: "To-Do".into(),
}; // };
} }
} }

View File

@@ -29,6 +29,16 @@ pub mod ticket {
status, status,
} }
} }
pub fn title(self) -> String {
self.title
}
pub fn description(self) -> String {
self.description
}
pub fn status(self) -> String {
self.status
}
// TODO: Add three public methods to the `Ticket` struct: // TODO: Add three public methods to the `Ticket` struct:
// - `title` that returns the `title` field. // - `title` that returns the `title` field.

View File

@@ -34,16 +34,16 @@ impl Ticket {
} }
} }
pub fn title(self) -> String { pub fn title(&self) -> String {
self.title self.title.clone()
} }
pub fn description(self) -> String { pub fn description(&self) -> String {
self.description self.description.clone()
} }
pub fn status(self) -> String { pub fn status(&self) -> String {
self.status self.status.clone()
} }
} }

View File

@@ -10,23 +10,62 @@ pub struct Ticket {
} }
impl Ticket { impl Ticket {
pub fn new(title: String, description: String, status: String) -> Ticket { fn cTitleEmpty(newString: &String) -> bool {
if title.is_empty() { if newString.is_empty() {
panic!("Title cannot be empty"); panic!("Title cannot be empty");
} }
if title.len() > 50 { false
panic!("Title cannot be longer than 50 bytes");
}
if description.is_empty() {
panic!("Description cannot be empty");
}
if description.len() > 500 {
panic!("Description cannot be longer than 500 bytes");
}
if status != "To-Do" && status != "In Progress" && status != "Done" {
panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
} }
fn cTitleTooLong(newString: &String) -> bool {
if newString.len() > 50 {
panic!("Title cannot be longer than 50 bytes");
}
false
}
fn cDescTooLong(newDesc: &String) -> bool {
if newDesc.len() > 500 {
panic!("Description cannot be longer than 500 bytes");
}
false
}
fn cDescEmpty(newDesc: &String) -> bool {
if newDesc.is_empty() {
panic!("Description cannot be empty");
}
false
}
fn cInvalidStatus(newStatus: &String) -> bool {
if newStatus != "To-Do" && newStatus != "In Progress" && newStatus != "Done" {
panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
}
false
}
pub fn new(title: String, description: String, status: String) -> Ticket {
// if title.is_empty() {
// panic!("Title cannot be empty");
// }
// if title.len() > 50 {
// panic!("Title cannot be longer than 50 bytes");
// }
// if description.is_empty() {
// panic!("Description cannot be empty");
// }
// if description.len() > 500 {
// panic!("Description cannot be longer than 500 bytes");
// }
// if status != "To-Do" && status != "In Progress" && status != "Done" {
// panic!("Only `To-Do`, `In Progress`, and `Done` statuses are allowed");
// }
Ticket::cTitleTooLong(&title);
Ticket::cTitleTooLong(&title);
Ticket::cDescEmpty(&description);
Ticket::cDescTooLong(&description);
Ticket::cInvalidStatus(&status);
Ticket { Ticket {
title, title,
description, description,
@@ -34,6 +73,21 @@ impl Ticket {
} }
} }
pub fn set_title(&mut self, newTitle: String) {
Ticket::cTitleEmpty(&newTitle);
Ticket::cTitleTooLong(&newTitle);
self.title = newTitle;
}
pub fn set_description(&mut self, newDesc: String) {
Ticket::cDescEmpty(&newDesc);
Ticket::cDescTooLong(&newDesc);
self.description = newDesc;
}
pub fn set_status(&mut self, newStatus: String) {
Ticket::cInvalidStatus(&newStatus);
self.status = newStatus;
}
pub fn title(&self) -> &String { pub fn title(&self) -> &String {
&self.title &self.title
} }