Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. type Amount = f64;
  2.  
  3. trait TObj {
  4. fn get_name(&self) -> &str;
  5. fn get_price(&self) -> Amount;
  6. fn get_qty(&self) -> Amount;
  7.  
  8. fn set_qty(&mut self, v: Amount);
  9.  
  10. fn amount(&self) -> Amount { self.get_price() * self.get_qty() }
  11.  
  12. fn prn(&self) { println!("{} - amount: {}", self.get_name(), self.amount()); }
  13. }
  14.  
  15. trait TObjTax: TObj {
  16. fn get_taxrate(&self) -> Amount;
  17.  
  18. fn tax(&self) -> Amount { self.amount() * self.get_taxrate() / 100. }
  19.  
  20. fn prn(&self) { println!("{} - amount: {}, tax: {}", self.get_name(), self.amount(), self.tax()); }
  21. }
  22.  
  23. struct Obj {
  24. name: String,
  25. price: Amount,
  26. qty: Amount,
  27. taxrate: Amount,
  28. }
  29.  
  30. impl TObj for Obj {
  31. fn get_name(&self) -> &str { &self.name }
  32. fn get_price(&self) -> Amount { self.price }
  33. fn get_qty(&self) -> Amount { self.qty }
  34.  
  35. fn set_qty(&mut self, v: Amount) { self.qty = v; }
  36. }
  37.  
  38. impl TObjTax for Obj {
  39. fn get_taxrate(&self) -> Amount { self.taxrate }
  40. }
  41.  
  42. fn main() {
  43. let mut obj = Obj {
  44. name: "Веник электрический".to_string(),
  45. price: 12.3,
  46. qty: 10.,
  47. taxrate: 18.,
  48. };
  49. TObj::prn(&obj);
  50. TObjTax::prn(&obj);
  51.  
  52. obj.set_qty(50.);
  53. TObjTax::prn(&obj);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement