Currency

Java Currency Example – Working with money

In this example we will discuss about Currency class and its usage. This class represents a currency based on the ISO 4217 currency codes. The class is designed so that there’s never more than one Currency instance for any given currency. Therefore, there’s no public constructor. You obtain a Currency instance using the getInstance() methods.

Currency extends the Object class, the base class of all classes in Java.

The Currency class exists since JDK1.4.
 
 

The Currency in Java

To see a basic usage of Currency class, create a class called BasicCurrencyExample with the following source code:

BasicCurrencyExample.java

package com.javacodegeeks.examples;

import java.util.Currency;
import java.util.Locale;

public class BasicCurrencyExample {
	public static void main(String[] args) {
		Currency curr = Currency.getInstance(Locale.UK);
		
		System.out.println("The currency of United Kingdom is " + curr.getCurrencyCode());
	}
}

On line 8, I create a new instance of Currency class. The class has no constructors, the new instance is created by using the getInstance() method, just like in every other locale-sensitive class. There are two implementations of this method, the getInstance(Locale locale) and the getInstance(String currencyCode). Both of them have the same function

Then, by using the getCurrencyCode() method, I display the currency code for the currency in United Kingdom.

As you might have thought, the output is this:

The currency of United Kingdom is GBP

A better usage of Currency in Java

I created a class called Account that should show a more practical usage of the Currency class. So, create a new class with that name and put this code into it:

Account.java

package com.javacodegeeks.examples.account;

import java.util.Currency;
import java.util.Locale;

public class Account {
	private Locale locale;
	private int amount;
	private Currency c;
	
	public Account(Locale locale, int amount) {
		this.locale = locale;
		this.amount = amount;
		this.c = Currency.getInstance(locale);
	}
	
	public int getAmount() {
		return amount;
	}
	
	public Locale getLocale() {
		return this.locale;
	}
	
	public void status() {
		System.out.println(amount + " " + c.getCurrencyCode()+" in the account");
	}
	
	public void deposit(int deposit) {
		amount += deposit;
		System.out.println(deposit + " " + c.getCurrencyCode()+" deposited");
	}
	
	public void withdraw(int w) {
		if (w<amount) {
			amount -= w;
			System.out.println(w + " " + c.getCurrencyCode()+" withdrawed");
		} else {
			System.out.println("You can't withdraw  "+w+" "+ c.getCurrencyCode()+"!");
		}
		
	}
	
}

In this class I just store the amount of money in someone’s account, and the locale. This locale is used then to get the currency of that country. I also wrote some methods for printing the amount, depositing and withdrawing from that amount.

To test this class, create a class called Main with this code into it:

Main.java

package com.javacodegeeks.examples.account;

import java.util.Locale;

public class Main {

	public static void main(String[] args) {
		Account ac = new Account (Locale.UK,15000);
		
		ac.status();
		ac.withdraw(1000);
		ac.status();
		ac.deposit(5000);
		ac.status();
		ac.withdraw(100000);
	}

}

In this class I just created an instance of the Account class and call its methods for printing the amount before and after testing withdraws and deposits. The output would be like this:

15000 GBP in the account
1000 GBP withdrawed
14000 GBP in the account
5000 GBP deposited
19000 GBP in the account
You can't withdraw  100000 GBP!

More about the Currency in Java

Currency class represents a currency. Currencies are identified by their ISO 4217 currency codes.

The class is designed so that there’s never more than one Currency instance for any given currency. Therefore, there’s no public constructor. You obtain a Currency instance using the getInstance() methods.

Users can supersede the Java runtime currency data by creating a properties file named /lib/currency.properties. The contents of the properties file are key/value pairs of the ISO 3166 country codes and the ISO 4217 currency data respectively. The value part consists of three ISO 4217 values of a currency, i.e., an alphabetic code, a numeric code, and a minor unit. Those three ISO 4217 values are separated by commas. The lines which start with ‘#’s are considered comment lines. For example,

#Sample currency properties
JP=JPZ,999,0

will supersede the currency data for Japan.

Download Code

Download
You can download the full source code of this example here : CurrencyExample

Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button