completed 03-ticket example
This commit is contained in:
@@ -6,16 +6,16 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn u16_size() {
|
||||
assert_eq!(size_of::<u16>(), todo!());
|
||||
assert_eq!(size_of::<u16>(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn i32_size() {
|
||||
assert_eq!(size_of::<i32>(), todo!());
|
||||
assert_eq!(size_of::<i32>(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bool_size() {
|
||||
assert_eq!(size_of::<bool>(), todo!());
|
||||
assert_eq!(size_of::<bool>(), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn string_size() {
|
||||
assert_eq!(size_of::<String>(), todo!());
|
||||
assert_eq!(size_of::<String>(), 24);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -23,6 +23,6 @@ mod tests {
|
||||
// but, in general, the memory layout of structs is a more complex topic.
|
||||
// If you're curious, check out the "Type layout" section of The Rust Reference
|
||||
// https://doc.rust-lang.org/reference/type-layout.html for more information.
|
||||
assert_eq!(size_of::<Ticket>(), todo!());
|
||||
assert_eq!(size_of::<Ticket>(), 72);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,16 +13,16 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn u16_ref_size() {
|
||||
assert_eq!(size_of::<&u16>(), todo!());
|
||||
assert_eq!(size_of::<&u16>(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn u64_mut_ref_size() {
|
||||
assert_eq!(size_of::<&mut u64>(), todo!());
|
||||
assert_eq!(size_of::<&mut u64>(), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ticket_ref_size() {
|
||||
assert_eq!(size_of::<&Ticket>(), todo!());
|
||||
assert_eq!(size_of::<&Ticket>(), 8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// We'll pick the concept up again in a later chapter after covering traits and
|
||||
// interior mutability.
|
||||
fn outro() -> &'static str {
|
||||
"I have a basic understanding of __!"
|
||||
"I have a basic understanding of destructors!"
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -11,3 +11,74 @@
|
||||
// Integration here has a very specific meaning: they test **the public API** of your project.
|
||||
// You'll need to pay attention to the visibility of your types and methods; integration
|
||||
// tests can't access private or `pub(crate)` items.
|
||||
|
||||
pub struct Order {
|
||||
product_name: String,
|
||||
quantity: u32,
|
||||
unit_price: u32,
|
||||
}
|
||||
|
||||
impl Order {
|
||||
fn cName(name: &String) {
|
||||
if name.is_empty() {
|
||||
panic!("Name must be valid");
|
||||
}
|
||||
if name.len() > 300 {
|
||||
panic!("Name too long");
|
||||
}
|
||||
}
|
||||
|
||||
fn cQuantity(quantity: &u32) {
|
||||
if quantity.eq(&0) {
|
||||
panic!("not enough quantity");
|
||||
}
|
||||
}
|
||||
|
||||
fn cPrice(price: &u32) {
|
||||
if price.eq(&0) {
|
||||
panic!("invalid price");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn total(&self) -> u32 {
|
||||
self.unit_price * self.quantity
|
||||
}
|
||||
|
||||
pub fn new(name: String, quantity: u32, price: u32) -> Order {
|
||||
Order::cName(&name);
|
||||
Order::cPrice(&price);
|
||||
Order::cQuantity(&quantity);
|
||||
Order {
|
||||
product_name: name,
|
||||
quantity,
|
||||
unit_price: price,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn product_name(&self) -> String {
|
||||
self.product_name.clone()
|
||||
}
|
||||
|
||||
pub fn quantity(&self) -> &u32 {
|
||||
&self.quantity
|
||||
}
|
||||
|
||||
pub fn unit_price(&self) -> &u32 {
|
||||
&self.unit_price
|
||||
}
|
||||
|
||||
pub fn set_product_name(&mut self, name: String) {
|
||||
Order::cName(&name);
|
||||
self.product_name = name;
|
||||
}
|
||||
|
||||
pub fn set_quantity(&mut self, quantity: u32) {
|
||||
Order::cQuantity(&quantity);
|
||||
self.quantity = quantity;
|
||||
}
|
||||
|
||||
pub fn set_unit_price(&mut self, price: u32) {
|
||||
Order::cPrice(&price);
|
||||
self.unit_price = price;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user