Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.DecimalFormat;
- import java.util.*;
- class main {
- private static DecimalFormat df2 = new DecimalFormat("#.##");
- public static void main(String[] args) {
- try {
- //Get the values from the user and assign these to the variables
- Scanner inputObj = new Scanner(System.in);
- System.out.println("Welke eenheid wilt u converten?");
- System.out.println("Keuze: EUR, USD, GBP");
- System.out.print("Converteer: ");
- String c1 = inputObj.next().toUpperCase();
- System.out.print("Naar: ");
- String c2 = inputObj.next().toUpperCase();
- System.out.print("Bedrag: ");
- double value = inputObj.nextDouble();
- //check what the user has desided to use to convert from
- switch (c1){
- case "USD":
- converterUSD(c1, c2, value);
- break;
- case "EUR":
- converterEUR(c1,c2,value);
- break;
- case "GBP":
- converterGBP(c1, c2, value);
- break;
- default:
- converterFailed();
- }
- //once the convertion has been run, ask if the user wants to convert more
- System.out.println("Wil je nog meer converteren? (Y/N)");
- String answer = inputObj.next();
- //if the answer is yes, re run the script
- if (answer.equals("Y")) {
- main(null);
- }
- //if value given by the user is not valid for the datatype return a error
- } catch (InputMismatchException exception) {
- System.out.println("De ingevulde waardes worden niet ondersteund, probeer het opnieuw.\n");
- main(null);
- }
- }
- static void converterUSD(String c1, String c2, double value) {
- if (c1.equals("USD") || c1.equals("DOLLAR")) {
- switch(c2) {
- case "USD":
- System.out.println("Waarom converteer je naar de zelfde valuta?\n");
- main(null);
- break;
- case "EUR":
- double eur = value * 0.91;
- System.out.println("USD " + value + " is EUR " + df2.format(eur) + "\n");
- break;
- case "GBP":
- double gbp = value * 0.78;
- System.out.println("USD " + value + " is GBP " + df2.format(gbp) + "\n");
- break;
- default:
- System.out.println("Ik weet niet wat ik hier mee aan moet,\nbegin maar overnieuw.\n");
- main(null);
- break;
- }
- }
- }
- static void converterEUR(String c1, String c2, double value) {
- if (c1.equals("EUR") || c1.equals("EURO")) {
- switch(c2) {
- case "EUR":
- System.out.println("Waarom converteer je naar de zelfde valuta?\n");
- main(null);
- break;
- case "USD":
- double usd = value * 1.10;
- System.out.println("EUR " + value + " is USD " + df2.format(usd) + "\n");
- break;
- case "GBP":
- double gbp = value * 0.86;
- System.out.println("EUR " + value + " is GBP " + df2.format(gbp) + "\n");
- break;
- default:
- System.out.println("Ik weet niet wat ik hier mee aan moet,\nbegin maar overnieuw.\n");
- main(null);
- break;
- }
- }
- }
- static void converterGBP(String c1, String c2, double value) {
- if (c1.equals("GBP") || c1.equals("POUND")) {
- switch(c2) {
- case "GBP":
- System.out.println("Waarom converteer je naar de zelfde valuta?");
- main(null);
- break;
- case "USD":
- double usd = value * 1.28;
- System.out.println("GBP " + value + " is USD " + df2.format(usd) + "\n");
- break;
- case "EUR":
- double eur = value * 1.17;
- System.out.println("GBP " + value + " is EUR " + df2.format(eur) + "\n");
- break;
- default:
- System.out.println("Ik weet niet wat ik hier mee aan moet,\nbegin maar overnieuw.\n");
- main(null);
- break;
- }
- }
- }
- static void converterFailed() {
- System.out.println("Ik weet niet wat ik hier mee aan moet,\nbegin maar overnieuw.\n");
- main(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement