Results 1 to 9 of 9
  1. #1
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default Report Display Issue

    I am trying to add a report summary to my code. I have it it working to where it displays but the issue when it does display for the country and rate no matter how many times the currency exchange is done, the country and rates are what was the last choice to convert. I need some help fixing this minor issue. The code for both files are below. Thanks ahead for whomever helps me with this issue

    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Lab9
    {
    	
    
    	
    	
    	public static void main(String[] args)
    	{
    		final int Max = 20;
    		int c = 0;
    		char choice, report;
    		String s, r;
    	   Foreign[] exchange = new Foreign[Max];
    		Scanner Keyboard = new Scanner(System.in);
    		
    		Foreign.title();
    		Foreign.menu();
    		
    	do
    		{	
    		exchange[c] = new Foreign();
    		exchange[c].choice();
    		exchange[c].dollars();
    		exchange[c].vertical();
    		System.out.println("\n" + exchange[c]);
    		c++;
    				
    		System.out.print("\nWould you like to enter another exchange? Y/N ");
    		s = Keyboard.nextLine();
    		choice = s.charAt(0);
    		choice = Character.toUpperCase(choice);
    		}
    		
    		
    	while (choice == 'Y' && c < Max);
    	
    	
    		
    		System.out.println("\nWould you like to see a summary report? Y/N ");
    		r = Keyboard.nextLine();
    		report = r.charAt(0);
    		report = Character.toUpperCase(report);
    
    		if (report == 'Y')
    		{
    		System.out.println("\nCountry\t\tRate\t\tDollars\t\tAmount");
    	             System.out.println("========\t\t=======\t\t=======\t\t=========");
    		for (int i=0; i < c; i++)
    		System.out.println(exchange[i]);
    
    		Foreign.Counter();
    			}
    
    	}
    }
    Java Code:
    import java.util.Scanner;
    import java.text.NumberFormat;
    
    public class Foreign
    {
    	private static int choice, count;
    	private String country;
    	private double dollars, rate, amount;
    	private static double[] rates = {1.2418,14.5436,88.9183,0.774078};
    	private static String[] countries = {"Canadian Dollars", "Mexican Pesos", 
                                                                "Japanese Yen", "Euros"};
    
    	NumberFormat amountForm = NumberFormat.getCurrencyInstance();
    	NumberFormat valueFormat = NumberFormat.getCurrencyInstance();
       NumberFormat dollarFormat = NumberFormat.getCurrencyInstance();
    	Scanner Keyboard = new Scanner(System.in);
    
    
    	public Foreign()
    	{
    				dollars = 0.0;
    				rate = 0.0;
    				amount = 0.0;
    				country = "null";
    	}
    
    	public static void title()
    	{
    	System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
    	System.out.println("			Foreign Exchange");
    	System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n");
    	}
    	
    	public static void Counter() // counter
    	{
    	System.out.println("\n\tThe total number of conversions is " + count);
    	}
    		
    	public static void menu()
    	{
    	System.out.println("======================================================");
    	System.out.println("                  Foreign Exchange Menu");
    	System.out.println("======================================================\n\n");
    	}
    	
    	public void choice()
    	{
    		for (int i=0; i<4; i++) System.out.println(i+1 + "  " + "U.S. to " + countries[i]);
    		System.out.print("\nPlease enter your choice: ");
    		choice = Keyboard.nextInt();
    		count++;
    	}
    	
    	public int getChoice()
    	{
    		return choice;
    	}		
    	
    	public void dollars()
    	{
    	System.out.print("\nEnter the amount of U.S. Dollars you want to convert: ");
    	dollars = Keyboard.nextDouble();
    	rate = rates[choice-1];
    	country = countries[choice-1];
    	amount = dollars * rates[choice-1];
    	}		
    
    	public void vertical()
    	{
    	System.out.println("\nCountry = " + countries[choice-1]);
    	System.out.println("Rate = " + valueFormat.format(rates[choice-1]));
    	System.out.println("Dollars = " + dollarFormat.format(dollars));
    	System.out.println("Value = " + amountForm.format(amount));
    	}
    	
    	public String toString()
    	{
    	String line;
    	line = countries[choice-1] + "\t" + valueFormat.format(rates[choice-1]) + "\t\t" 
    	+ dollarFormat.format(dollars) + "\t\t" + amountForm.format(amount);
    	return line;
    	}
    	
    }
    Last edited by JDAWG; 03-27-2009 at 07:53 AM.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    problem found in declaring improper static variable choice

  3. #3
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    still not sure what you mean or what I am doing wrong. what should the code look like?

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  5. #5
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    Because my instructor has us do labs and each lab we just adjust the previous one. in the lab before he wanted us to make choice an static int... So you are basically saying I need to change that from being static to just private int choice? Thanks

  6. #6
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

  7. #7
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    yes means doesnt change. I guess I was hoping you might understand more why my instructor at the time would have make choice static and now try to see whos paying attention and change that to just a private int...

  8. #8
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    static variable means that variable share with all objects come from the same class

  9. #9
    JDAWG is offline Member
    Join Date
    Feb 2009
    Posts
    22
    Rep Power
    0

    Default

    So isnt the static choice in the same class?? From what I wrote I thought I only had one class called Foreign?? Am i missing something?

Similar Threads

  1. Struts2 display tag Issue
    By nickraj in forum Web Frameworks
    Replies: 0
    Last Post: 02-26-2009, 07:03 PM
  2. Issue in calendar display
    By jeeva in forum New To Java
    Replies: 1
    Last Post: 01-14-2009, 02:19 PM
  3. Replies: 8
    Last Post: 12-29-2008, 08:12 AM
  4. Replies: 0
    Last Post: 06-28-2008, 10:26 PM

Posting Permissions

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