From the course: Java Object-Oriented Programming

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Solution: Banking application

Solution: Banking application - Java Tutorial

From the course: Java Object-Oriented Programming

Solution: Banking application

(upbeat music) - [Instructor] To add encapsulation to this Java program, the first thing we'll do is make the owner and balance attributes private. Then we'll make these indirectly accessible through public behaviors. In order to create a BankAccount, we need a constructor. With this constructor, we'll input an owner, as well as a startingBalance. For the balance, the user may input an invalid value, such as a negative number. To combat against this, we'll take the max of the startingBalance and zero. This will set the balance to whichever number's higher, either the startingBalance or zero, forcing the balance to be set as a positive number. Now for the setters. We'll make the owner of the BankAccount immutable for now, so no setter is needed. But what about the balance? For the balance, the user should be able to withdraw and deposit money. So instead of creating a setter, we'll create deposit and withdrawal…

Contents