4
\$\begingroup\$

The code follows the requested format of the output exactly for my assignment. But, for my own personal reference, is there a better way that I could have done anything? Are there any additional tips? Maybe some simple ways that I could gold plate against exceptions or errors? I know how try, catch and throw exceptions work. My instructor doesn't check for it, though, so I wasn't going to spend time on it.

class Money {

public static void main(String[] args){

    //Variable and Constant Declaration
    int price,amountpaid,dollars,quarters,dimes,nickels,change,remainder = 0;

    Scanner sc = new Scanner(System.in);

    DecimalFormat df = new DecimalFormat("#0.00");

        //Input
        System.out.println("Please enter the purchase price in cents: ");
        price = sc.nextInt();

        System.out.println("Please enter the amount tendered in cents: ");
        amountpaid = sc.nextInt();

        //Calculations
        remainder = amountpaid - price;

        change = remainder;

        dollars = remainder / 100;    
        remainder = remainder % 100;  

        quarters = remainder / 25;  
        remainder = remainder % 25;  

        dimes = remainder / 10;  
        remainder = remainder % 10;

        nickels = remainder / 5;  
        remainder = remainder % 5;  

        pennies = remainder;  

        //Output
        System.out.println("Purchase Price:\t $" + df.format((double)price/100) + 
                                "\nAmount Tendered: $" + df.format((double)amountpaid/100) +
                                "\n \nYour Change is:  $" + df.format((double)change/100) + 
                                "\n \n \t \t " + dollars + " one-dollar bills(s)\n \t \t " 
                                + quarters + " quarter(s)\n \t \t " + dimes + 
                                " dime(s)\n \t \t " + nickels + " nickle(s)\n \t \t " + 
                                remainder + " penn(y/ies)");
        System.out.println("\nThank you for your business. Come back soon.");

} // End Main
} //End Class
\$\endgroup\$
7
\$\begingroup\$

There is a general purpose guideline - the Single Responsibility Principle. For a given procedure/function you should be able to write a simple statement "This code does X." If you find yourself writing "This code does X and Y." then you should consider breaking the code up into pieces - one piece that does X and one piece that does Y.

The code above does three things - reads input, calculates change, writes output. Those three pieces are candidates for creating separate functions.

I say candidates because there is always a trade-off. When you extract some code into a new function/method then you also have to be able to communicate input to the new function/method as well as for it to communicate back its results. Your code is fairly simple and straightforward as is. Even though it has three different parts and is longer than would be preferred for a single function, splitting it up wouldn't improve it that much while at the same time adding extra complication/overhead in the method calls and returns.

However, that said, if this code became the basis for a more advanced assignment, one thing that you could do would be to create a Change or Payment class something like I have shown below. It encapsulates two parts of the functionality in your code in a simple and direct way that provides a basis for adding additional features.

For example, if you needed to calculate the amount tendered from counts of coins and bills, you could add a function that reads a count of coins tendered and another that calculates the total value.

class Payment {
    int dollars = 0;
    int quarters = 0;
    int dimes = 0;
    int nickels = 0;
    int pennies = 0;

    public payment(int value) {
        int remainder = value;

        dollars = remainder / 100;    
        remainder = remainder % 100;  

        quarters = remainder / 25;  
        remainder = remainder % 25;  

        dimes = remainder / 10;  
        remainder = remainder % 10;

        nickels = remainder / 5;  
        remainder = remainder % 5;  

        pennies = remainder;  
    }

    public writeOutput(PrintStream out) {
        out.println("\t \t " + dollars + " one-dollar bills(s)\n \t \t " 
                             + quarters + " quarter(s)\n \t \t " + dimes + 
                             " dime(s)\n \t \t " + nickels + " nickle(s)\n \t \t " + 
                             pennies + " penn(y/ies)");

    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.