Results 1 to 10 of 10
  1. #1
    CarsonUK is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default Java cmdline CurrencyConverter

    Hey guys, new to the forum, quite sure I'm posting in the right place.

    I was wondering if anyone could help me, I need to build a small cmdline based Java currency converter (pounds () into Euro's and vice versa) in Netbeans, it needs to ask the user which way to convert, the amount they want to convert and what the current exchange rate is.

    Basicly, my Java isn't great :(, I know what I want to do and I've tried doing it but alas the maths involved etc isn't my strong point either. I have tried using classes and a selection class but to no avale.

    Like I say there is no GUI, purely cmd line.

    Now after writing this I have realised I have left my code at uni, will post what I have so far tommorrow :p. But at least this get you thinking ;).

    Any help would be much appiecated. Rob.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that we'll need to see your code in order to help guide you. Otherwise the best we can do is refer you to the Sun tutorials.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, it's better to post your code. We cannot say anything without looking into that. Basically your requirement can be breakdown into three simple steps.

    1. Get the information/data from the user
    2. Do the process (calculation, data storing if any and so on)
    3. Display results to the user

    Like that try to view your question in simple way first, so you can easily find the solution by yourself.

  4. #4
    CarsonUK is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Sorry for the delay guys, here is my code, made the menu no probs, just havn't a clue about the maths invovled. Thanks again.

    CODE:

    package SoftwareDesign;

    /**
    *
    * @author rncarson
    */
    public class CurrencyConverter {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    String Exchange;
    String Deciesion;
    String Amount;

    System.out.println("Please enter the current exchange rate: " +"");
    Scanner scan = new Scanner(System.in);
    Exchange = scan.next();

    System.out.println("Would you like to convert from POUND to EURO or from EURO to POUND? " +"");
    Scanner scan = new Scanner(System.in);
    Deciesion = scan.next();

    System.out.println("Please enter the amount you would like to exchange: " +"");
    Scanner scan = new Scanner(System.in);
    Exchange = scan.next();


    }

    }

    Rob.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is online now Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    If u google,u will get lot of mathematical logic.But,do it urself.

    Anyway for receiving number data u are having method like nextInt,nextDouble() etc.No need to take as String and later convert problems........
    Ramya:cool:

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Now you know the basic steps to collect user information. Think about the following code segment you've post here.

    Java Code:
    System.out.println("Would you like to convert from POUND to EURO or from EURO to POUND? " +"");
    Scanner scan = new Scanner(System.in);
    Deciesion = scan.next();
    How you going to handle the user input here now, I mean how can a user enter the option. If user type "Pound to Euro" how you going to process it? Lets start from there...

  7. #7
    CarsonUK is offline Member
    Join Date
    Oct 2009
    Posts
    3
    Rep Power
    0

    Default

    Hey guys, soz for the late reply and thank you all for your input, I went on the Sun libarys and with abit of help from my GF i was able to create a fully fuctional currency converter :D.

    Heres my Java Main Class:

    package SoftwareDesign;
    import java.text.NumberFormat;
    import java.util.Locale;
    import java.util.Scanner;

    /**
    *
    * @author rncarson
    */
    public class CurrencyConverter {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    Double Exchange;
    Double conversionToEuros;
    Double Amount;
    String s1;
    char D1;

    NumberFormat Pound1 = NumberFormat.getCurrencyInstance();
    NumberFormat Euro1 = NumberFormat.getCurrencyInstance(Locale.FRANCE);
    Conversion Converter1 = new Conversion();

    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter the current exchange rate(Pounds to Euros): " +"");
    Exchange = scan.nextDouble();
    Converter1.setExchange(Exchange);

    System.out.println("Please enter the amount you would like to exchange: " +"");
    Amount = scan.nextDouble();
    Converter1.setAmount(Amount);

    System.out.println ("Please select the correct converter you would like to use");
    System.out.println ("POUND to EURO = A");
    System.out.println ("EURO to POUND = B\n");

    System.out.println("Select converter now: " +"");

    s1 = scan.next().toUpperCase();
    D1 = s1.charAt(0);

    switch (D1) {
    case 'A':
    System.out.println("You chose \"" +
    "POUND to EURO" + "\"");
    System.out.println("Amount after conversion: " + Euro1.format(Converter1.conversionToEuros()));
    break;

    case 'B':
    System.out.println("You chose \"" +
    "EURO to POUND" + "\"");
    System.out.println("Amount after conversion: " + Pound1.format(Converter1.conversionToPounds()));
    break;
    default:
    System.out.println("Not a valid converter. Please run the program again");
    }
    }
    }

    And heres the Java Class:

    package SoftwareDesign;

    /**
    *
    * @author rncarson
    */
    public class Conversion {

    private double Exchange;
    private double Amount;
    private double conversionToEuros;
    private double conversionToPounds;

    public Conversion () {
    }
    public double getExchange() {
    return Exchange;
    }
    public void setExchange (double currentRate) {
    this.Exchange = currentRate;

    }
    public double getAmount() {
    return Amount;
    }
    public void setAmount (double currentAmount) {
    this.Amount = currentAmount;
    }

    public double conversionToEuros() {
    return (Exchange * Amount);
    }

    public double conversionToPounds() {
    return (Amount / Exchange);
    }
    }


    thanks again and I hope this helps someone else with a simular problem. If anyone spots any errors in my code, feel free to say :).

    Carson

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please don't use color tags but code tags. Else your code is nigh impossible to read. You don't have to repost anew, but can edit your post above mine.

    Thanks for your cooperation and much luck!

  9. #9
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    what if your user doesn't enter an a or b? you need to either catch an exception or case all other possible inputs....? will it crash?

  10. #10
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    what if the user doesn't enter an a or a b or an A or a B
    Java Code:
    if(D1 != 'a' || D1 != 'b' || D1 !='A' // etc
    
    { do_something();} //catch my drift

    make them restart the program? they will just google it and say screw that program.....
    Last edited by aaroncarpet; 11-19-2009 at 06:21 AM.

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
  •