Final Interaction
Part of the Object Oriented Programming section of Coddy's Rust journey. Lesson 23 of 61.
Challenge
EasyCongratulations on building your Virtual Pet! In this final step you bring everything together by simulating a full interaction session with your pet, so that every method you wrote works side by side.
Your pet.rs module is given to you complete: new, the getters, set_energy, set_hunger, feed, play and status. You only write main.rs.
You will receive three inputs, the same three as in the earlier steps:
- The pet's name
- The energy the pet starts with
- The hunger the pet starts with
In main.rs, perform this sequence:
- Create the pet with the given name, then set its energy and hunger from the input
- Display the status report
- Play with your pet twice
- Feed your pet once
- Print the separator line
--- After Activities --- - Display the status report again
Remember how the stats change: playing costs 20 energy and adds 15 hunger, feeding takes 30 off the hunger, energy never falls below 0 and hunger never rises above 100. The mood is happy when energy is 50 or more and hunger is 50 or less, and tired otherwise, so a pet can start happy and end the session tired.
For example, with the inputs Luna, 50 and 50:
--- Status Report ---
Name: Luna
Energy: 50
Hunger: 50
Mood: happy
Luna had fun playing!
Luna had fun playing!
Luna enjoyed the meal!
--- After Activities ---
--- Status Report ---
Name: Luna
Energy: 10
Hunger: 50
Mood: tiredThis last challenge shows the complete Virtual Pet system you have built: a module with private fields, getters, setters and methods that work together as one interactive session.
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");
// TODO: create the pet and set its energy and hunger from the input
// TODO: print the first status report
// TODO: play twice, then feed once
// TODO: print the separator "--- After Activities ---"
// TODO: print the final status report
}
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 MockPractice on your own: Online Rust compiler