-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
58 lines (53 loc) · 1.7 KB
/
Account.java
File metadata and controls
58 lines (53 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class Account
{
private UserInputService UserInputService;
private FileService FileService;
/**
* Account constructor: Instantiates required services
*/
public Account()
{
UserInputService = new UserInputService();
FileService = new FileService();
}
/**
* Deposit to the account. Prompts user for a deposit amount and adds a record of the transaction.
*/
public void Deposit()
{
var depositAmount = UserInputService.PromptUserForInput(PromptEnum.Deposit);
FileService.AddTransaction(depositAmount);
}
/**
* Withdraw from the account. Prompts user for a withdrawal amount and adds a record of the transaction.
*/
public void Withdraw()
{
var withdrawAmount = UserInputService.PromptUserForInput(PromptEnum.Withdraw);
FileService.AddTransaction("-" + withdrawAmount);
}
/**
* Determine the balance of the account. Displays the current account balance to the user.
*/
public void Balance()
{
var balance = ReadBalanceFromFile();
var message = "The current balance is: ";
var balanceSign = balance < 0 ? "-" : "";
message += String.format("%s$%.2f", balanceSign, Math.abs(balance));
System.out.println(message);
}
/**
* Retrieves a list of all transactions for the account and aggregates them into an account balance.
* @return The sum of all account transactions
*/
private double ReadBalanceFromFile()
{
var transactions = FileService.ReadAllTransactions();
var balance = transactions
.stream()
.mapToDouble(d -> d)
.sum();
return balance;
}
}