wrong accumulated service Charge in banking system
I just made a program for banking account
But When i put initial balance let say $500 and put a "1" code to withdraw
and finally put amount of money i want to withdraw from the balance.
But ServiceCharge has to be accumulated in the CheckingAccount class but
It seems they are not accumulated in the class.
I think i made wrong loop structure but not sure what it is.
Code:
import javax.swing.JOptionPane;
public class BankAccount {
static CheckingAccount check;
static String input;
static double initialBalance;
static double transAmount;
static double serviceCharge;
static double currentBalance;
static double toServiceCharge;
static private double below500Charge = 5.00;
static private double below0Charge = 10.00;
static private double checkCharge = 0.15;
static private double depositCharge = 0.10;
static int transCode;
public static void main(String[] args) {
initialBalance = getInitialBalance(); // get initial balance
check = new CheckingAccount(); // create CheckAccount class
check.setBalance(initialBalance); // pass initial balance to CheckAccount class
transCode = getTransCode(); // get trans code
check.setTransCode(transCode); // pass trans code to CheckingAccount class
//iteration until terminate the program
while(transCode == 1 || transCode == 2){ // iteration as long as trans code is 1 or 2
if(transCode == 1){
transAmount = getTransAmount(); // get trans amount
check.setTransAmount(transAmount); // pass trans amount to CheckingAccount class
processChecking(); // display result of transaction
transCode = getTransCode(); // get trans code again to iterate the loop
check.setTransCode(transCode);// pass trans code to CheckingAcount class
}
else
processDeposit();
}
processEnding();
}
public static double getInitialBalance() {
double balance;
input = JOptionPane.showInputDialog("Enter your initial balance: ");
balance = Double.parseDouble(input);
return balance;
}
public static int getTransCode() {
int trCode;
input = JOptionPane.showInputDialog("Enter the trans code: ");
trCode = Integer.parseInt(input);
return trCode;
}
public static double getTransAmount(){
double tAmount;
input = JOptionPane.showInputDialog("Enter the trans amount: ");
tAmount = Double.parseDouble(input);
return tAmount;
}
public static void processChecking() {
// charge $ 5 only once when balance is below $ 500
currentBalance = check.getBalance();
serviceCharge = check.getServiceCharge();
transAmount = check.getTranAmount();
if(currentBalance < 500){
serviceCharge = below500Charge;
check.setServiceCharge(serviceCharge); //pass service charge to CheckingAccount class
if(currentBalance < 0){ // if balance is less than $ 0
serviceCharge += checkCharge; // when account was withdrew, service fee charged by $ 0.15
serviceCharge += below0Charge; // when account balance is below $ 0, service fee charged by $10
check.setServiceCharge(serviceCharge); // pass to CheckingAccount class
JOptionPane.showMessageDialog(null, "Transaction : Check in Amount of $" + transAmount +
"\nCurrent Balance : $ " + currentBalance +
"\nService Charge : Check --- charge $ " + checkCharge +
"\nWarning balance below $ 50" +
"\nService Charge : Below $ 0 --- charge $ " + below0Charge +
"\nTotal ServiceCharge : $ " + serviceCharge);
}
else if(currentBalance < 50){
serviceCharge += checkCharge;
check.setServiceCharge(serviceCharge);
JOptionPane.showMessageDialog(null, "Transaction : Check in Amount of $" + transAmount +
"\nCurrent Balance : $ " + currentBalance +
"\nService Charge : Check --- charge $ " + checkCharge +
"\nWarning balance below $ 50" +
"\nTotal ServiceCharge : $ " + serviceCharge);
}
else if(currentBalance < 500){
serviceCharge += checkCharge;
check.setServiceCharge(serviceCharge);
JOptionPane.showMessageDialog(null, "Transaction : Check in Amount of $" + transAmount +
"\nCurrent Balance : $ " + currentBalance +
"\nService Charge : Check --- charge $ " + checkCharge +
"\nTotal ServiceCharge : $ " + serviceCharge);
}
}
else{
serviceCharge += checkCharge;
check.setServiceCharge(serviceCharge);
JOptionPane.showMessageDialog(null, "Transaction : Check in Amount of $" + transAmount +
"\nCurrent Balance : $ " + currentBalance +
"\nService Charge : Check --- charge $ " + checkCharge +
"\nTotal ServiceCharge : $ " + serviceCharge);
}
}
public static void processDeposit() {
}
public static void processEnding() {
}
}
Code:
public class CheckingAccount {
double balance;
double transCode;
double transAmount;
double totalServiceCharge;
double serviceCharge;
public void setBalance(double bal){
balance = bal;
}
public void setTransCode(int tCode){
transCode = tCode;
}
public void setTransAmount(double tAmount){
transAmount = tAmount;
}
public void setServiceCharge(double sCharge){
totalServiceCharge = sCharge;
}
public double getTransCode(){
return transCode;
}
public double getTranAmount(){
return transAmount;
}
public double getBalance(){
if(transCode == 1)
balance = balance - transAmount;
else if(transCode == 2)
balance = balance + transAmount;
return balance;
}
public double getServiceCharge(){
return totalServiceCharge += totalServiceCharge;
}
}
Re: wrong accumulated service Charge in banking system
Code:
public void setServiceCharge(double sCharge){
totalServiceCharge = sCharge;
}
If you want the service charge to accumulate then I imagine some sort of addition would need to be performed.