Menu
Coddy logo textTech

Final Interaction

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

challenge icon

Challenge

Easy

Congratulations on building your Virtual Pet! In this final step, you'll bring everything together by simulating a complete interaction session with your pet. This is where all your methods work in harmony!

Your pet module already has everything it needs: feeding, playing, and status reporting. Now you'll create a simulation that shows a day in the life of your virtual companion.

In your main.rs, you'll read your pet's name and then perform a sequence of interactions:

  1. Create a new pet with the given name
  2. Display the initial status report
  3. Play with your pet twice
  4. Feed your pet once
  5. Display the final status report

Between the two status reports, print a separator line to show the transition:

--- After Activities ---

For example, if the input is:

Buddy

The output should be:

--- Status Report ---
Name: Buddy
Energy: 100
Hunger: 0
Mood: happy
Buddy had fun playing!
Buddy had fun playing!
Buddy enjoyed the meal!
--- After Activities ---
--- Status Report ---
Name: Buddy
Energy: 60
Hunger: 0
Mood: happy

Remember how the stats change: playing costs 20 energy and adds 15 hunger each time, while feeding reduces hunger by 30. After two play sessions and one feeding, your pet's energy drops from 100 to 60, and hunger goes from 0 to 30 then back to 0.

This final challenge showcases the complete Virtual Pet system you've built—a fully encapsulated module with private fields, getters, setters, and methods that work together to create an interactive experience!

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.status();
}

All lessons in Object Oriented Programming