Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Amount = f64;
- trait TObj {
- fn get_name(&self) -> &str;
- fn get_price(&self) -> Amount;
- fn get_qty(&self) -> Amount;
- fn set_qty(&mut self, v: Amount);
- fn amount(&self) -> Amount { self.get_price() * self.get_qty() }
- }
- trait TObjTax: TObj {
- fn get_taxrate(&self) -> Amount;
- fn tax(&self) -> Amount { self.amount() * self.get_taxrate() / 100. }
- fn prn(&self) { println!("{} - amount: {}, tax: {}", self.get_name(), self.amount(), self.tax()); }
- }
- struct Obj {
- name: String,
- price: Amount,
- qty: Amount,
- taxrate: Amount,
- }
- impl TObj for Obj {
- fn get_name(&self) -> &str { &self.name }
- fn get_price(&self) -> Amount { self.price }
- fn get_qty(&self) -> Amount { self.qty }
- fn set_qty(&mut self, v: Amount) { self.qty = v; }
- }
- impl TObjTax for Obj {
- fn get_taxrate(&self) -> Amount { self.taxrate }
- }
- fn main() {
- let mut obj = Obj {
- name: "Веник электрический".to_string(),
- price: 12.3,
- qty: 10.,
- taxrate: 18.,
- };
- obj.prn();
- obj.set_qty(50.);
- obj.prn();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement