0% found this document useful (0 votes)
174 views2 pages

Run Savings Account

This Java code defines a SavingsAccount class with methods to deposit, withdraw, add interest, and view the balance. It also contains a main method that prompts the user to enter an interest rate and initial deposit amount, allows additional deposits or withdrawals, and calculates interest if the balance exceeds $1000.

Uploaded by

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

Run Savings Account

This Java code defines a SavingsAccount class with methods to deposit, withdraw, add interest, and view the balance. It also contains a main method that prompts the user to enter an interest rate and initial deposit amount, allows additional deposits or withdrawals, and calculates interest if the balance exceeds $1000.

Uploaded by

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

import java.util.

Scanner;
public class RunSavingsAccount{

static Scanner sc=new Scanner(System.in);

public static void main(String[]args){


SavingsAccount savings= new SavingsAccount();

System.out.print("Enter interest rate: ");


double rate=sc.nextDouble();
SavingsAccount.setInterestRate(rate);
System.out.print("Enter deposit amount: ");
double depAmount=sc.nextDouble();
savings.deposit(depAmount);
if(savings.getBalance()>=1000){
savings.addInterest();
}
SavingsAccount.showBalance(savings);
System.out.println("Press D for another deposit or W to withdraw");
char press=sc.next().charAt(0);
if(press=='D' || press=='d'){
System.out.println("Enter deposit amount: ");
double dep=sc.nextDouble();
savings.deposit(dep+savings.getBalance());
if(savings.getBalance()>=1000){
savings.addInterest();
}
SavingsAccount.showBalance(savings);
}
if(press=='W' || press=='w'){
System.out.println("Enter withdraw amount: ");
double withDr= sc.nextDouble();
savings.withdraw(depAmount);
if(savings.getBalance()>=1000){
savings.addInterest();
}
SavingsAccount.showBalance(savings);
}
}
}
class SavingsAccount {
private double balance;
public static double InterestRate=0;

public SavingsAccount(){
balance=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*InterestRate;
balance+=interest;

public static void showBalance(SavingsAccount account){


System.out.println("Your balance is"+ account.getBalance());
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy