Skip to content

Commit 18c899a

Browse files
committed
# WARNING: head commit changed in the meantime
missing method mentioned in javadoc #192 Task-Url: JavaMoney/jsr354-ri#192
1 parent 097d21f commit 18c899a

File tree

8 files changed

+527
-0
lines changed

8 files changed

+527
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
/.settings
3+
/.classpath
4+
/.project
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javamoney.examples.console</groupId>
6+
<artifactId>javamoney-examples-console</artifactId>
7+
<version>1.2-SNAPSHOT</version>
8+
</parent>
9+
<artifactId>javamoney-console-java8</artifactId>
10+
<name>JavaMoney Java 8 Console Examples</name>
11+
<properties>
12+
<jdkVersion>10</jdkVersion>
13+
</properties>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.javamoney</groupId>
17+
<artifactId>moneta</artifactId>
18+
<version>${impl.version}</version>
19+
<type>${impl.type}</type>
20+
</dependency>
21+
</dependencies>
22+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* JavaMoney Examples
3+
* Copyright 2012-2018, Werner Keil
4+
* and individual contributors by the @author tags.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.javamoney.examples.console.java10;
17+
18+
import org.javamoney.examples.console.java10.util.ConsoleUtils;
19+
import org.javamoney.moneta.FastMoney;
20+
import org.javamoney.moneta.Money;
21+
22+
import javax.money.Monetary;
23+
import javax.money.MonetaryAmountFactoryQueryBuilder;
24+
25+
26+
/**
27+
* @author Werner Keil
28+
* @version 0.9
29+
*/
30+
public class AmountsDoCalculations {
31+
32+
/**
33+
* @param args
34+
*/
35+
public static void main(String[] args) {
36+
var amount = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(234).create();
37+
ConsoleUtils.printDetails(amount);
38+
39+
amount = Monetary.getAmountFactory(FastMoney.class).setCurrency("EUR").setNumber(234).create();
40+
ConsoleUtils.printDetails(amount);
41+
42+
amount = Monetary.getAmountFactory(
43+
MonetaryAmountFactoryQueryBuilder.of().setMaxScale(50).setPrecision(30).build())
44+
.setCurrency("EUR").setNumber(234).create();
45+
ConsoleUtils.printDetails(amount);
46+
47+
var amt1 = Money.of(10.1234556123456789, "USD");
48+
var amt2 = FastMoney.of(123456789, "USD");
49+
50+
var total = amt1.add(amt2).multiply(0.5)
51+
.remainder(1);
52+
ConsoleUtils.printDetails(total);
53+
}
54+
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.javamoney.examples.console.java10;
2+
3+
import org.javamoney.moneta.Money;
4+
import org.javamoney.moneta.format.AmountFormatParams;
5+
import org.javamoney.moneta.format.CurrencyStyle;
6+
7+
import javax.money.MonetaryAmount;
8+
import javax.money.format.AmountFormatQueryBuilder;
9+
import javax.money.format.MonetaryFormats;
10+
import java.util.Locale;
11+
12+
/**
13+
* Created by Anatole on 14.05.2015.
14+
*/
15+
public class FormattingAmounts {
16+
17+
public static void main(String... args) {
18+
var amt = Money.of(1234.5678, "EUR");
19+
System.out.println(amt.query(MonetaryFormats.getAmountFormat(Locale.GERMANY)));
20+
System.out.println(MonetaryFormats.getAmountFormat(Locale.GERMANY).format(amt));
21+
amt = Money.of(123412341234.5678, "INR");
22+
System.out.println(MonetaryFormats.getAmountFormat(new Locale("", "INR")).format(amt));
23+
24+
// no with adaptive groupings
25+
System.out.println(MonetaryFormats.getAmountFormat(
26+
AmountFormatQueryBuilder.of(new Locale("", "INR"))
27+
.set(AmountFormatParams.GROUPING_SIZES, new int[]{2, 3})
28+
.set(AmountFormatParams.GROUPING_GROUPING_SEPARATORS, new char[]{',', '`'})
29+
.build())
30+
.format(amt));
31+
32+
amt = Money.of(5, "USD");
33+
System.out.println(MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.SYMBOL).set(AmountFormatParams.PATTERN, "¤##.##").build()).format(amt));
34+
}
35+
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.javamoney.examples.console.java10;
2+
3+
import java.util.Locale;
4+
5+
import javax.money.MonetaryAmount;
6+
import javax.money.format.MonetaryAmountFormat;
7+
import javax.money.format.MonetaryFormats;
8+
9+
import org.javamoney.moneta.Money;
10+
11+
/**
12+
* Created by Werner on 11.10.2019.
13+
*/
14+
public class FormattingLocaleDemo {
15+
16+
public static void main(String[] args) {
17+
final String COMPARISON = "EUR 123,456.78";
18+
19+
MonetaryAmountFormat format = MonetaryFormats.getAmountFormat(Locale.ROOT);
20+
21+
MonetaryAmount source = Money.of(123456.78, "EUR");
22+
String formatted = format.format(source);
23+
24+
System.out.println(formatted); // "EUR 123,456.78", space is a 32 on JDK 8, a 160 on JDK 9 and 10
25+
System.out.println((int) formatted.toCharArray()[3]); // JDK 8: 32, JDK 9: 160, JDK 10: 160
26+
27+
MonetaryAmount result = format.parse(COMPARISON); // Space is char of 32 (standard space)
28+
formatted = format.format(result);
29+
System.out.println(formatted);
30+
System.out.println(COMPARISON.equals(formatted));
31+
System.out.println(result.toString());
32+
}
33+
34+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* - Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
*
11+
* - Redistributions in binary form must reproduce the above copyright
12+
* notice, this list of conditions and the following disclaimer in the
13+
* documentation and/or other materials provided with the distribution.
14+
*
15+
* - Neither the name of Oracle or the names of its
16+
* contributors may be used to endorse or promote products derived
17+
* from this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*
31+
*
32+
*
33+
* JavaMoney Examples
34+
* Copyright 2014, Werner Keil
35+
* and individual contributors by the @author tags.
36+
*
37+
* Licensed under the Apache License, Version 2.0 (the "License");
38+
* you may not use this file except in compliance with the License.
39+
* You may obtain a copy of the License at
40+
* http://www.apache.org/licenses/LICENSE-2.0
41+
* Unless required by applicable law or agreed to in writing, software
42+
* distributed under the License is distributed on an "AS IS" BASIS,
43+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44+
* See the License for the specific language governing permissions and
45+
* limitations under the License.
46+
*/
47+
package org.javamoney.examples.console.java10;
48+
49+
import java.util.List;
50+
import java.util.ArrayList;
51+
import java.time.chrono.IsoChronology;
52+
import java.time.LocalDate;
53+
54+
import javax.money.MonetaryAmount;
55+
56+
import org.javamoney.moneta.Money;
57+
58+
/**
59+
*
60+
* @author Werner Keil
61+
*
62+
*/
63+
public class Person {
64+
65+
public enum Sex {
66+
MALE, FEMALE
67+
}
68+
69+
final String name;
70+
final LocalDate birthday;
71+
final Sex gender;
72+
final String emailAddress;
73+
final MonetaryAmount salary;
74+
75+
Person(String nameArg, LocalDate birthdayArg, Sex genderArg,
76+
String emailArg, MonetaryAmount salArg) {
77+
name = nameArg;
78+
birthday = birthdayArg;
79+
gender = genderArg;
80+
emailAddress = emailArg;
81+
salary = salArg;
82+
}
83+
84+
public int getAge() {
85+
return birthday.until(IsoChronology.INSTANCE.dateNow()).getYears();
86+
}
87+
88+
public MonetaryAmount getSalary() {
89+
return salary;
90+
}
91+
92+
public void printPerson() {
93+
System.out.println(name + ", " + this.getAge());
94+
}
95+
96+
public Sex getGender() {
97+
return gender;
98+
}
99+
100+
public String getName() {
101+
return name;
102+
}
103+
104+
public String getEmailAddress() {
105+
return emailAddress;
106+
}
107+
108+
LocalDate getBirthday() {
109+
return birthday;
110+
}
111+
112+
public static int compareByAge(Person a, Person b) {
113+
return a.birthday.compareTo(b.birthday);
114+
}
115+
116+
public static int compareBySalary(Person a, Person b) {
117+
return a.salary.compareTo(b.salary);
118+
}
119+
120+
public static List<Person> createRoster() {
121+
final List<Person> roster = new ArrayList<>();
122+
roster.add(new Person("Fred", IsoChronology.INSTANCE.date(1980, 6, 20),
123+
Person.Sex.MALE, "[email protected]", Money.of(100000, "USD")));
124+
roster.add(new Person("Jane", IsoChronology.INSTANCE.date(1990, 7, 15),
125+
Person.Sex.FEMALE, "[email protected]", Money.of(80000, "USD")));
126+
roster.add(new Person("George", IsoChronology.INSTANCE
127+
.date(1991, 8, 13), Person.Sex.MALE, "[email protected]",
128+
Money.of(70000, "USD")));
129+
roster.add(new Person("Bob", IsoChronology.INSTANCE.date(2000, 9, 12),
130+
Person.Sex.MALE, "[email protected]", Money.of(25000, "USD")));
131+
132+
return roster;
133+
}
134+
135+
}

0 commit comments

Comments
 (0)