PDA

Click to See Complete Forum and Search --> : Currency Program NEED HELP


NOTSOSURE
Nov 23rd, 2002, 10:00 AM
i am trying 2 write a program using BlueJ Terminal Window to input data and convert from another currency to British Pounds Sterling
and charge commission according to the method below.

Method
===========================================
1. If the sterling sum arising from direct conversion is less than or equal to the minimum
commission, print the message

"Amount too small!"

2. If the calculated commission is less than the minimum commission, the payout will be
the sterling amount minus the minimum commission.

3. If the calculated commission is greater than or equal to the minimum commission, the
payout is the sterling amount minus the calculated commission.


In practice, the conversion rate and percentage commission and minimum commission
would be read in as input values rather than defined as constants.

=============================================

im having troble displaying the commission in ponds and pence




INPUT / OUTPUT
=============================================
These are the INput and outputs

Typical input :

Commission rate (percentage): 2
Currency to convert from: CNY
Rate of exchange to Sterling pence: 0.1377
Amount to exchange: 327.55

Corresponding output :

327.55 CNY goes directly to 23 pounds 78 pence
Commission is 5 pounds 0 pence
Actual sterling amount is 18 pounds 78 pence


below is the code i have done so far please tell me where i am going wrong!!!! thanx

public class CheckCurrency{

public static void main(String[] argv) {



// put your local declarations here


String currencyToConvert;
float amountToExchange, rateOfExchange, commission, amountExchanged;

int exchangePounds, exchangePence, commissionPounds, commissionPence;


// Prompt and read the commision rate

System.out.println ("Commission rate (percentage)");
commission = UserInput.readFloat();

// Prompt and read the three-letter currency code used by the industry

System.out.println ("Currency to convert from");
currencyToConvert = UserInput.readString();

// Prompt and enter the conversion rate here

System.out.println ("Rate of exchange to Sterling pence");
rateOfExchange = UserInput.readFloat();

// Prompt and read the amount of money to be converted here

System.out.println ("Amount to exchange");
amountToExchange = UserInput.readFloat();

// Compute and print

amountExchanged = amountToExchange / rateOfExchange;
commission = commission / amountToExchange * 100;


// Compute and print Exchange pounds and pence

exchangePounds = (int)(amountExchanged / 100);
exchangePence = (int) amountExchanged - exchangePounds * 100;

System.out.println (amountToExchange + " " + currencyToConvert + " goes directly to " + exchangePounds + " pounds " + exchangePence + " pence");

// Compute and print commission pounds and pence

commissionPounds = (int) (commission / 100);
commissionPence = (int) commission - commissionPounds * 100;

System.out.println ("Commission is " + commissionPounds + " pounds " + commissionPence + " pence");


// Compute and print actual pound and pence

System.out.println ("Actual sterling amount is " + exchangePounds + " pounds " + exchangePence + " pence");



} // end of main

} // end class

CornedBee
Nov 23rd, 2002, 07:13 PM
If you had the code between [ code] and [/ code] tags it would preserve identation and therefore be much easier to read.

NOTSOSURE
Nov 24th, 2002, 05:26 AM
Thanx BEEEEEEEEEEEEEEEEEEEEEEEEEE

CornedBee
Nov 25th, 2002, 03:24 AM
Ok, let's see then...

commission = commission / amountToExchange * 100;

This computation is wrong. If you want comission% of amountToExchange you should write
comission = amountToExchange * (comission/100);

exchangePence = (int) amountExchanged - exchangePounds * 100;
While this is absolutly valid it is easier to write
exchangePence = ((int)amountExchanged) % 100;
The modulo (%) operation calculates the remainder of an integer division. Perfect for splitting amounts into pence and pounds, minutes and hours etc.
Use your method if you really need more than 1 penny precision.

Your program doesn't yet have a minimum comission and doesn't print "Amount too small".

And it is very likely that readFloat throws an exception, most such IO classes do. You should catch it and re-request a number.

NOTSOSURE
Nov 26th, 2002, 04:10 PM
works great thanx mate for your help

:) :) :) :) :) :) :) :) :) :) :) :) :) :) :) :) :)