I have a program to add coins into a piggy bank and calculate the total but my program is not asking how many of each coin and will not give me the total.

import java.util.Scanner;

public class PiggyBankTester
{
	public static void main(String[] args) 
	{

		int pennies;
		int nickels;
		int dimes;
		int quarters;

		Scanner in = new Scanner(System.in);
		
		
		String choice = "";
		while (choice != "X")
		{
		
		System.out.println("What type of coin to add(P, N, D, Q, or X to exit?");
	    choice = in.next();
		}
		if (choice == "P")
		{
			System.out.print("Enter the number of pennies: ");
			pennies = in.nextInt();
		}
		else if (choice == "N")
		{
			System.out.print("Enter the number of nickels: ");
			nickels = in.nextInt();
		}
		else if (choice == "D")
		{
			System.out.print("Enter the number of dimes: ");
			dimes = in.nextInt();
		}
		else if (choice == "Q")
		{
			System.out.print("Enter the number of quarters: ");
			quarters = in.nextInt();
		}
		
		PiggyBank bank = new PiggyBank();
		System.out.println("Your total change in dollars and cents is "+ bank.bankTotal());



		}
		}
public class PiggyBank
{
  private int pennies;
  private int nickels;
  private int dimes;
  private int quarters;

  public PiggyBank()
  {

  }

  public void addPennies(int numPennies)
  {
    pennies = pennies + numPennies;
  }

  public void addNickels(int numNickels)
  {
     nickels = nickels + numNickels;
  }

  public void addDimes(int numDimes)
  {
     dimes = dimes + numDimes;
  }

  public double bankTotal()
  {
    double total = 0.0;
    total = pennies * 1 + nickels * 5 + dimes * 10.0 + quarters * 25;
    return total;
  }

  }

Recommended Answers

All 3 Replies

You need to call addPennies() etc. to actually add the money to the piggybank,.

Would I do that by doing....bank.addPennies(); ? And would I need anything in the parenthesis?

you would need the number of pennies added in the brackets

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.