templa revidoval tento gist . Přejít na revizi
1 file changed, 0 insertions, 0 deletions
gistfile1.txt přejmenováno na gistfile1.rs
Soubor přejmenován beze změn
templa revidoval tento gist . Přejít na revizi
1 file changed, 53 insertions
gistfile1.txt(vytvořil soubor)
@@ -0,0 +1,53 @@ | |||
1 | + | use std::io; | |
2 | + | ||
3 | + | struct BankAccount { | |
4 | + | account_holder: String, | |
5 | + | balance: f64, | |
6 | + | } | |
7 | + | ||
8 | + | impl BankAccount { | |
9 | + | // Создание новой учетной записи | |
10 | + | fn new(account_holder: String, initial_balance: f64) -> BankAccount { | |
11 | + | BankAccount { | |
12 | + | account_holder, | |
13 | + | balance: initial_balance, | |
14 | + | } | |
15 | + | } | |
16 | + | ||
17 | + | // Метод для пополнения счета | |
18 | + | fn deposit(&mut self, amount: f64) { | |
19 | + | if amount > 0.0 { | |
20 | + | self.balance += amount; | |
21 | + | println!("Deposited: {}", amount); | |
22 | + | } else { | |
23 | + | println!("Invalid deposit amount."); | |
24 | + | } | |
25 | + | } | |
26 | + | ||
27 | + | // Метод для снятия средств | |
28 | + | fn withdraw(&mut self, amount: f64) { | |
29 | + | if amount > 0.0 && amount <= self.balance { | |
30 | + | self.balance -= amount; | |
31 | + | println!("Withdrawn: {}", amount); | |
32 | + | } else { | |
33 | + | println!("Invalid or insufficient funds."); | |
34 | + | } | |
35 | + | } | |
36 | + | ||
37 | + | // Метод для отображения текущего баланса | |
38 | + | fn show_balance(&self) { | |
39 | + | println!("Account Holder: {}", self.account_holder); | |
40 | + | println!("Balance: {:.2}", self.balance); | |
41 | + | } | |
42 | + | } | |
43 | + | ||
44 | + | fn main() { | |
45 | + | // Создаем объект банковского аккаунта | |
46 | + | let mut account = BankAccount::new("John Doe".to_string(), 1000.0); | |
47 | + | ||
48 | + | // Выполняем операции с аккаунтом | |
49 | + | account.show_balance(); | |
50 | + | account.deposit(500.0); | |
51 | + | account.withdraw(200.0); | |
52 | + | account.show_balance(); | |
53 | + | } |