Menu
Coddy logo textTech

Status Report

Part of the Object Oriented Programming section of Coddy's Rust journey — lesson 22 of 61.

challenge icon

Challenge

Easy

Your 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 50 or above AND hunger is 50 or 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
30

The output should be:

--- Status Report ---
Name: Buddy
Energy: 70
Hunger: 30
Mood: happy

And if the input is:

Max
40
60

The output should be:

--- Status Report ---
Name: Max
Energy: 40
Hunger: 60
Mood: tired

This 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