I wrote this OOP code which is supposed to add the total at the end. The code I wrote has a current balance of .41 cents, then it will ask for total pennies, nickels, dimes and quarters. It is supposed to add those to the current balance but it is not. I appreciate any help in advance. cruzer
Here is the private code:
public class Bank
{
private double pennies;
private double nickels;
private double dimes;
private double quarters;
private double total;
public Bank()
{
pennies = .01;
nickels = .05;
dimes = .10;
quarters = .25;
}
public void setPennies(double p)
{
if (p >= 0)
{
pennies = p;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}
public double getPennies()
{
return pennies;
}
public void setNickels(double n)
{
if (n >= 0)
{
nickels = n;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}
public double getNickels()
{
return nickels;
}
public void setDimes(double d)
{
if (d >= 0)
{
dimes = d;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}
public double getDimes()
{
return dimes;
}
public void setQuarters(double q)
{
if (q >= 0)
{
quarters = q;
}
else
{
System.out.println("Please enter a numer greater than zero!");
}
}
public double getQuarters()
{
return quarters;
}
public double total()
{
double total;
total = pennies + nickels + quarters + dimes;
return total;
}
}
AND HERE IS MY MAIN CODE:
import java.util.Scanner;
import java.text.*;
public class UseBank
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
Bank e1 = new Bank();
double pennies;
double nickels;
double dimes;
double quarters;
int total;
DecimalFormat df = new DecimalFormat("#.##");
System.out.println(" Your current balance is: " + (df.format(e1.total())) );
System.out.print("How much pennies: ");
pennies = kbd.nextDouble();
e1.setPennies(pennies);
System.out.print("How much nickels: ");
nickels = kbd.nextDouble();
e1.setNickels(nickels);
System.out.print("How much dimes: ");
dimes = kbd.nextDouble();
e1.setDimes(dimes);
System.out.print("How much quarters: ");
quarters = kbd.nextDouble();
e1.setQuarters(quarters);
System.out.println("Your current balance is: " + e1.total());
}
}
Thanks again.