|
-
Nov 23rd, 2002, 11:00 AM
#1
Thread Starter
Addicted Member
Currency Program NEED HELP
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
Code:
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
Last edited by NOTSOSURE; Nov 24th, 2002 at 07:19 AM.
-
Nov 23rd, 2002, 08:13 PM
#2
If you had the code between [ code] and [/ code] tags it would preserve identation and therefore be much easier to read.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 24th, 2002, 06:26 AM
#3
Thread Starter
Addicted Member
Thanx for the tip BEE
Thanx BEEEEEEEEEEEEEEEEEEEEEEEEEE
-
Nov 25th, 2002, 04:24 AM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 26th, 2002, 05:10 PM
#5
Thread Starter
Addicted Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|