All pastes #43317 Raw Edit

Money.java

public java v1 · immutable
#43317 ·published 2006-02-27 05:11 UTC
rendered paste body
import java.util.Scanner;public class Money {	private int dollars;	private int cents;	private int firstDollars;	private int firstCents;	private int secondDollars;	private int secondCents;	public int getCents() {		return cents;	}	public void setCents(int cents) {		this.cents = cents;	}	public int getDollars() {		return dollars;	}	public void setDollars(int dollars) {		this.dollars = dollars;	}	public Money() {		dollars = 0;		cents = 0;	}	public Money(int dollars) {		this.dollars = dollars;	}	public Money(int dollars, int cents)	{		this.dollars = dollars;		this.cents = cents;	}	public void readInput() {		Scanner keyboard = new Scanner(System.in);		System.out				.println("Enter two amounts of money.  "						+ "Put a space between the dollars and cents.  Do not use a decimal.");		firstDollars = keyboard.nextInt();		firstCents = keyboard.nextInt();		secondDollars = keyboard.nextInt();		secondCents = keyboard.nextInt();			}		public Money addMoney(Money firstAmount, Money secondAmount) {		int firstCents = firstAmount.getCents();		int secondCents = secondAmount.getCents();		int totalCents = firstCents + secondCents;		if (totalCents > 100) {			theResult.setDollars(dollars + 1);			theResult.setCents(totalCents - 100);		} else			theResult.setCents(totalCents);		int firstDollars = firstAmount.getDollars();		int secondDollars = secondAmount.getDollars();		int totalDollars = firstDollars + secondDollars;		theResult.setDollars(totalDollars);		return theResult;	}	public Money subtractMoney(Money firstAmount, Money secondAmount)	{		int firstCents = firstAmount.getCents();		int secondCents = secondAmount.getCents();		if (firstCents < secondCents)		{			theResult.setDollars(dollars-1);			theResult.setCents(cents + 100);		}			int totalCents = firstCents - secondCents;	theResult.setCents(totalCents);			int firstDollars = firstAmount.getDollars();		int secondDollars = secondAmount.getDollars();		int totalDollars = firstDollars + secondDollars;	theResult.setDollars(totalDollars);	return theResult;		}}