Results 1 to 9 of 9
  1. #1
    javaNewblet is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default FileWriter/Method Calling Problem

    Hi all,
    I have just about completed this code, but am having trouble at the end. I need my displayReport() method to write data to a .txt file. I call the method in a while loop that is in the main method, but I do not think it is actually being called for some reason because it is not writing to a file. I ran the code for a file write and tested it in another program(with a different string to print) and it worked fine so I think the FileWriter/PrintWriter portion of the code is right, but I may be calling it wrong. I hope you guys can help, I've been trying to figure this out all day. Thanks for any help in advance! Here is my code for the program.

    Java Code:
    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;
    		}
    	}
    
    }
    Last edited by javaNewblet; 11-11-2011 at 05:17 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: FileWriter/Method Calling Problem

    A gazillion lines of unformatted code and a vague description of the problem. I don't like your chances of getting help. When posting code use code tags: use [ code ] before and [ /code ] after your code (without the spaces). I see you have a print statement in the dsiplay method. Is that actually displaying anything? If not then the method is not being called and you need to debug your code to see why.

  3. #3
    javaNewblet is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: FileWriter/Method Calling Problem

    Sorry, I edited it. I put the println in the displayResult() method to test to see if the method is properly called. It does not print so the method is not being properly called. I do not understand where I need to put the method to be properly called so that it functions correctly. The other methods that are called in main function as they should but the displayResult() method does not. I do not understand it. Thanks for the reply and help!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: FileWriter/Method Calling Problem

    That is why I suggested debugging your code. It requires going through it line by line seeing what is actually getting executed. You basically pretend you are the computer. For me to find your problem I would have to do exactly that but (I hate to be blunt) why should I? I don't care if it works or not. Whereas you do and besides this is a great learning experience.

  5. #5
    javaNewblet is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: FileWriter/Method Calling Problem

    I know it is being called wrong, otherwise it would println and say it was called. So the calling of the method is wrong. But the assignment says that it is called in the while loop in the main. So I do not understand why it does not work if that is where it is to be called. It has to be in the while loop for the program so that the text is appended to the file each time a user does a transaction.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default Re: FileWriter/Method Calling Problem

    That while loop is only entered if a user responds that they want to try another transaction. So what happens for the first transaction? Despite my encouragement to debug your own code it seems I have to do it for you. I tried running your code and you have numerous logic problems. If I provide incorrect input for any of the questions (dialog boxes) it goes off into a tailspin of incorrect execution. For example even after entering no for another transaction it kept asking my to input an amount and if I wanted to buy or sell.
    Last edited by Junky; 11-11-2011 at 06:00 AM.

  7. #7
    javaNewblet is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: FileWriter/Method Calling Problem

    I do not really understand why it does that, because if newTransaction() returns 1 shouldn't it close? Also, if the while loop in the main is only entered if a new transaction is needed, how is it even called in the first place. Sorry, I'm having trouble knowing how my own code is working.

  8. #8
    javaNewblet is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: FileWriter/Method Calling Problem

    Oh I get the while loop problem I think. I need to run the program and if newTransaction is false, then displayReport() and it should write the data then? It wasn't writing any data because the code is buggy and it isn't actually entering the while loop to reach displayReport()?

  9. #9
    javaNewblet is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: FileWriter/Method Calling Problem

    So it should be this instead:

    Java Code:
    		while(newTransaction()==0){
    			
    			inputCurrency();
    			inputAmount();
    			buyOrSell();
    			displayResult();
    			
    			if(newTransaction()==1){
    				displayResult();
    			}
    		}
    Which I tried but still does not write data to the file.

Similar Threads

  1. problem with FileReader and FileWriter
    By abetemari in forum New To Java
    Replies: 20
    Last Post: 03-28-2011, 01:53 AM
  2. Calling method problem
    By jmoutia in forum New To Java
    Replies: 7
    Last Post: 10-24-2010, 09:06 AM
  3. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 11:40 AM
  4. Problem calling a method
    By ToeJam in forum New To Java
    Replies: 5
    Last Post: 12-12-2009, 01:45 AM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •