use std::sync::atomic::{AtomicUsize, Ordering};
use std::fmt;
static TOTAL_ITEMS_CREATED: AtomicUsize = AtomicUsize::new(0);
struct Item {
name: String,
description: String,
use_time: u32,
use_count: u32,
}
impl Item {
fn new(name: &str, description: &str, use_time: u32) -> Item {
TOTAL_ITEMS_CREATED.fetch_add(1, Ordering::SeqCst);
Item {
name: name.to_string(),
description: description.to_string(),
use_time,
use_count: 0,
}
}
fn use_item(&mut self) -> String {
self.use_count += 1;
format!(
"Používá se {} ({}), což trvá {} sekund. Počet použití: {}.",
self.name, self.description, self.use_time, self.use_count
)
}
}
fn get_total_items_created() -> usize {
TOTAL_ITEMS_CREATED.load(Ordering::SeqCst)
}
struct Survik {
name: String,
item: Item,
}
impl Survik {
fn new(name: &str, item: Item) -> Survik {
Survik {
name: name.to_string(),
item,
}
}
fn set_item(&mut self, item: Item) {
self.item = item;
}
fn use_item(&mut self) {
let item_line = self.item.use_item();
println!("{} používá {}: {}", self.name, self.item.name, item_line);
}
}
fn main() {
let mut baterka = Item::new("Baterka", "osvětluje cestu", 0);
println!("{}", baterka.use_item());
let mut lekarnicka = Item::new("Lékárnička", "léčí zranění", 20);
println!("{}", lekarnicka.use_item());
println!("Celkový počet vytvořených instancí Item: {}", get_total_items_created());
let mut pepa = Survik::new("Pepik the survik", baterka);
pepa.use_item();
pepa.set_item(lekarnicka);
pepa.use_item();
}