double val = 0.00;
for ( int i = 0; i <10; i ++)
val + = 0.10;
System. out .println (val == 1.00); * This source code was highlighted with Source Code Highlighter .
package money;
import java.util.Currency;
import java.util.Locale;
import org.junit.Assert;
')
/ **
* Created by IntelliJ IDEA.
* User: Anthony
* Date: 09/14/2008
* Time: 15:25:26
* To change this template use File | Settings | File Templates.
* /
public class Money {
private long amount;
private currency currency;
/ **
* return The numeric value of the money.
* /
public double getAmount () {
return ( double ) amount / centFactor ();
}
/ **
* return Current currency
* /
public Currency getCurrency () {
return currency;
}
public Money ( long amount, Currency currency)
{
this .currency = currency;
this .amount = amount * centFactor ();
}
public Money ( double amount, Currency currency)
{
this .currency = currency;
this .amount = Math.round (amount * centFactor ());
}
private Money ()
{
}
public static Money dollars ( double amount)
{
return new Money (amount, Currency.getInstance (Locale.US));
}
public static Money locale ( double amount)
{
Return new Money (amount, Currency.getInstance (Locale.getDefault ()));
}
public boolean equals (Object o) {
if ( this == o) return true ;
if (o == null || getClass ()! = o.getClass ()) return false ;
Money Money = (Money) o;
if (amount! = money.amount) return false ;
if (currency! = null ?! currency.equals (money.currency): money.currency! = null ) return false ;
return true ;
}
public int hashCode () {
int result;
result = ( int ) (amount ^ (amount >>> 32));
result = 31 * result + (currency! = null ? currency.hashCode (): 0);
return result;
}
/ **
* Function adds money.
* param other with what to fold.
* return The result of the addition.
* /
public Money add (Money other)
{
assertSameCurrencyAs (other);
return newMoney (amount + other.amount);
}
/ **
* Function deducts money.
* param other cleaned.
* return The result of the subtraction.
* /
public Money subtract (Money other)
{
assertSameCurrencyAs (other);
return newMoney (amount - other.amount);
}
/ **
* Function multiplies money by a factor.
* param arg cleaned.
* return The result of multiplication.
* /
public Money multiply ( double arg)
{
return new Money (getAmount () * arg, currency);
}
public int compareTo (Object other)
{
return compareTo ((Money) other);
}
/ **
* Func comparison of money.
* param other to compare with
* return
* -1 less than <br>
* 0 equals <br>
* 1 more <br>
* /
public int compareTo (Money other)
{
assertSameCurrencyAs (other);
if (amount <other.amount)
return -1;
if (amount == other.amount)
return 0;
return 1;
}
/ **
* Is there more money?
* param other to compare with.
* return True if more.
* /
public boolean greaterThan (Money other)
{
return (compareTo (other)> 0);
}
/ **
* Divide money into several parts.
* param n number of parts.
* return Array of divided money.
* /
public Money [] allocate ( int n)
{
Money lowResult = newMoney (amount / n);
Money highResult = newMoney (lowResult.amount + 1);
Money [] results = new Money [n];
int remainder = ( int ) amount% n;
for ( int i = 0; i <remainder; i ++)
results [i] = highResult;
for ( int i = remainder; i <n; i ++)
results [i] = lowResult;
return results;
}
/ **
* Splits money into unequal parts.
* param ratios proportion for separation.
* return Array of divided money.
* /
public Money [] allocate ( long [] ratios) {
long total = 0;
for ( int i = 0; i <ratios.length; i ++)
total + = ratios [i];
long remainder = amount;
Money [] results = new Money [ratios.length];
for ( int i = 0; i <results.length; i ++)
{
results [i] = newMoney (amount * ratios [i] / total);
remainder - = results [i] .amount;
}
for ( int i = 0; i <remainder; i ++) {
results [i] .amount ++;
}
return results;
}
private static final int [] cents = new int [] {1, 10, 100, 1000};
private int centFactor ()
{
return cents [currency.getDefaultFractionDigits ()];
}
private void assertSameCurrencyAs (Money arg)
{
Assert.assertEquals ( "money math mismatch" , currency, arg.currency);
}
private Money newMoney ( long amount)
{
Money Money = new Money ();
money.currency = this .currency;
money.amount = amount;
return money;
}
}
* This source code was highlighted with Source Code Highlighter .
package test.money;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.framework.TestCase;
import junit.framework.Assert;
import money.Money;
/ **
* Money Tester.
*
* author <Authors name>
* since <pre> 09/14/2008 </ pre>
* version 1.0
* /
public class MoneyTest extends TestCase {
public MoneyTest (String name) {
super (name);
}
public void setUp () throws Exception {
super.setUp ();
}
public void tearDown () throws Exception {
super.tearDown ();
}
/ **
*
* Method: add (Money other)
*
* /
public void testAdd () throws Exception {
Money m1 = Money.dollars (1.316);
Money m2 = Money.dollars (1.291);
Assert.assertEquals (m1.add (m2), Money.dollars (2.61));
}
/ **
*
* Method: subtract (Money other)
*
* /
public void testSubtract () throws Exception {
Money m1 = Money.dollars (1.316);
Money m2 = Money.dollars (1.291);
Assert.assertEquals (m1.subtract (m2), Money.dollars (0.03));
}
/ **
*
* Method: compareTo (Object other)
*
* /
public void testCompareTo () throws Exception {
Money m1 = Money.dollars (1.316);
Money m2 = Money.dollars (1.313);
Assert.assertEquals (m1.compareTo (m2), 1);
}
/ **
*
* Method: multiply (double arg)
*
* /
public void testMultiply () throws Exception {
Money m1 = Money.dollars (1.316);
Assert.assertEquals (m1.multiply (1.333), Money.dollars (1.76));
}
/ **
*
* Method: allocate (int n)
*
* /
public void testAllocateN () throws Exception {
Money m [] = Money.dollars (1.35). Allocate (3);
Assert.assertEquals (m [0] .add (m [1]) .add (m [2]), Money.dollars (1.35));
}
/ **
*
* Method: allocate (long [] ratios)
*
* /
public void testAllocateRatios () throws Exception {
long [] allocation = {3,7};
Money [] result = Money.dollars (0.05). Allocate (allocation);
assertEquals (Money.dollars (0.02), result [0]);
assertEquals (Money.dollars (0.03), result [1]);
}
public static Test suite () {
return new TestSuite (MoneyTest. class );
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/39811/
All Articles