Question

In: Computer Science

Your task is to create a Java application that converts a specified amount of one currency...

Your task is to create a Java application that converts a specified amount of one currency into another currency for a user. The application must meet the following requirements:

  1. Upon execution, the application shall display a brief welcome message announcing that the user is running the currency converter application.
  2. The application shall allow a user to convert amounts between any two of the following currencies: United States Dollars (USD), British Pounds (GBP), Swiss Francs (CHF), and Indian Rupees (INR).
  3. The application shall prompt the user for the following input: the currency she wishes to convert from (“source currency”); the amount of currency to be converted (“requested amount”) as either a decimal or a whole number (e.g., 21.45, 1.748484, or 34); and the currency she wishes to convert to (“result currency”).
  4. The application shall limit the amount of the currency to be converted to no more than 1,000,000 (one million) units of that currency; if the user attempts to enter an amount larger than this number, the application should return a message stating that the user has exceeded the maximum amount, and prompt the user to enter another amount.
  5. The application shall handle incorrect user input (e.g., if the user inputs a character or string for the “requested amount” value) and prompt the user for correct input.
  6. The application shall prevent the user from attempting to convert to a “result currency” that is the same as the “source currency” (e.g., the user should be prevented from converting United States Dollars to United States Dollars).
  7. The application shall output the following information: the amount of the “result currency” that is equivalent to the “requested amount” in the “source currency”; this amount shall be returned as a decimal figure rounded to two decimal places (e.g., 67.49).
  8. The application shall allow the user to exit the program at any time by accepting input of an exit character (for example “Q” for quit) to terminate the program.
  9. After the user has successfully converted a currency, the application shall prompt the user to either perform another conversion or terminate the program.
  10. Upon program termination, the application shall display an exit message to the user.

Program Data

Your program should use the following data for currency conversions.

USD

GBP

CHF

INR

USD

1.000000

1.29498

1.09820

0.0136097

GBP

0.772268

1.000000

0.848271

0.0105101

CHF

0.910585

1.17887

1.000000

0.0123905

INR

73.4772

95.1467

80.7070

1.000000

Here is some sample data you can use to verify your calculations:

20.00 USD = 15.45 GBP

20.00 USD = 18.21 CHF

20.00 USD = 1469.54 INR

20.00 CHF = 21.96 USD

20.00 CHF = 16.97 GBP

20.00 CHF = 1614.14 INR


50.00 INR = 0.68 USD

50.00 INR = 0.53 GBP

50.00 INR = 0.62 CHF


Please note that currency rates fluctuate continuously in the real world. The table outlined above sets baseline rates that I will be using to verify your calculations. If you use online currency converters to verify your results, they may be using real-time data that will result in calculations that are different from those made using the conversion rate table above.

You may store this data in any fashion you like for this assignment. I would recommend using arrays (single-dimensional or multi-dimensional) for this purpose.

Solutions

Expert Solution

// CurrencyConverter.java

import java.util.InputMismatchException;
import java.util.Scanner;

public class CurrencyConverter {

   public static void main(String[] args) {

       // array to store the valid currencies
       String valid_currency[] = {"USD", "GBP", "CHF", "INR"};
      
       // array to store the conversion charts, source currency - rows, destination currency - columns
       double conversion_chart[][] = { {1.00,    0.772268 , 0.910585 , 73.4772},
                                       {1.29498, 1.00, 1.17887, 95.1467},
                                       {1.09820, 0.848271, 1,00, 80.7070},
                                       { 0.0136097, 0.0105101, 0.0123905, 1.00}};
      
       int src_index = -1, dst_index = -1;
      
       String src_currency, dst_currency, choice;
       double amount_to_convert, converted_amount;
       Scanner scan = new Scanner(System.in);
      
       // loop that continues until the user exits
       do
       {
           // input the source currency
           System.out.print("Enter the source currency(USD/GBP/CHF/INR): ");
           src_currency =scan.nextLine();
           // validate source currency and re-prompt until valid
           while(!src_currency.equalsIgnoreCase("usd") && !src_currency.equalsIgnoreCase("gbp") && !src_currency.equalsIgnoreCase("chf") && !src_currency.equalsIgnoreCase("inr"))
           {
               System.out.print("Valid currency are: USD, GBP, CHF and INR. Re-enter source currency: ");
               src_currency =scan.nextLine();
           }
          
           // input conversion amount
           System.out.print("Enter the amount to convert: ");
           // validate conversion amount and re-prompt until valid
           while(true) {
               try
               {
                   amount_to_convert = scan.nextDouble();
                   if(amount_to_convert > 1000000)
                   {
                       System.out.print("Cannot convert currency more than 1,000,000 (one million) units. Re-enter: ");
                   }else
                       break;
               }catch(InputMismatchException e)
               {
                   System.out.print("Invalid input for amount to convert. Re-enter: ");
                   scan.nextLine();
               }
           }
          
           scan.nextLine(); // ignore \n left by nextDouble
          
           // input destination currency
           System.out.print("Enter the destination currency(USD/GBP/CHF/INR): ");
           dst_currency =scan.nextLine();
           // validate destination currency and re-prompt until valid
           while(!dst_currency.equalsIgnoreCase("usd") && !dst_currency.equalsIgnoreCase("gbp") && !dst_currency.equalsIgnoreCase("chf")
                   && !dst_currency.equalsIgnoreCase("inr") || ((dst_currency.equalsIgnoreCase(src_currency))))
           {
               if(dst_currency.equalsIgnoreCase(src_currency))
                   System.out.print("Source and destination currency cannot be same. Re-enter: ");
               else
                   System.out.print("Valid currency are: USD, GBP, CHF and INR. Re-enter source currency: ");
               dst_currency =scan.nextLine();
           }
          
           // loop to get the index of source currency and destination currency in the array
           for(int i=0;i<valid_currency.length;i++)
           {
               if(valid_currency[i].equalsIgnoreCase(src_currency))
                   src_index = i;
               else if(valid_currency[i].equalsIgnoreCase(dst_currency))
                   dst_index = i;
           }
          
           // display the result of conversion
           System.out.printf("\nThe amount of the %s that is equivalent to %.2f in the %s: %.2f\n",src_currency, amount_to_convert
                       , dst_currency, (amount_to_convert*conversion_chart[src_index][dst_index]));
          
           // input the choice,if the user wants to perform another conversion or exit
           System.out.print("Perform another converion(C) or Terminate the program(Q)? ");
           choice = scan.nextLine();
           // validate choice and re-prompt if invalid
           while(!choice.equalsIgnoreCase("C") && !choice.equalsIgnoreCase("Q"))
           {
               System.out.print("Invalid input. Valid inputs are C or Q. Re-enter: ");
               choice = scan.nextLine();
           }
          
       }while(choice.equalsIgnoreCase("C"));
     
   }

}

// end of CurrencyConverter.java

Output:


Related Solutions

You are tasked with developing an application that converts US currency ($) to 3 other major...
You are tasked with developing an application that converts US currency ($) to 3 other major world currencies. Here are the current rates: 1 US dollar = 0.90 EURO 1 US dollar = 107.7 Japanese YEN 1 US dollar =19.45 Mexican PESO Your application must prompt the user to enter an amount in US dollars, convert it into EURO, YES, and PESO and then display all using a suitable format. Coding requirements: -All your variables MUST  declared inside main() except...
Create a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters.
JAVACreate a Java application that will exhibit concurrency concepts. Your application should create two threads that will act as counters. One thread should count up to 20. Once thread one reaches 20, then a second thread should be used to count down to 0.
Create a java Swing GUI application that presents the user with a “fortune”. Create a java...
Create a java Swing GUI application that presents the user with a “fortune”. Create a java Swing GUI application in a new Netbeans project called FortuneTeller. Your project will have a FortuneTellerFrame.java class (which inherits from JFrame) and a java main class: FortuneTellerViewer.java. Your application should have and use the following components: Top panel: A JLabel with text “Fortune Teller” (or something similar!) and an ImageIcon. Find an appropriate non-commercial Fortune Teller image for your ImageIcon. (The JLabel has a...
Create in java an interactive GUI application program that will prompt the user to use one...
Create in java an interactive GUI application program that will prompt the user to use one of three calculators, label project name MEDCALC. You can use any color you want for your background other than grey. Be sure to label main java box with “MEDCALC” and include text of “For use only by students” On all three calculators create an alert and signature area for each user. The alert should be something like “All calculations must be confirmed by user...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls:...
Java Please comment code Create an Interactive JavaFX Application Create an application with the following controls: A Label control with the following text displayed: "First Number:" A Label control with the following text displayed: "Second Number:" An empty TextField control beside the First Number label. An empty TextField control beside the Second Number label. Five buttons: Button 1 labeled + Button 2 labeled - Button 3 labeled * Button 4 labeled / Button 5 labeled = An empty Label control...
Create a Java application NumberLoops to practice loops. The draft has one loop to calculate a...
Create a Java application NumberLoops to practice loops. The draft has one loop to calculate a product and the final will add another loop to print integers in a required format. There is no starter code for the problem. Use a Scanner to input an integer then compute and display the product of all positive integers that are less than the input number and multiple of 3. If no numbers satisfy the conditions, then print out 1 for the product....
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java...
In Java Develop, test, and execute a graphics application for simulations using Java. Create a Java application. Given a set of events, choose the resulting programming actions. Understand the principles behind Java. Understand the basic principles of object-oriented programming including classes and inheritance. Deliverables .java files as requested below. Requirements Create all the panels. Create the navigation between them. Start navigation via the intro screen. The user makes a confirmation to enter the main panel. The user goes to the...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
Must be in Visual C# using windows forms : Create an application that opens a specified...
Must be in Visual C# using windows forms : Create an application that opens a specified text file and then displays a list of all the unique words found in the file. Hint: Use a LINQ method to remove all duplicate words.
Your task for this assignment is use the Java Canvas and Graphics classes to create an example of a computer generated image.
Your task for this assignment is use the Java Canvas and Graphics classes to create an example of a computer generated image. This is an opportunity for you to explore computer graphics and exercise some individual creativity. You should submit well-documented code, and once your program is done, create a presentation of your program. The presentation can be a PowerPoint or Word document, or something created with similar software. It could be a PDF file. Tell us what your prject is and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT