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 - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
(Locked)
What is encapsulation?2m 16s
-
(Locked)
Discovering access modifiers2m 35s
-
(Locked)
Implementing encapsulation with access modifiers4m 20s
-
(Locked)
Exploring encapsulation in Java string class3m 37s
-
(Locked)
Challenge: Banking application1m 14s
-
(Locked)
Solution: Banking application4m 29s
-
(Locked)
-
-
-
-