0% found this document useful (0 votes)
27 views4 pages

Saving Accounts

OOP Labexercise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views4 pages

Saving Accounts

OOP Labexercise
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Nieva, Michael Vernice B.

BSIT-A306 Object-oriented
Programming August 26,2024

Filename: SavingAccounts

Import java.util.Scanner;

Class SavingsAccount {

Private double balance;

Public static double interestRate = 0;

Public static void setInterestRate(double newRate) {

interestRate = newRate;

Public static double getInterestRate() {

Return interestRate;

Public double getBalance() {

Return balance;

Public void deposit(double amount) {

Balance += amount;

Public double withdraw(double amount) {

If (balance >= amount) {


Balance -= amount;

} else {

Amount = 0;

Return amount;

Public void addInterest() {

Double interest = balance * getInterestRate();

Balance += interest;

Public static void showBalance(SavingsAccount account) {

System.out.println(“Your new balance is: “ + account.getBalance());

Public class SavingAccounts {

Public static void main(String[] args) {

SavingsAccount savings = new SavingsAccount();

Scanner scanner = new Scanner(System.in);

Try {

System.out.print(“Enter Interest Rate: “);

Double interestRateInput = scanner.nextDouble();

SavingsAccount.setInterestRate(interestRateInput);
System.out.print(“Enter Deposit Amount: “);

Double initialDeposit = scanner.nextDouble();

Savings.deposit(initialDeposit);

System.out.println(“Your balance is: “ + initialDeposit);

Boolean exitFlag = false;

While (!exitFlag) {

System.out.print(“Press D for another deposit, W to withdraw, or E


to exit: “);

String transactionType = scanner.next().toLowerCase();

If (transactionType.equals(“d”)) {

System.out.print(“Enter Deposit Amount: “);

Double depositAmount = scanner.nextDouble();

Savings.deposit(depositAmount);

Savings.addInterest();

SavingsAccount.showBalance(savings);

} else if (transactionType.equals(“w”)) {

System.out.print(“Enter Withdraw Amount: “);

Double withdrawAmount = scanner.nextDouble();

Savings.withdraw(withdrawAmount);

System.out.println(“You have successfully withdrawn: “ +


withdrawAmount);

SavingsAccount.showBalance(savings);

} else if (transactionType.equals(“e”)) {

System.out.println(“Thank you! Please come again.”);

exitFlag = true;
} else {

System.out.println(“Invalid input. Please try again.”);

} catch (Exception e) {

System.out.println(“Invalid input. Please enter a valid number.”);

Output:

You might also like