Hi All,

I am currently trying to figure out to finish this project. Below is my code that I have so far. There are some blank methods that I am not sure where to even begin on how to complete them. I have a description of what the methods should do above them, so please help guide me on the blank methods so I can figure them out. Part of what is confusing to me is how to get the data that is read from the file and use it in the conversions since some of the data is inputted through dialog boxes. Thanks for any help in advance.

import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;

import javax.swing.JOptionPane;
public class CurrencyExchange {

	final static double MIN_COMISSION = 5;
	static double commissionPerCent;
	static double buyingRateC;
	static double sellingRateC;
	static double buyingRateM;
	static double sellingRateM;
	static double buyingRateE;
	static double sellingRateE;
	static double amount;
	static char code;
	static boolean buy;
	static double rate;
	DecimalFormat format = new DecimalFormat();
	
	/*displays the welcoming message;
	*calls readRates( ); 
	*runs a while loop as long as newTransaction( ) returns YES; 
	*the loop calls the methods input Currency( ), inputAmount( ), 
	*buyOrSell( ), displayResult( );
	*sends the closing message "Exchange is closed for the day." 
	*to the console and terminates the process.
	*/
	public static void main(String[] args) throws IOException {
		JOptionPane.showMessageDialog(null,"Welcome to the Modest International Currency Exchange Services!", "Modest International",1);
		readRates();
		
		while(newTransaction()== true){
			inputCurrency();
			inputAmount();
			buyOrSell();
			displayResult();
		}
	}
	
	
	/*return type double; 
	*parameters: double rate, double amount, boolean buy;
	*computes and returns the dollar cost of a transaction of the 
	*given exchange rate 'rate', currency amount 'amount' and buy value true 
	*for buying and false for selling, respectively; 
	*/
	
	public static Double priceInDollars(double rate, double amount, boolean buy){
		
		return;
	}
	
	
	/*return type String; no parameters;
	*runs a switch controlled by the 'code' data field as its selector; 
 	*for each case of the code and the accompanying buy value the switch determines and 
 	*assigns the current rate of the transaction; 
 	*the method composes and returns the output report to be 
	*written to the output file; calls the priceInDollars( ) method 
	*to obtain the dollar value which is to be included in the report;
	*/
	public static String composeReport(){
		
		return;
	}
	
	
	/*void; no parameter; calls the composeReport( ) method and 
	 * writes the return value to the output file; calls the 
	 * priceInDollars( ) method and displays the output message
	 */
	public static void displayResult(){
		
	}
	
	
	/*void; parameter: String line; the parameter must be formatted 
	 * as a line shown in the input file; instantiates a Scanner object to tokenize the 
	 * parameter string; uses a nested if-else sequence to assign the second 
	 * token to the correct rate variable or to the commission
	 */
	public static void assignRate(String line){
		
	}
	
	
	/*void; no parameter; 
	*throws IOException; instantiates a Scanner object and runs a 
	*while loop to read the lines of the input file; calls the 
	*assignRate() method for every input line, passing the line to 
	*the method as the parameter
	*/
	public static void readRates() throws IOException{
	File f = new File("dailyrates.txt");	
	Scanner y = new Scanner(f);
	String com;
	String buyC;
	String sellC;
	String buyM;
	String sellM;
	String buyE;
	String sellE;
	while(y.hasNext()){
		com = y.next();
		commissionPerCent= y.nextDouble();
		buyC = y.next();
		buyingRateC= y.nextDouble();
		sellC = y.next();
		sellingRateC=y.nextDouble();
		buyM = y.next();
		buyingRateM= y.nextDouble();
		sellM = y.next();
		sellingRateM=y.nextDouble();
		buyE = y.next();
		buyingRateE= y.nextDouble();
		sellE = y.next();
		sellingRateE=y.nextDouble();
	}
	}
	
	/*void; no parameter; 
	*solicits and assigns the code value; if no input supplied, 
	*sends the message "N0 currency input. Transaction aborted." to the console
 	*and assigns character '?' to code upon which the main method opens the window on Figure 9 
 	*and the process starts all over.
 	*/
	public static void inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
	}
	
	/*void; no parameter; 
	*solicits and assigns the amount value; if no input supplied,
	*sends the message "N0 amount input. Transaction aborted." to the console and assignes -1 
	*to amount upon which the main method opens the window 
	*/
	public static void inputAmount(){
		String inputAmount = JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
	}
	
	/*void; no parameter; solicits and assigns the buy value; 
	 * note that no input validation is necessary if a Confirm Dialog is used
	 */
	public static void buyOrSell(){
		String buyOrSell = JOptionPane.showInputDialog(null, "Please \n type \"b\" or \"buy\" if you want to buy and" +
				" \n type \"s\" or \"sell\" if you want to sell the amount.", JOptionPane.QUESTION_MESSAGE);
	}
	
	/*return type boolean; no parameter; opens a Confirm Dialog and
	*returns true for JOptionPane.YES_OPTION, false otherwise
	*/
	public static boolean newTransaction(){
		int result = JOptionPane.showConfirmDialog(null,"Would you like to make a transaction?", "Modest International",     
		           JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(result == JOptionPane.YES_OPTION){
		return true;
		}else{
			System.out.print("Modest International is closed for the day. See you tomorrow!");
			return false;
		}
		}

}

Thanks again!

Recommended Answers

All 126 Replies

how to get the data that is read from the file and use it in the conversions since some of the data is inputted through dialog boxes

One way would be to save the data in class variables where all the methods can see it.

So for example, what you are saying is:

public static void inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
	}

public static void inputAmount(){
		String inputAmount = JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
	}

For these, declare the string choice and inputAmount at the class level?

So like this?

public static char inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
		if(choice.equalsIgnoreCase("CD"))
		{
			code = 'c';
			if(choice.equalsIgnoreCase("MP"))
			{
				code= 'm';
				if(choice.equalsIgnoreCase("EU"))
				{
					code = 'e';
				}
			}
		}
		return code;
	}

Your testing of the value of choice should be in the if else if format.
The inner test will always fail because to get inside the if, the value had to be different from the value you are testing inside the if.


if(x == 1) {
.. do things for when x is 1
}else if(x == 2) {
.. do things for when x is 2
}else if ....

declare the string choice and inputAmount at the class level?

The second solution is better. The method looks at what the user input and returns a code describing it.


What is the inputCurrency() method supposed to do? The first version read two values from the user. The second version only reads one.
The method's name should describe what is doing. Its name should include a verb describing its actions.
The second version looks like it is getting a currency type: getCurrencyType()

Thanks very much NormR1! I think I've properly fixed them, but no way of telling until I complete the other methods test them(working on it now) but could you take a look over these real quick and see if they look correct:

/*return type char; no parameter; 
	 * opens a JOptionPane input window to obtain the currency code from the user; 
	 * if the input string is null or empty, the program offers a new transaction; 
	 * otherwise returns the first character of the input string.
 	*/
	public static char inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
		if(choice.equalsIgnoreCase("CD"))
		{
			code = 'c';
		}else if(choice.equalsIgnoreCase("MP"))
			{
				code= 'm';
			}else if(choice.equalsIgnoreCase("EU"))
				{
					code = 'e';
				}
		
		return code;
	}
	
	/*return type double; no parameter;  
	*opens a JOptionPane input window to obtain the currency amount
	*from the user; if the input string is null or empty, the program offers a
	*new transaction; otherwise returns the converted numerical value of 
	*the input numeric string (use parseDouble)
	*/
	public static double inputAmount(){
		String inputAmount = JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest   International",  JOptionPane.QUESTION_MESSAGE);
		if(inputAmount == " "){
			JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest International", 
	                JOptionPane.QUESTION_MESSAGE);
		} else {
			amount = Double.parseDouble(inputAmount);
		}
		return amount;
		
	}
	
	/*no parameter; the method must not be void, but you choose the return
	 * type as you see it best; 
	 * opens a JOptionPane input window to obtain the buy/sell option from
	 * the user; if the input string is null or empty, the program offers a new
	 * transaction; otherwise returns a value depending on the input string.
	 */
	public static boolean buyOrSell(){
		String buyOrSell = JOptionPane.showInputDialog(null, "Please \n type \"b\" or \"buy\" if you    want to buy and" + " \n type \"s\" or  \"sell\" if you want to sell the amount.",  JOptionPane.QUESTION_MESSAGE);
		if(buyOrSell == "b"){
			buy = true;
		}else if(buyOrSell == "s"){
			buy = false;	
		}
		return buy;
	}

Thanks, sorry about the formatting it is getting goofy when I paste it in here and not letting me move it.

What is the inputCurrency() method supposed to do? The first version read two values from the user. The second version only reads one.
The method's name should describe what is doing. Its name should include a verb describing its actions.
The second version looks like it is getting a currency type: getCurrencyType()

The inputCurrency() method ask the user what currency they would like to exchange by entering "CD" "MP" or "EU" into the input dialog box.

I think I've properly fixed them, but no way of telling until I complete the other methods test them

This is a very common mistake. In fact you can, and should, test methods as early and as often as possible. Waiting until all the code is "finished" makes it really hard to tell where things are going wrong - it could be anywhere. If you test methods as you go then any problems are most likely to be in the most recent method. Experienced programmers are constantly testing methods and small modules before putting them into the main program.
eg for your inputCurrency() method all you need to test it is:

public static void main(String[] args) {
   System.out.println(inputCurrency));
}

Just run that, enter some currency names (and some wrong ones), and see if it returns the right code values. Once you've debugged this method you can stop thinking about it and get on with the next one.

Thanks JamesCherrill!

Your methods ignore invalid input. You'd detect that at the end of the chain of if/else if in the final else clause. Should the method ask the user again? Should the the program allow the user to exit the program? It's something to think about.

Ok trying to fix invalid input and here is what I have been trying:

public static char inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
		if(choice == null||choice.equals("")){
			code = '?';
			if(code == '?'){
			System.out.println("No currency input. Transaction aborted");
			}
		}
		else if(choice.equalsIgnoreCase("CD"))
		{
			code = 'c';
		}else if(choice.equalsIgnoreCase("MP"))
			{
				code= 'm';
			}else if(choice.equalsIgnoreCase("EU"))
				{
					code = 'e';
				}
			
		return code;
	}

When I tested the method with invalid input, it does not print anything to the console though, just blank, but if I test it with valid input it still prints the correct code.

I don't see any code in your method that tests for an invalid data entered. What would the code do if the user entered: XX
You need to add an else clause at the end of the if/else if chain that would be executed if none of the above if statements are true.

I don't see any code in your method that tests for an invalid data entered. What would the code do if the user entered: XX
You need to add an else clause at the end of the if/else if chain that would be executed if none of the above if statements are true.

I thought this was testing to see if there was invalid input:

if(choice == null||choice.equals("")){
                  code = '?';
		  if(code == '?'){
			System.out.println("No currency input. Transaction aborted");
			}
		}

That's part of it.
What if the user enters: XX

Ok I changed it, now it displays error message to the console for all else:

if(choice.equalsIgnoreCase("CD"))
		{
			code = 'c';
		}else if(choice.equalsIgnoreCase("MP"))
			{
				code= 'm';
			}else if(choice.equalsIgnoreCase("EU"))
				{
					code = 'e';
				}else{
					System.out.println("No currency input. Transaction aborted");
				}
			
		return code;
	}

What about the value of code?

Your error message is misleading. Also you should show what the invalid value is in the message.

What about the value of code?

Your error message is misleading. Also you should show what the invalid value is in the message.

Ok I changed it to:

System.out.println("Incorrect Currency Code: " + choice +"." + " Next time please choose from CD (Canadian Dollar), " + "MP (Mexican Peso), or EU (Euro)." +" Transaction aborted");

How can I get values from my other code to this method so it can compute? :

/*return type double; 
	*parameters: double rate, double amount, boolean flag;
	*computes and returns the dollar cost of a transaction of the 
	*given exchange rate 'rate', currency amount 'amount' and buy value true 
	*for buying and false for selling, respectively; 
	*/
	
	public static Double priceInDollars(double rate, double amount, boolean flag){
		double dollarValue = rate * amount;
		return dollarValue;
	}

Thanks for all your help so far!

How can I get values from my other code to this method

Not sure what you mean. The method is passed values as arguments. The caller of the method will provide the values in the method call.

I am just confused I think with how it is all communicating with each other. So it would be something like this? :

public static Double priceInDollars(double rate, double amount, boolean flag){
		amount = inputAmount();
		//rate = ????
		//where does flag come in?
		double dollarValue = rate * amount;
		return dollarValue;
	}

I think I get where amount would come in, but I don't know how to get rate, because that information in is the file that is read in through scanner. Also, I'm not sure how to incorporate a true or false boolean value.

The caller of the method should get the value for amount and pass it to the method as the second argument.
The same is true for all the values. The caller gets them and passes them to the method via its arguments.

The caller of the method should get the value for amount and pass it to the method as the second argument.
The same is true for all the values. The caller gets them and passes them to the method via its arguments.

Could you show me an example of this? Thank you.

arg1 = get the value somewhere
arg2 = get the value
arg3 = get the value

val = aMethod(arg1, arg2, arg3); // call a method with 3 arguments

I kind of understand the concept, but I dont know how to get the proper values to those parameters.

Start with where the data comes from. Then provide code to get it from there to where you call the method that needs the data.

Where does the amount come from?
Where does the rate come from?
Where does the flag come from?

You need to get those three values BEFORE you call the method that uses them.

Remember what James suggested: do it one step at a time.
Work on getting the amount and print it out to see if your code gets the correct value.

When that works move on to getting the rate.

Where does the amount come from? Comes from inputAmount() method
Where does the rate come from? Comes from dailyrates.txt which is read by readRates() method
Where does the flag come from? I think comes from buyOrSell() method

public static Double priceInDollars(double rate, double amount, boolean flag){
		amount = inputAmount();
		rate = readRates();//error because readRates() has no return value
		flag = buyOrSell();
		double dollarValue = rate * amount;
		return dollarValue;
	}

Reposting code for whole program:

import java.io.*;
import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class CurrencyExchange {

	final static double MIN_COMISSION = 5;
	static double commissionPerCent;
	static double buyingRateC;
	static double sellingRateC;
	static double buyingRateM;
	static double sellingRateM;
	static double buyingRateE;
	static double sellingRateE;
	static double amount;
	static char code;
	static boolean buy;
	static double rate;
	DecimalFormat df = new DecimalFormat();
	
	/*displays the welcoming message;
	*calls readRates( ); 
	*runs a while loop as long as newTransaction( ) returns YES; 
	*the loop calls the methods input Currency( ), inputAmount( ), 
	*buyOrSell( ), displayResult( );
	*sends the closing message "Exchange is closed for the day." 
	*to the console and terminates the process.
	*/
	public static void main(String[] args) throws IOException {
		JOptionPane.showMessageDialog(null,"Welcome to the Modest International Currency Exchange Services!", "Modest International",1);
		readRates();
		
		System.out.println("input currency: " + inputCurrency());
		System.out.println("input amount: " + inputAmount());
		System.out.println("buy or sell: " + buyOrSell());
		
		while(newTransaction() == true){
			inputCurrency();
			inputAmount();
			buyOrSell();
			displayResult();
		}
	}
	
	/*return type double; 
	*parameters: double rate, double amount, boolean flag;
	*computes and returns the dollar cost of a transaction of the 
	*given exchange rate 'rate', currency amount 'amount' and buy value true 
	*for buying and false for selling, respectively; 
	*/
	
	public static Double priceInDollars(double rate, double amount, boolean flag){
		amount = inputAmount();
		rate = readRates();//error because readRates() has no return value
		flag = buyOrSell();
		double dollarValue = rate * amount;
		return dollarValue;
	}
	
	
	/*return type double; no parameters;
	*runs a switch controlled by the 'code' data field as its selector; 
 	*for each case of the code and the accompanying buy value the switch determines and 
 	*assigns the current rate of the transaction; 
 	*the method composes and returns the output report to be 
	*written to the output file; calls the priceInDollars( ) method 
	*to obtain the dollar value which is to be included in the report;
	*a typical case from the switch is shown in the Hints section
	*/
	public static String composeReport(){
		
		return;
	}
	
	
	/*void; no parameter; calls the composeReport( ) method and 
	 * writes the return value to the output file; calls the 
	 * priceInDollars( ) method and displays the output message
	 */
	public static void displayResult(){
		
	}
	
	
	/*void; parameter: String line; the parameter must be formatted 
	 * as a line shown in the input file; instantiates a Scanner object to tokenize the 
	 * parameter string; uses a nested if-else sequence to assign the second 
	 * token to the correct rate variable or to the commission
	 */
	public static void assignRate(String line){
		
	}
	
	
	/*void; no parameter; 
	*throws IOException; instantiates a Scanner object and runs a 
	*while loop to read the lines of the input file; calls the 
	*assignRate() method for every input line, passing the line to 
	*the method as the parameter
	*/
	public static void readRates() throws IOException{
	File f = new File("dailyrates.txt");	
	Scanner y = new Scanner(f);
	String com;
	String buyC;
	String sellC;
	String buyM;
	String sellM;
	String buyE;
	String sellE;
	while(y.hasNext()){
		com = y.next();
		commissionPerCent= y.nextDouble();
		buyC = y.next();
		buyingRateC= y.nextDouble();
		sellC = y.next();
		sellingRateC=y.nextDouble();
		buyM = y.next();
		buyingRateM= y.nextDouble();
		sellM = y.next();
		sellingRateM=y.nextDouble();
		buyE = y.next();
		buyingRateE= y.nextDouble();
		sellE = y.next();
		sellingRateE=y.nextDouble();
	}
	}
	
	/*return type char; no parameter; 
	 * opens a JOptionPane input window to obtain the currency code from the user; 
	 * if the input string is null or empty, the program offers a new transaction; 
	 * otherwise returns the first character of the input string.
 	*/
	public static char inputCurrency(){
		String choice = JOptionPane.showInputDialog(null, "We Exchange the following currencies:\nCD (Canadian Dollar) \nMP (Mexican Peso) \nEU (Euro) " +
				"\nEnter the currency code:", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);

		if(choice.equalsIgnoreCase("CD"))
		{
			code = 'c';
		}else if(choice.equalsIgnoreCase("MP"))
			{
				code= 'm';
			}else if(choice.equalsIgnoreCase("EU"))
				{
					code = 'e';
				}else{
					System.out.println("Incorrect Currency Code: " + choice +"." + " Next time please choose from CD (Canadian Dollar), " +
							"MP (Mexican Peso), or EU (Euro)." +" Transaction aborted");
				}
			
		return code;
	}
	
	/*return type double; no parameter;  
	*opens a JOptionPane input window to obtain the currency amount
	*from the user; if the input string is null or empty, the program offers a
	*new transaction; otherwise returns the converted numerical value of 
	*the input numeric string (use parseDouble)
	*/
	public static double inputAmount(){
		String inputAmount = JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest International", 
                JOptionPane.QUESTION_MESSAGE);
		if(inputAmount == " "){
			JOptionPane.showInputDialog(null, "Please Enter Amount", "Modest International", 
	                JOptionPane.QUESTION_MESSAGE);
		} else {
			amount = Double.parseDouble(inputAmount);
		}
		return amount;
		
	}
	
	/*no parameter; the method must not be void, but you choose the return
	 * type as you see it best; 
	 * opens a JOptionPane input window to obtain the buy/sell option from
	 * the user; if the input string is null or empty, the program offers a new
	 * transaction; otherwise returns a value depending on the input string.
	 */
	public static boolean buyOrSell(){
		String buyOrSell = JOptionPane.showInputDialog(null, "Please \n type \"b\" or \"buy\" if you want to buy and" +
				" \n type \"s\" or \"sell\" if you want to sell the amount.", JOptionPane.QUESTION_MESSAGE);
		if(buyOrSell == "b"){
			buy = true;
		}else if(buyOrSell == "s"){
			buy = false;	
		}
		return buy;
		
	}
	
	/*return type boolean; no parameter; opens a Confirm Dialog and
	*returns true for JOptionPane.YES_OPTION, false otherwise
	*/
	public static boolean newTransaction(){
		int result = JOptionPane.showConfirmDialog(null,"Would you like to make a transaction?", "Modest International",     
		           JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
		if(result == JOptionPane.YES_OPTION){
		return true;
		}else{
			System.out.print("Modest International is closed for the day. See you tomorrow!");
			return false;
		}
		}

}

You should remove this line from the priceInDollars() method:
amount = inputAmount();

That value should come into the method via its parameters.

So since the inputAmount method already returns the value as amount, then I just call inputAmount method? Like this:

public static Double priceInDollars(double rate, double amount, boolean flag){
		      inputAmount();
		      rate = readRates();//error because readRates() has no return value
		      flag = buyOrSell();
		      double dollarValue = rate * amount;
		      return dollarValue;
	}

There is no need to call the inputAmount() method as you show because nothing it does should effect what the priceInDollars() method does. Remove it.

Ok so I removed

inputAmount();
		      rate = readRates();
		      flag = buyOrSell();

from the code and added this test line:

System.out.println("price in dollars: " + priceInDollars(rate, amount, buy));

And it prints out 0.0 to the console. I don't think it is getting the proper values to do the computation.

The method has been changed to:

public static Double priceInDollars(double rate, double amount, boolean buy){
		double dollarValue = rate * amount;
		return dollarValue;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.