不确定根据用户输入计算佣金

时间:2015-01-02 15:05:38

标签: java

public static void main(String[] args) {
    Scanner keybNum = new Scanner(System.in);
    Scanner keybStr = new Scanner(System.in);

    boolean yn = false;

    //Start of program
    System.out.println("Welcome to Currency Exchanger");
    System.out.print("Are you an Account Holder (y or n)?    ");
    String AccHolder = keybStr.next();
    boolean blnYN = true;

    //validation of y/n answer
    while (blnYN) {
        if (AccHolder.equalsIgnoreCase("y")) {
            yn = true;
            blnYN = false;
            break;
        }//if 
        else if (AccHolder.equalsIgnoreCase("n")) {
            yn = false;
            blnYN = false;
            break;
        }//else if 
        else {
            System.out.println("Invalid value entered. Choose either y/n.");
            AccHolder = keybStr.next();
        }//else
    }//while

    //Start of menu choices
    System.out.println("Please choose from the following menu.");
    System.out.println("\n1: Exchange another currency for Sterling");
    System.out.println("2: Buy another currency from us");
    System.out.println("0: Exit");
    System.out.print("Which option?    ");
    int MenuChoice = keybNum.nextInt();


    //Exchange Variables (First option)
    double Euro = 1.37;
    double USAD = 1.81;
    double JapYen = 190.00;
    double Zloty = 5.88;

    //Buy Variables (Second Option)
    double EuroB = 1.21;
    double USADB = 1.61;
    double JapYenB = 163.00;
    double ZlotyB = 4.89;

    //First menu choice screen
    if (MenuChoice == 1) {
        System.out.println("Currencies to exchange into sterling?");
        System.out.println("Euro - EUR");
        System.out.println("USA Dollar - USD");
        System.out.println("Japanese Yen - JPY");
        System.out.println("Polish Zloty - PLN");
        System.out.print("Please enter the three letter currency:   ");

        //Currency input validation
        String CurChoice = "";
        boolean isCorrectCurrency = false;
        do {
            CurChoice = keybStr.next();
            isCorrectCurrency = CurChoice.matches("^EUR|USD|JPY|PLN$");

            if (isCorrectCurrency) {
                System.out.println("");
            } else {
                System.out.print("Invalid Currency Entered. Please Retry: ");
            }

        } while (!isCorrectCurrency);

        //Exchange amount calculator
        System.out.print("Enter the amount you wish to exchange of " + CurChoice + ": ");
        double ExcAmount = keybStr.nextInt();
        double result = 0.00;

        //Selection and calculation of user's input
        if (CurChoice.equals("EUR")) {
            result = ExcAmount / Euro;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else if (CurChoice.equals("USD")) {
            result = ExcAmount / USAD;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else if (CurChoice.equals("JPY")) {
            result = ExcAmount / JapYen;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else if (CurChoice.equals("PLN")) {
            result = ExcAmount / Zloty;
            DecimalFormat df = new DecimalFormat("#.##");
            System.out.println(ExcAmount + " in " + CurChoice + "\t=£" + df.format(result));
        } else {
            System.out.println("");
        }

是的,这就是我被困住的地方。所以这个程序是货币兑换器。我不确定的部分是计算佣金。一开始我问用户他们是否是帐户持有人。如果他们没有收取佣金,而如果他们那么他们会收取佣金。如果他们希望交换的金额少于1000英镑,那么他们将收取2%的佣金,如果超过1000英镑,那么他们将收取1%的佣金。我不知道如何将这个实现到我的程序中,所以我要求你们帮忙。

1 个答案:

答案 0 :(得分:1)

计算结果后尝试此操作:

double commission = 0.;
if (ExcAmount < 1000) {
    commission = result * 0.02;
} else {
    commission = result * 0.01;
}

result += commission;
String stringToPrint = "";
if (!yn) { //you could use meaningful variable names
    stringToPrint = "Commission = " + commission;
} else {
    stringToPrint = "Commission = Not charged";
}
System.out.println(stringToPrint + "\nTotal = " + (result - commission));
相关问题