Status Report
Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 22 of 61.
Challenge
EasyYour virtual pet is coming along nicely! Now let's give it the ability to report on how it's feeling. In this step, you'll add a status method that provides a complete overview of your pet's current state.
The status report should describe your pet's condition based on its energy and hunger levels. Your pet can be in different moods depending on how well you've been taking care of it:
- If energy is
50or above AND hunger is50or below, your pet is happy - Otherwise, your pet is tired
Add a status method to your Pet that takes &self and prints a full status report showing the pet's name, energy, hunger, and mood.
The status report should follow this exact format:
--- Status Report ---
Name: {name}
Energy: {energy}
Hunger: {hunger}
Mood: {mood}In your main.rs, read the pet's name, energy, and hunger values, set up your pet with those values, and then call the status method to display the report.
For example, if the input is:
Buddy
70
30The output should be:
--- Status Report ---
Name: Buddy
Energy: 70
Hunger: 30
Mood: happyAnd if the input is:
Max
40
60The output should be:
--- Status Report ---
Name: Max
Energy: 40
Hunger: 60
Mood: tiredThis method gives you a quick way to check on your pet's wellbeing at any time!
Try it yourself
mod pet;
use pet::Pet;
fn main() {
let mut name = String::new();
std::io::stdin().read_line(&mut name).expect("Failed to read line");
let name = name.trim().to_string();
let mut energy_input = String::new();
std::io::stdin().read_line(&mut energy_input).expect("Failed to read line");
let initial_energy: u32 = energy_input.trim().parse().expect("Failed to parse energy");
let mut hunger_input = String::new();
std::io::stdin().read_line(&mut hunger_input).expect("Failed to read line");
let initial_hunger: u32 = hunger_input.trim().parse().expect("Failed to parse hunger");
let mut my_pet = Pet::new(name);
my_pet.set_energy(initial_energy);
my_pet.set_hunger(initial_hunger);
my_pet.play();
println!("Energy: {}", my_pet.energy());
println!("Hunger: {}", my_pet.hunger());
}
All lessons in Object Oriented Programming
1Methods and Behavior
Intro Implementation BlocksThe Self ParameterMutable MethodsAssociated FunctionsMultiple Implementation BlocksMethod ChainingRecap - Rectangle Actions4Project: Virtual Pet
Defining the PetFeeding the Pet7Standard Traits
The Debug TraitThe Display TraitClone and CopyEquality TraitsRecap - Printable Point10Project: Document System
The Draw TraitText Component2Encapsulation and Modules
Modules BasicsThe Public KeywordPrivate FieldsGettersSettersRecap - Secure Locker5Generics
Generic StructsGeneric MethodsMultiple Generic TypesGeneric FunctionsRecap - Coordinate Point8Traits as Bounds
Trait Bounds SyntaxMultiple BoundsThe Where ClauseReturning Types with TraitsRecap - Generic Printer11Design Patterns in Rust
Newtype PatternCompositionThe Drop TraitFrom and IntoRecap - Smart Pointer Mock