Code ‘n …coffee?

30 Nov

Chai latte, actually, ’cause I just can’t take coffee these days.  Makes my heart race and my hair stand on end. At my age, that’s risky business better saved for the sudden joyous preponderance of short skirts in Springtime.

Just wanted to post an exercise I did today from ‘Big Java’ (first edition, purchased at Boomerang’s for $1 w00t!).  The code looks much longer than it is with the javadoc comments in it, but I am gabberflasted at what javadoc can do.  One quick command and I’ve got a very slick web site explaining everything there is to know about my wonderful code.

Here ’tis:

import java.text.NumberFormat;
public class Purse
{
    /**
	 * Constructs an empty purse.
	 */
	public Purse()
	{
	    // empty purse
		this.nickels = 0;
		this.dimes = 0;
		this.quarters = 0;
		this.total = 0.0;
	}

    /**
	 * Constructs a non- empty purse.
	 */
	public Purse(int nickels, int dimes, int quarters)
	{
	    // non-empty purse
		this.nickels = nickels;
		this.dimes = dimes;
		this.quarters = quarters;
	}

	/**
	 * Add nickels to the purse.
	 * @param count the number of nickels to add
	 */
	public void addNickels(int count)
	{
	   this.nickels += count; 
	}

	/**
	 * Add dimes to the purse.
	 * @param count the number of dimes to add
	 */
	public void addDimes(int count)
	{
	   this.dimes += count; 
	}

	/**
	 * Add quarters to the purse.
	 * @param count the number of dimes to add
	 */
	public void addQuarters(int count)
	{
	   this.quarters += count; 
	}

	/**
	 * Get the total value of the coins in the purse.
	 * @return the sum of all coin values
	 */
	public double getTotal()
	{
	   total = nickels * .05 +
	           dimes * .10 +
			   quarters * .25;

	   return total; 
	}

	public String getDollarTotal()
	{
	   total = nickels * .05 +
	           dimes * .10 +
			   quarters * .25;

	   dollarTotal = currency.format(total);
	   return dollarTotal; 
	}

    private int nickels;
	private int dimes;
	private int quarters;
	private double total;
	private String dollarTotal;
    NumberFormat currency =
	    NumberFormat.getCurrencyInstance();
}

The NumberFormat business is something I picked up from Beginning Programming With Java For Dummies. Don’t let the title fool you, it also works great for brilliant minds like ours. NumberFormat lets you take a double value and turn into I nicely formatted dollar amount String. Snazzy!

The exercise in the book provided the method names, but not the implementation. Nothing difficult, but always fun to figure it out, see it work and try to make it look nice. I had to write another class with a main method to test it out. That’s here:

public class TestPurse
{
    public static void main(String[] args)
	{
		// create new empty purse
	    Purse myPurse = new Purse();
		Purse hisPurse = new Purse(20, 10, 4);

		myPurse.addNickels(29);
		hisPurse.addNickels(27);
		myPurse.addDimes(13);
		hisPurse.addDimes(15);
		myPurse.addQuarters(2);
		hisPurse.addQuarters(34);

		// output totals
		System.out.println("My purse: " + myPurse.getDollarTotal());
		System.out.println("His purse: " + hisPurse.getDollarTotal());
    }
}
Advertisements

2 Responses to “Code ‘n …coffee?”

  1. Scott December 18, 2012 at 4:35 pm #

    Created two files from your sources: Purse.java and TestPurse.java, then compiled and ran it.

    Q:> javac TestPurse.java
    Q:> java TestPurse

    My purse: $3.25
    His purse: $14.35

    Confirmed ;)

  2. wordpresskwood December 18, 2012 at 4:45 pm #

    I think this code is ready for Production. :)

Comments are welcome!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: