How would I modify this program or make a program that works in conjunction with
ID: 3680778 • Letter: H
Question
How would I modify this program or make a program that works in conjunction with this one, which I am not sure I understand how, to allow user input for a starting balance in Java?
/**
* Chapter 5
* Programming Challenge 10: Deposit and Withdrawl Files
* The SavingsAccount class stores data for a savings account.
*/
public class SavingsAccount
{
private double balance; // The account balance
private double interestRate; // The annual interest rate
private double lastInterest; // Last amount of interest earned
/**
* The constructor initializes an object with a
* balance and an annual interest rate.
*/
public SavingsAccount(double bal, double intRate)
{
balance = bal;
interestRate = intRate;
lastInterest = 0.0;
}
/**
* The withdraw method withdraws an amount from
* the account.
*/
public void withdraw(double amount)
{
balance -= amount;
}
/**
* The deposit method deposits an amount into
* the account.
*/
public void deposit(double amount)
{
balance += amount;
}
/**
* The addInterest method calculates the monthly
* interest and adds it to the account balance.
*/
public void addInterest()
{
// Get the monthly interest rate.
double monthlyInterestRate = interestRate / 12;
// Calculate the last amount of interest earned.
lastInterest = monthlyInterestRate * balance;
// Add the interest to the balance.
balance += lastInterest;
}
/**
* The getBalance method returns the account balance.
*/
public double getBalance()
{
return balance;
}
/**
* The getInterestRate method returns the annual
* interest rate.
*/
public double getInterestRate()
{
return interestRate;
}
/**
* The getLastInterest method returns the last amount
* of interest earned.
*/
public double getLastInterest()
{
return lastInterest;
}
}
Explanation / Answer
package bankmanagment;
import java.util.*;
/**
*
* @author Shohan
* Bank Account Class
* // Main ClassName -- "Main"
*/
class Account{
String name,acc_type;
int Acc_num,Acc_Balance;
Account(){
}
Account(String n,int acc_num,int b,String a_t){
name=n;
Acc_num=acc_num;
Acc_Balance=b;
acc_type=a_t;
}
} // end class
class create_account extends Account{
create_account(String n,int acc_num,int b,String a_t){ // pass name and account type
name=n;
Acc_num=acc_num;
Acc_Balance=b;
acc_type=a_t;
}
create_account(){
super();
}
void insert(String n,int acc_num,String a_t){ // input user name, account number and type
name=n;
acc_type=a_t;
Acc_num=acc_num; // generate random number
Acc_Balance=0;
}
void display_details(){
System.out.println("Depositor Name :" +name);
System.out.println("Account Number : "+Acc_num);
System.out.println("Account Balance : "+Acc_Balance);
System.out.println("Account Type : "+acc_type);
}
void deposite(int acc_num,int money){
Acc_Balance=money;
}
int withdraw(int withd){
Acc_Balance=Acc_Balance-withd;
return Acc_Balance;
}
} // end class
public class Main {
public static void main(String args[]){
String user_name=null,type;
type = null;
int balance=0,tmp=0;
int withd=0,cb=0;
// to generate Random Account Number
int aNumber = 0;
aNumber = (int)((Math.random() * 9000)+1000);
create_account user = new create_account("user",0,0,"savings"); // initilaize -- name,acc_number,Balance,Type
Scanner in = new Scanner(System.in);
Scanner strng=new Scanner(System.in);
int userChoice;
boolean quit = false;
do {
System.out.println("1. Create Account");
System.out.println("2. Deposit money");
System.out.println("3. Withdraw money");
System.out.println("4. Check balance");
System.out.println("5. Display Account Details");
System.out.println("5. Display interest rate for 10 years");
System.out.println("0. to quit: ");
System.out.print("Enter Your Choice : ");
userChoice = in.nextInt();
switch (userChoice) {
case 1:
System.out.print("Enter your Name : ");
user_name=strng.nextLine();
System.out.print("Enter Accout Type : ");
type=in.next();
user.insert(user_name, aNumber, type); // inserted
System.out.println(" Your Account Details Dont Forget Account Number ");
System.out.println("**************************");
user.display_details();
break;
case 2: // deposite
System.out.print("Enter your account Number : ");
tmp=in.nextInt();
if(tmp==user.Acc_num){
System.out.print("Enter Amount Of Money : ");
balance=in.nextInt();
user.Acc_Balance=balance;
System.out.println(" Successfully Deposited.");
}
else
System.out.println("Wrong Accoount Number.");
break;
case 3: // withdraw money
System.out.print("Enter your account Number : ");
tmp=in.nextInt();
if(tmp==user.Acc_num){
if(user.Acc_Balance==0)
System.out.print("Your Account is Empty.");
else{
System.out.print("Enter Amout Of Money : ");
withd=in.nextInt();
if(withd>user.Acc_Balance){
System.out.print("Enter Valid Amout of Money : ");
withd=in.nextInt();
}
else
cb= user.withdraw(withd);
System.out.println("Your Current Balance : "+cb);
}
}
else
System.out.println("Wrong Accoount Number.");
break;
case 4: // check balance
System.out.print("Enter your Account Number : ");
tmp=in.nextInt();
if(tmp==user.Acc_num){
System.out.println("Your Current Balance : "+user.Acc_Balance);
}
else
System.out.println("Wrong Accoount Number.");
break;
case 5: // display all info
System.out.print("Enter your Account Number :");
tmp=in.nextInt();
if(tmp==user.Acc_num){
user.display_details();
}else
System.out.println("Wrong Accoount Number.");
break;
case 6: // calculating simple interest for 10 years
int rate = 7;
float interest = (balance*rate*time)/100;
}
Read more: http://java67.blogspot.com/2012/10/java-program-to-calculate-simple-interest.html#ixzz43haFMvtg
case 0:
quit = true;
break;
default:
System.out.println("Wrong Choice.");
break;
}
System.out.println(" ");
} while (!quit);
System.out.println("Thanks !");
} // end main function
} // end mian class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.