20120128

Java Project #2 : Coinjar

Got spare change in the piggie jar? Coinjar is a Java script that determines the value of coins in a jar. Tell it how many quarters, dimes, nickels, and pennies you have and it will spit out the dollar value.

/**
 * CoinJar determines the value of coins in a jar.
 * 
 * @author (Andrew Samuels) 
 * @version (02-01-2010)
 */
//import scanner class
import java.util.Scanner;

public class CoinJar
{

    public static void main(String[] args)
    {
        //create currency variables in pennies
        int quarters = 25;
        int dimes = 10 ;
        int nickels = 5;
        int pennies = 1;
        
        //create variables for user number of change
        int quartersNumber;
        int dimesNumber;
        int nickelsNumber;
        int penniesNumber;
        
        //create variables for value of change
        int quartersTotal;
        int dimesTotal;
        int nickelsTotal;
        int penniesTotal;
        
        //variables for total value as double
        int totalValue;
        double dollarCents;
        
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //input quarters from the user
        System.out.print("Please enter number of quarters: ");
        quartersNumber = keyboard.nextInt();
        
        //input dimes from the user
        System.out.print("Please enter number of dimes: ");
        dimesNumber = keyboard.nextInt();
        
        //input nickels from the user
        System.out.print("Please enter number of nickels: ");
        nickelsNumber = keyboard.nextInt();
        
         //input pennies from the user
        System.out.print("Please enter number of pennies: ");
        penniesNumber = keyboard.nextInt();
        
        //calculate quarters value
        quartersTotal = quarters*quartersNumber;
    
        //calculate dimes value
        dimesTotal = dimes*dimesNumber;
        
        //calculate nickels value
        nickelsTotal = nickels*nickelsNumber;
        
        //calculate pennies value
        penniesTotal = pennies*penniesNumber;
        
        //calculate total value in pennies
        totalValue = quartersTotal + dimesTotal + nickelsTotal + penniesTotal;
        
        //calculate total value as double in dollarCents ex. 2.19
        dollarCents = totalValue*(.01);
        
        //convert dollarCents to String for later use.
        String dollarCentsStr;
        dollarCentsStr = "" + dollarCents;
        
        //convert to dollarCents into integer
        int dollarCentsInt = (int)dollarCents;
        
        //separate cents using substring
        String number = dollarCentsStr;
        String result;
        result = number.substring(2);
       
        //print total dollars and cents
        System.out.println("Jar has " + dollarCentsInt + " dollars and " + result + " cents");
        
        
    }
}