This commit is contained in:
2024-05-15 17:45:14 -04:00
parent 533b39b4ab
commit 469f4b022d

View File

@@ -18,6 +18,15 @@ impl Trader {
balances: HashMap::new() balances: HashMap::new()
} }
} }
fn add_balance(&mut self, cur:i32, delta:u64) {
self.balances.entry(cur).and_modify(|ent| *ent+=delta ).or_insert_with(|| delta);
}
fn sub_balance(&mut self, cur:i32, delta:u64) {
self.balances.entry(cur).and_modify(|ent| *ent-=delta );
}
fn get_balance(&self, cur:i32) -> u64 {
*self.balances.get(&cur).map(|bal| bal).unwrap_or(&0u64)
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -232,6 +241,11 @@ fn fxp_mult(a: u64, b: u64) -> u64 {
} }
fn main() { fn main() {
let mut t1=Trader::new("Teppy",1);
t1.add_balance(1,100);
t1.add_balance(2,150);
t1.sub_balance(1, 25);
println!("Balances are {} and {}",t1.get_balance(1),t1.get_balance(2));
let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, SOL type is 12 let mut m=Market::new(); // USD type is 1, EUR type is 2, BTC type is 10, ETH type is 11, SOL type is 12
let usd=m.register_asset("USD"); let usd=m.register_asset("USD");
let btc=m.register_asset("BTC"); let btc=m.register_asset("BTC");