SHARE
TWEET

Untitled

a guest Sep 20th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.     let t = Thailand {};
  3.     //    let u: USA = Box::new();
  4.  
  5.     //let dyn_tax_calculator = Box::<dyn CompoundTax>::new();
  6.     //let usa = Box::new(USA {
  7.     //   tax_calculator: dyn_tax_calculator,
  8.     //});
  9.  
  10.     //println!(usa.compound_tax(333, 2.3, 3.3));
  11. }
  12.  
  13. struct Thailand {}
  14. struct USA {
  15.     pub tax_calculator: Box<dyn CompoundTax>,
  16. }
  17.  
  18. trait SimpleTax {
  19.     fn calculate_tax(&self, amount: f32, rate: f32) -> f32;
  20. }
  21.  
  22. trait CompoundTax: SimpleTax {
  23.     fn compound_tax(&self, amount: f32, base_rate: f32, rate: f32) -> f32;
  24. }
  25.  
  26. impl dyn SimpleTax {
  27.     fn calculate_tax(&self, amount: f32, rate: f32) -> f32 {
  28.         amount * rate
  29.     }
  30. }
  31.  
  32. impl dyn CompoundTax
  33. where
  34.     Self: SimpleTax,
  35. {
  36.     fn compound_tax(&self, amount: f32, base_rate: f32, compound_rate: f32) -> f32 {
  37.         let base_tax = &self.calculate_tax(amount, base_rate);
  38.         base_tax + amount * compound_rate
  39.     }
  40. }
  41.  
  42. impl SimpleTax for USA {
  43.     fn calculate_tax(&self, amount: f32, rate: f32) -> f32 {
  44.         amount * rate
  45.     }
  46. }
  47.  
  48. impl CompoundTax for USA {
  49.     fn compound_tax(&self, amount: f32, base_rate: f32, compound_rate: f32) -> f32 {
  50.         let base_tax = &self.calculate_tax(amount, base_rate);
  51.         base_tax + amount * compound_rate
  52.     }
  53. }
  54.  
  55. fn get_taxed_amount<C: CompoundTax>(c: C) -> f32 {
  56.     c.compound_tax(200.00, 0.08, 0.015)
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top