I know how to get output for this problem but I do not know how to create public
ID: 3550157 • Letter: I
Question
I know how to get output for this problem but I do not know how to create public int getBalance() that has to return balance in pennies?
A certain company has written a significant amount of code for a class called BankAccount that has many methods including the following:
// the only constructor for a BankAccount, takes as a parameter
// a Startup object with information about initial deposit, etc
public BankAccount(Startup s)
// record the given Debit
public void debit(Debit d)
// record the given Credit
public void credit(Credit c)
// returns the current balance in pennies
public int getBalance()
The constructor sets the initial balance based on the startup information. Assume that only the debit and credit methods change the balance. You are to design a new class called MinMaxAccount whose instances can be used in place of a BankAccount object but which include the extra behavior of remembering the minimum and maximum balances ever recorded for this account
Explanation / Answer
package com.steves.chegg.java;
public class MinMaxAccount{
public static void main(String[] args) {
BankAccount acc = new BankAccount(new Startup(100.0));
acc.debit(new Debit(5.0));
acc.credit(new Credit(10.0));
acc.debit(new Debit(5.0));
acc.credit(new Credit(50.0));
acc.debit(new Debit(20.0));
System.out.println("Min Balance : "+acc.minBal);
System.out.println("Max Balance : "+acc.maxBal);
}
}
class Startup{
double balance;
String name;
public Startup(int balance, String name) {
this.balance = balance;
this.name = name;
}
public Startup(double balance) {
this.balance = balance;
}
}
class Credit{
double amt;
public Credit(double amount) {
this.amt=amount;
}
}
class Debit{
double amt;
public Debit(double amount) {
this.amt=amount;
}
}
class BankAccount{
double balance;
double minBal;
double maxBal;
public BankAccount(Startup obj){
this.balance=obj.balance;
this.maxBal=obj.balance;
this.maxBal=obj.balance;
}
public double getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
public void credit(Credit c){
this.balance=this.balance+c.amt;
if(this.balance>this.maxBal){
this.maxBal=this.balance;
}
if(this.minBal<this.balance){
this.minBal=this.balance;
}
}
public void debit(Debit c){
this.balance=this.balance-c.amt;
if(this.balance>this.maxBal){
this.maxBal=this.balance;
}
if(this.minBal<this.balance){
this.minBal=this.balance;
}
}
}