0

The code you wrote will write a file.
Use a Search program to find where you wrote it.

Are you sure the displayResult() method is called? Add a println in the method to show that it is called.

0

The method is called but not writing anything to the file. I searched the hd for "dailyreport.txt" but nothing was found except the blank text file that I created and put into the source folder in eclipse.

0

I don't know what to say. The code I posted did write a file and then appended to it as I executed it several times more.
Take the 8 lines of code I posted just now, put them into a small test program, compile and execute it and see if a file is written.

except the blank text file that I created and put into the source folder

What does the blank text file have to do with this problem of not writing out a file?

Edited by NormR1: n/a

0

I don't know what to say. The code I posted did write a file and then appended to it as I executed it several times more.
Take the 8 lines of code I posted just now, put them into a small test program, compile and execute it and see if a file is written.


What does the blank text file have to do with this problem of not writing out a file?

It has nothing to do with it. I ran your code and it wrote the .txt file, but mine does not. I have been changing where my method is called and sometimes the println says that the displayRates() method is called but still does not write to the file, just displays in the console that the method has been called.

0

displayResult() is called in the main method as seen here:

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());
		System.out.println("rate: "+ rate + " amount: "+ amount + " buy "+ buy);
		System.out.println("price in dollars: " + priceInDollars(rate, amount, buy));

		while(newTransaction()==0){
			
			inputCurrency();
			inputAmount();
			buyOrSell();
			displayResult();
		}
		
	}

When it is in the while loop the println test for the method does not print, but if I move the displayResult() method outside of the while loop like this:

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());
		System.out.println("rate: "+ rate + " amount: "+ amount + " buy "+ buy);
		System.out.println("price in dollars: " + priceInDollars(rate, amount, buy));

		while(newTransaction()==0){
			
			inputCurrency();
			inputAmount();
			buyOrSell();
			
		}
		displayResult();
	}

This println test for it says the method has been called. When I do this though the program goes a little crazy and dialog boxes pop up when they shouldn't and it throws off the order of the while loop for "more transactions" by the user.

0

Change what is written to be a simple String.
Build a String to be written, write it and then print it.

try{
		FileWriter fw = new FileWriter("dailyreport.txt", true);
		BufferedWriter out = new BufferedWriter(fw);
		Strint output = "Customer " + buyOrSell() + " " + inputAmount() + " " + inputCurrency() + " " 
				+ priceInDollars(rate, amount, buy) + " dollars";
                out.append(output);    // write it
                System.out.println("output=" + output); // show what was written
		out.close();
	}catch(Exception e){
		System.err.println("Error: " + e.getMessage());
	}
0
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;
	static DecimalFormat df = new DecimalFormat("0.00");

	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());
		System.out.println("rate: "+ rate + " amount: "+ amount + " buy "+ buy);
		System.out.println("price in dollars: " + priceInDollars(rate, amount, buy));

		while(newTransaction()==0){
			
			inputCurrency();
			inputAmount();
			buyOrSell();
			displayResult();
		}
		
	}

	public static Double priceInDollars(double rate, double amount, boolean buy){
		
		double rateAmt = rate * amount;
		double baseCom = rateAmt * commissionPercent/100;
		double commission = Math.min(baseCom, 10);
		double dollarValue;
		if(buy){
			dollarValue = rateAmt += commission ;
		}else{
			dollarValue = rateAmt -= commission;
		}
		return dollarValue;
	}


	public static double composeReport(){
		priceInDollars(rate, amount, buy);
		switch (code){
		case 'C' : case 'c':
			if(buy){
				rate = buyingRateC;
			}
			else{
				rate = sellingRateC;
			}
			break;
		case 'M' : case 'm':
			if(buy){
				rate = buyingRateM;
			}
			else{ 
				rate = sellingRateM;
			}
			break;
		case 'E' : case 'e':
			if(buy){
				rate = buyingRateE;
			}
			else{
				rate= sellingRateE;
			}
			break;
		}
		
		return rate;
	}

	public static void displayResult() throws IOException{
		composeReport();
		try{
			FileWriter fw = new FileWriter("dailyreport.txt", true);
			PrintWriter out = new PrintWriter(fw);
			String output = ("Customer " + buyOrSell() + " " + inputAmount() + " " + inputCurrency() + " " 
					+ priceInDollars(rate, amount, buy) + " dollars" );
			out.append(output);
			System.out.println("output = " + output);
			out.close();
			}catch(Exception e){
				System.err.println("Error: " + e.getMessage());
			}
		
		if(newTransaction()==1){
			JOptionPane.showMessageDialog(null,"You receive\n" + priceInDollars(rate, amount, buy) 
					+ " dollars.\n We appreciate your business!"
					, "Payment",1);
		}else{
			JOptionPane.showMessageDialog(null,"Your charge for the transaction is: \n" + priceInDollars(rate, amount, buy) 
					+ " dollars.\n " + "We appreciate your business!", "Payment",1);
		}
		System.out.println("The method was called");
		
	}

	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();
		}	
		y.close();
	}

	public static char inputCurrency() throws IOException{
		
		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{
			JOptionPane.showMessageDialog(null,"Please enter correct currency code.", "Modest International",1);
			while(newTransaction()==0){
				inputCurrency();
				inputAmount();
				buyOrSell();
				displayResult();
			}

		}
		
		composeReport();
		return code;
	}


	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;

	}

	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.equalsIgnoreCase("b"))
		{
			buy = true;
		}else if(buyOrSell.equalsIgnoreCase("s"))
		{
			buy = false;	
		}
		return buy;	
	}

	public static int 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 0;
		}else{
			System.out.print("Modest International is closed for the day. See you tomorrow!");
			return 1;
		}
	}

}
0

When I executed the program it did NOT print anything with this line:
System.out.println("output = " + output);
That would mean that the displayResult() method is NOT being called.

0

You are correct and I am still stuck on the same problem. Not only that my code is so buggy I don't know where to begin. I am not going to have this done in time :/

0

One problem you are having is that you are not consistent when calling methods.
Your methods return values and also set global variables.
You should NOT do both. The preferred method is to have a method return a value and have the caller handle what to do with the value.
Setting global variables from methods is a very poor technique.

I recommend you convert your methods to return values and NOT set global variables.

Then you need to look at what the program is to do and call the methods in the correct order to do that task.

0

One problem you are having is that you are not consistent when calling methods.
Your methods return values and also set global variables.
You should NOT do both. The preferred method is to have a method return a value and have the caller handle what to do with the value.
Setting global variables from methods is a very poor technique.

I recommend you convert your methods to return values and NOT set global variables.

Then you need to look at what the program is to do and call the methods in the correct order to do that task.

So for example instead of what I have here:

public static double composeReport(){
		priceInDollars(rate, amount, buy);
		switch (code){
		case 'C' : case 'c':
			if(buy){
				rate = buyingRateC;
			}
			else{
				rate = sellingRateC;
			}
			break;
		case 'M' : case 'm':
			if(buy){
				rate = buyingRateM;
			}
			else{ 
				rate = sellingRateM;
			}
			break;
		case 'E' : case 'e':
			if(buy){
				rate = buyingRateE;
			}
			else{
				rate= sellingRateE;
			}
			break;
		}
		
		return rate;
	}

Do something like:

public static double composeReport(){
		priceInDollars(rate, amount, buy);

                double setRate;//The local variable I added.

		switch (code){
		case 'C' : case 'c':
			if(buy){
				setRate = buyingRateC;
			}
			else{
				setRate = sellingRateC;
			}
			break;
		case 'M' : case 'm':
			if(buy){
				setRate = buyingRateM;
			}
			else{ 
				setRate = sellingRateM;
			}
			break;
		case 'E' : case 'e':
			if(buy){
				setRate = buyingRateE;
			}
			else{
				setRate= sellingRateE;
			}
			break;
		}
		
		return setRate;
	}

Then later when I need rate I would use the global variable like this?:

rate = composeReport();

As far as calling the methods the way I did, I called them as the project described them to be. I attached a document(which I did not know I could do before) it describes what my methods should be doing and how they should be called.

0

Yes the new version looks like it returns a value and doesn't set a global.

However the priceInDollars() method does not seem to return value. It needs to be changed to work the same way. Return a value, do NOT set a global.

0
public static Double priceInDollars(double rate, double amount, boolean buy){
		
		double rateAmt = rate * amount;
		double baseCom = rateAmt * commissionPercent/100;
		double commission = Math.min(baseCom, 10);
		double dollarValue;
		if(buy){
			dollarValue = rateAmt += commission ;
		}else{
			dollarValue = rateAmt -= commission;
		}
		return dollarValue;
	}

priceInDollars(); returns its local variable dollarValue;

0

Why does your code in composeReport() call priceInDollars()?

Where does priceInDollars() get commissionPercent from?

0

Why does your code in composeReport() call priceInDollars()?

Where does priceInDollars() get commissionPercent from?

The method description in my project description says to call priceInDollars() in composeReport(). The priceInDollars() gets commissionPercent as it is read in from the file "dailyrates.txt".

0

Your code does NOT use the value returned. So why bother calling if you do not use the results?

The priceInDollars() gets commissionPercent as it is read in from the file "dailyrates.txt".

priceInDollars does NOT read the file to get that data.
Another example of using global variables.

0

readRates() method reads the file which has global variable commissionPercent in it, which is contained in priceInDollars().

0

What about the call to priceInDollars() that ignores the returned value?
Obviously the calling method does not need the value that is returned. So why call it?

0

What difference would there be in the way the program works if you removed the call to that method?
Why do you call it? What does it do that is needed?

0

You are right, before you posted I made this change and commented out the 2 extra method calls, which were not needed and did not make a difference:

public static double composeReport(){
		//priceInDollars(rate, amount, buy);
		switch (code){
		case 'C' : case 'c':
			if(buy){
				rate = buyingRateC;
			}
			else{
				rate = sellingRateC;
			}
			break;
		case 'M' : case 'm':
			if(buy){
				rate = buyingRateM;
			}
			else{ 
				rate = sellingRateM;
			}
			break;
		case 'E' : case 'e':
			if(buy){
				rate = buyingRateE;
			}
			else{
				rate= sellingRateE;
			}
			break;
		}
		
		return rate;
	}

	public static void displayResult() throws IOException{
		//composeReport();
		try{
			FileWriter fw = new FileWriter("dailyreport.txt", true);
			PrintWriter out = new PrintWriter(fw);
			String output = ("Customer " + buyOrSell() + " " + inputAmount() + " " + inputCurrency() + " " 
					+ priceInDollars(rate, amount, buy) + " dollars" );
			out.append(output);
			System.out.println("output = " + output);
			out.close();
			}catch(Exception e){
				System.err.println("Error: " + e.getMessage());
			}
		
		if(newTransaction()==1){
			JOptionPane.showMessageDialog(null,"You receive\n" + priceInDollars(rate, amount, buy) 
					+ " dollars.\n We appreciate your business!"
					, "Payment",1);
		}else{
			JOptionPane.showMessageDialog(null,"Your charge for the transaction is: \n" + priceInDollars(rate, amount, buy) 
					+ " dollars.\n " + "We appreciate your business!", "Payment",1);
		}
		System.out.println("The method was called");
		
	}
0

But I still cannot get the file to write because the method is not truly being called. So I tried adjusting the while loop in my main method to this:

if(newTransaction()==1){
			displayResult();
		}else{
		while(newTransaction()==0){
			
			inputCurrency();
			inputAmount();
			buyOrSell();
			displayResult();
		}
		}

But that still does not work.

0

You need to think about the logic flow through your program.
List the steps the program is to take to do its job.
Your logic goes here and there and asks questions here and there and gets very confused and lost.

What are the programs requirements?
What does the user need to provide and when?
What does the program do with the user's input?

0

In your last post, I have no idea what your code is supposed to do.

You need to rethink your logic and make it simpler.
For example You have too many calls in differnt places to newTransaction()
Why is that?

0

I have no idea my logic always seems to be messed up because I over complicate the problem, I will continue to look at it and hopefully find my unnecessary method calls.

0

Make a simple list of what the program needs to do:
Read the rates file
Begin loop:
Get info from user
compute ans
ask user if there is another
exit loop if use done
end loop

0

Ok, I need to get some of the bugs out of my code, so it starts running correctly. If I go through the original loop of input and click no on the dialogue box that ask if user wants to make another transaction, then even though I click no, the loop continues anyway and more input/dialog boxes continue to pop up.

Edited by javaNooblet: n/a

0

even though I click no, the loop continues anyway

Do you call newTransactions() at more than one place?
You should only call it at ONE place.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.