0

There are two things I need assistance with. One is a problem with the rounding in the output and the other is just to find a better way to write my program that outputs the same results, if necessary.

What is the most efficient way to write this program? Even though it works like it should, I know it is not designed the best.

package program2;

import java.util.*;

class PiggyBank
{
    Scanner console = new Scanner( System.in );

    private int numPennies, numNickles, numDimes, numQuarters;
    private float total;

    public PiggyBank( int pennies, int nickles, int dimes, int quarters )
    {
        numPennies = pennies;
        numNickles = nickles;
        numDimes = dimes;
        numQuarters = quarters;
        total = (float) 0.00;
    }

    public void addPennies( int pennies )
    {
        System.out.println( "Have entered " + pennies + " pennies" );

        if ( pennies < 0 )
        {
            System.out.println( "No Pennies Added" );
        }
        else
        {
            numPennies = numPennies + pennies;
            total = (float) ( total + pennies * 0.01 );
        }
    }

    public void addNickles( int nickles )
    {
        System.out.println( "Have entered " + nickles + " nickles" );

        if ( nickles < 0 )
        {
            System.out.println( "No Nickles Added" );
        }
        else
        {
            numNickles = numNickles + nickles;
            total = (float) ( total + nickles * 0.05 );
        }
        System.out.println( "Bank has $" + total + " in it" );
        System.out.println();
    }

    public void addDimes( int dimes )
    {
        System.out.println( "Have entered " + dimes + " dimes" );

        if ( dimes < 0 )
        {
            System.out.println( "No Dimes Added" );
        }
        else
        {
            numDimes = numDimes + dimes;
            total = (float) ( total + dimes * 0.10 );
        }
        System.out.println( "Bank has $" + total + " in it" );
        System.out.println();
    }

    public void addQuarters( int quarters )
    {
        System.out.println( "Have entered " + quarters + " quarters" );

        if ( quarters < 0 )
        {
            System.out.println( "No Quarters Added" );
        }
        else
        {
            numQuarters = numQuarters + quarters;
            total = (float) ( total + quarters * 0.25 );
        }
    }

    public float getContents()
    {
        return total;
    }

    public final int breakTheBank()
    {
        if ( total >= 0 )
        {
            total = 0;
        }

        return (int) total;
    }

}

public class PiggyBankTester
{

    public static void main( String[] args )
    {
        Scanner console = new Scanner( System.in );

        System.out.print( "Program By " );
        String name = console.next();
        System.out.println();

        test();
    }

    public static void test()
    {
        PiggyBank bank = new PiggyBank( 0, 0, 0, 0 );

        bank.addNickles( 3 );

        bank.addPennies( 4 );
        System.out.println( "Bank has $" + bank.getContents() + " in it \n" );

        bank.addPennies( -18 );
        System.out.println( "Bank has $" + bank.getContents() + " in it \n" );

        bank.addDimes( 2 );
        bank.addQuarters( 3 );
        System.out.println( "Bank has $" + bank.getContents() + " in it \n" );

        bank.addQuarters( -3 );
        System.out.println( "Bank has $" + bank.getContents() + " in it \n" );

        System.out.println( "Broke the bank and got $" + bank.getContents() + " from it \nBank has $" + bank.breakTheBank() + " in it" );
    }
}

Here is a sample of my output. The float total rounded some of the results but I am not sure how to get it to round all of the results.

Program By JakeBrono46

Have entered 3 nickles
Bank has $0.15 in it

Have entered 4 pennies
Bank has $0.19000001 in it 

Have entered -18 pennies
No Pennies Added
Bank has $0.19000001 in it 

Have entered 2 dimes
Bank has $0.39000002 in it

Have entered 3 quarters
Bank has $1.14 in it 

Have entered -3 quarters
No Quarters Added
Bank has $1.14 in it 

Broke the bank and got $1.14 from it 
Bank has $0 in it

I did use another site to find the structures for the accessors and mutators. I do not think I am missing anything too major, but I just can't think of what else I need to do at the moment.

mateuscb
  • 10,150
  • 3
  • 52
  • 76
  • Cleaned up code formatting. It was mostly there, just missed a couple "}". Minor spelling. Added punctuation. Removed line about question being understandable, not relevant. – mateuscb May 09 '17 at 18:30

3 Answers3

0
public final int breakTheBank()`
{
if(total >= 0)
{
    total = 0;
}

return (int) total;
}

I would just change the if statement to if(total == 0) because you don't need to check if it's 0 if your going to change it to 0 anyway. Other than that there are not many changes that you can make.

0

One is a problem with the rounding in the output

Here is a method that I tend to use when rounding decimal numbers, so when you output a total just call it. More information can be found here about rounding numbers: Round a double to 2 decimal places

public static double round(double value, int places) {
  if (places < 0) throw new IllegalArgumentException();

    BigDecimal bd = new BigDecimal(value);
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

find a better way to write my program that outputs the same results

If I was tasked to do this I would have used enumerations, where you can just create a enum value for every coin. If you're unsure on how to use enumerations take a look here for more information, https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

What you'd want to do in your case is to create a enum called Coins, where is takes a float of the value of that coin. Then create another method for instance addCoin where that would take a enum of coin and the amount to be added. Then simply calculate the result by accessing the value of the coin in the enum and multiplying it by the amount that's added.

Community
  • 1
  • 1
Jordan
  • 157
  • 5
  • 17
0

find a better way to write my program that outputs the same results

import java.util.*;
import java.text;

class PiggyBank
{
 Scanner console = new Scanner(System.in);
 NumberFormat formatter = NumberFormat.getCurrencyInstance();

 private int numPennies, numNickles, numDimes, numQuarters;
 private float total;

 public PiggyBank(int pennies, int nickles, int dimes, int quarters)
{
   numPennies = pennies;
   numNickles = nickles;
   numDimes = dimes;
   numQuarters = quarters;     
   total = 0.00;
}

I would use the NumberFormat class in the Text package. This way you can format numbers to your default currency (In this case dollars). You also do not need to cast float to your total variable since you declared it as a float instance variable.

bank.addPennies(4);     
System.out.println("Bank has" + formatter.format(bank.getContents()) + " in it    \n");

You use the formatter like so to print the formatted $#.## which allows you to avoid explicitly adding a dollar sign.

Jeremy
  • 18
  • 3