currency converter

w/ an arraylist...

Page 1 of 1

9 Replies - 10432 Views - Last Post: 23 February 2010 - 05:53 PM Rate Topic: -----

#1 justin23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

currency converter

Posted 13 February 2010 - 09:23 PM

import javax.swing.*;
import java.util.*;
class Chapter10_20{
	public static void main(String []args){
		ArrayList<String> al = new ArrayList<String>();
		String strFrom = JOptionPane.showInputDialog(null, "Enter initial type of currency :");
		String strTo = JOptionPane.showInputDialog(null, "Enter convertered typed of currency :");
		String strInt = JOptionPane.showInputDialog(null, "Enter intial amount of money to convert :");
		float cashCnvrt = Float.parseFloat(strInt);
		al.add(strFrom);
		al.add(strTo);
		al.add(cashCnvrt);
		System.out.println(al);
	}
}


i have a pretty good idea of what im supposed to be doing but im not very experienced with arraylists. My problem is that i have to have 3 argumens in my "exchange" method. The first two arguments are supposed to be strings(starting currency and converted currency) and the last argument must be a float. I've figured out that im not allowed to add a float argument into a string arraylist, so i was wondering if theres another way to convert that float to make it compatible with the arraylist, or do i have it all wrong and im not supposed to add these arguments into my arraylist?
thanks

Is This A Good Question/Topic? 0
  • +

Replies To: currency converter

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,794
  • Joined: 27-December 08

Re: currency converter

Posted 13 February 2010 - 09:33 PM

Design your own class, maybe call it Transaction. In Transaction, you want to store the two types of currencies and the initial amount. Then, just store an ArrayList<Transaction>. It is much easier to use a class!

I've written a tutorial on moving to using classes from parallel arrays that you might find helpful. While you're not using parallel arrays or ArrayLists, you may find the process for designing a class helpful. Here's the link: http://www.dreaminco...topic147196.htm
Was This Post Helpful? 0
  • +
  • -

#3 tobenna  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 23-January 09

Re: currency converter

Posted 14 February 2010 - 03:11 AM

I've done a unit converter although not in Java, but the best solution as was suggested earlier is to create your own class probably called currency.

Its constructor could have three arguments two string one for 'from' and another for 'to' and the third argument could be a float for the from value.

The class could contain three instance variables with set and get methods, and also convert methods not just one, but as many currencies as you want to adapt to your program. At least this should get you started.

One last thing I don't think its possible to put a float in an ArrayList of Strings.

This post has been edited by tobenna: 14 February 2010 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 justin23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Re: currency converter

Posted 18 February 2010 - 02:27 PM

if i make another Currency class and have an ArrayList<Currency> will the float be compatible in the ArrayList?
Was This Post Helpful? 0
  • +
  • -

#5 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: currency converter

Posted 18 February 2010 - 05:23 PM

Have a method inside your Currency class that returns the converted value as a float value.
public float getConvrt(){
    //return the converted value as a float data type
}


of course, to do the conversion, you will need to get the 2 String Objects as parameters in the Currency constructor. (or set them with a set method).

after you have that Currency class, add new Currency Objects to the Arraylist.
then just loop the ArrayList, and call the getConvert method for each object.
Was This Post Helpful? 0
  • +
  • -

#6 justin23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Re: currency converter

Posted 19 February 2010 - 12:52 PM

well im not very skilled with working with more than one class so i decided to just make an arrayList in my main class cause i've never made an arraylist of another class.
import javax.swing.*; 
import java.util.*; 
class Chapter10_20{ 
	public static void main(String []args){
		ArrayList al = new ArrayList();
		ArrayList al2 = new ArrayList();
		
		String strFrom = JOptionPane.showInputDialog(null, "Enter initial type of currency :"); 
		String strTo = JOptionPane.showInputDialog(null, "Enter convertered typed of currency :"); 
		String strInt = JOptionPane.showInputDialog(null, "Enter intial amount of money to convert :"); 
		double cashCnvrt = Double.parseDouble(strInt); 
			
		al.add(strFrom); 
		al.add(strTo); 
		al.add(strInt);
		
		String strRate = JOptionPane.showInputDialog(null, "Enter exchange rate from intial currency to new currency :");
		double rate = Double.parseDouble(strRate);
		
		al2.add(strTo);
		al2.add(strRate);

		System.exit(0);
	}
}

import javax.swing.*;
import java.util.*;
class Currency{
	private double rate, strInt;
	private String from, to;
	
	public void setRate(String from, double rate){
		this.from = from;
		this.rate = rate;
	}
	
}



like i said, im terrible with multiple classes and am struggling with the setRate method
any suggestions?
Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: currency converter

Posted 20 February 2010 - 07:17 AM

usually a set method should should handle only one variable.
so if the method is called setRate, it should only handle the rate of this currency object.
public void setRate(double rate){ 
                this.rate = rate; 
        } 


but first, create a constructor for your class:
public Currency(String fromSt, String toSt){
      this.fromSt = fromSt;
      this.toSt = toSt;
}

thus, when creating a new Currency object, you supply it with the "from" String and "to" String.
you could also pass a rate as parameter in the constructor.
Was This Post Helpful? 0
  • +
  • -

#8 justin23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-December 09

Re: currency converter

Posted 23 February 2010 - 01:53 AM

so ive created a set rate method properly and even a currency constructor but now im confused as to how to use these new methods
import javax.swing.*; 
import java.util.*; 
class Chapter10_20{ 
	public static void main(String []args){
		ArrayList al = new ArrayList();
		ArrayList al2 = new ArrayList();
		
		String strFrom = JOptionPane.showInputDialog(null, "Enter initial type of currency :"); 
		String strTo = JOptionPane.showInputDialog(null, "Enter convertered typed of currency :"); 
		String strInt = JOptionPane.showInputDialog(null, "Enter intial amount of money to convert :"); 
		double initialAmount = Double.parseDouble(strInt); 
		
		
		String strRate = JOptionPane.showInputDialog(null, "Enter exchange rate from intial currency to new currency :");
		double rate = Double.parseDouble(strRate);
		
		double convertedAmount = initialAmount * rate;
		
		
		Currency currency = new Currency(strFrom, strTo);
		currency.setRate(strTo, rate);
		currency.exchange(strFrom, strTo, initialAmount);
		

		
		System.out.println(convertedAmount + " " + strFrom);
		System.exit(0);
	}
}



import javax.swing.*;
import java.util.*;
class Currency{
	private double rate, initialAmount, newAmount;
	private String fromStr, toStr;
	
	public void setRate(String toStr, double rate){
		this.rate = rate;
	}
	
	public double getRate(){
		return rate;
	}
	
	
	
	public Currency(String fromStr, String toStr){
		this.fromStr = fromStr;
		this.toStr = toStr;
	}
	
	public String getFromStr(){
		return fromStr;
	}
	
	public String getToStr(){
		return toStr;
	}
	
	
	public double exchange(String fromStr, String toStr, double initialAmount){
		newAmount = initialAmount * rate;
		this.newAmount = newAmount;
		return newAmount;
	}
}



and im also supposed to keep track of the rates with an arraylist
would i just do something like
al.add(strFrom, strTo, strRate);

?

This post has been edited by justin23: 23 February 2010 - 01:54 AM

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,794
  • Joined: 27-December 08

Re: currency converter

Posted 23 February 2010 - 04:49 PM

No. ArrayList comes with two add() methods- add(elem), which appends the elem to the end of the list; and add(index, elem), which adds the specified element at the specified index in the list. If you want to add a new Currency object, just simply say a1.add(new Currency(..params..));. Lastly, you should specify the generic type of the ArrayLists if you do not want to get a deprecation warning. So if you want to store an ArrayList<Currency>, specify it as I just did.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: currency converter

Posted 23 February 2010 - 05:53 PM

Why do you just don't make a class with your 4 arguments

   class ToConvert {
      private String strTo, strFrom, strFrom;
      private double rate, amount;
      
// constructor
      ToConvert(String from, String to, String strRate, String strAmount) {
          strTo = to;
          strFrom = from;
          rate = Double.parseDouble(strRate);
          amount = Double.parseDouble(strAmount);
      }
}



And then in your main() just make an for those ToConvert objects

ArrayList<ToConvert> al = new ArrayList<ToConvert>()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1