xref: /aosp_15_r20/external/dagger2/java/dagger/example/atm/DepositCommand.java (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1 /*
2  * Copyright (C) 2019 The Dagger Authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package dagger.example.atm;
18 
19 import dagger.example.atm.Database.Account;
20 import java.math.BigDecimal;
21 import javax.inject.Inject;
22 
23 /** Deposits money to the ATM. */
24 final class DepositCommand extends BigDecimalCommand {
25   private final Outputter outputter;
26   private final Account account;
27   private final WithdrawalLimiter withdrawalLimiter;
28 
29   @Inject
DepositCommand(Outputter outputter, Account account, WithdrawalLimiter withdrawalLimiter)30   DepositCommand(Outputter outputter, Account account, WithdrawalLimiter withdrawalLimiter) {
31     super(outputter);
32     this.outputter = outputter;
33     this.account = account;
34     this.withdrawalLimiter = withdrawalLimiter;
35   }
36 
37   @Override
handleAmount(BigDecimal amount)38   protected void handleAmount(BigDecimal amount) {
39     account.deposit(amount);
40     withdrawalLimiter.recordDeposit(amount);
41     outputter.output("your new balance is: " + account.balance());
42   }
43 }
44