货币兑换计划

时间:2019-11-24 00:31:12

标签: java

附件是我为Lab项目完成的代码。我已完成所有工作,但由于某种原因我的代码处于闲置状态?我认为这可能是“ bank1”和“ bank2”对象与Bank类之间的问题吗?

我很抱歉代码很长,因为我不知道问题出在哪里。请帮助:(

货币兑换类(主要)

import javax.swing.*;
import java.io.FileNotFoundException;
import java.util.*;

public class CurrencyExchange {

    public static void main(String [] args) throws FileNotFoundException {
        //Create 2 bank objects, one for each file "bank1.txt" & "bank2.txt"
        Bank bank1 = new Bank("bank1.txt");
        Bank bank2 = new Bank("bank2.txt");
        int userCont = 0;

        do {
            boolean buy = false;
            double amount;
            int banksSupported = 0;
            String currencyCode;
            Scanner keyboard = new Scanner(System.in);
            String userAnswer;

        //Ask user to buy or sell - use continue to repeat loop if invalid
        System.out.println("Do you want to buy or sell?");
            userAnswer = keyboard.nextLine();
            if (!userAnswer.equalsIgnoreCase("buy") && (!userAnswer.equalsIgnoreCase("sell")) ) {
                System.out.println("Invalid response. Please enter buy or sell. ");
                continue;
            }
            if (userAnswer.equalsIgnoreCase("buy")) {
                buy = true;
            }

        //Ask user for foreign currency - use continue to repeat loop if invalid
        System.out.println("Which currency would you like to use?");
            currencyCode = keyboard.nextLine().toUpperCase();

        //Check and Print how many banks support the users currency - use continue to repeat loop if none
            if(bank1.supportCurrency(currencyCode))
                ++banksSupported;
            if(bank2.supportCurrency(currencyCode))
                ++banksSupported;

            if (banksSupported == 0) {
                System.out.println("There are no banks the support " + currencyCode + ". Please enter a different currency.");
                continue;
            }
            else if (banksSupported == 1) {
                System.out.println("One bank supports " + currencyCode + ".");
            }
            else {
                System.out.println("Two banks support " + currencyCode + ".");
            }

        //Prompt the user for the amount of foreign currency and get the user input
        System.out.println(" What amount of " + currencyCode + "?");
            amount = keyboard.nextDouble();

        //Use the quoteBuy or quoteSell methods of the Bank objects to get a Quote from every bank supporting the foreign currency.

        //Print the quote(s) from the bank(s), using the toString method of the Quote object(s).
        if(buy) {
            if (bank1.supportCurrency(currencyCode)) {
                System.out.println(bank1.quoteBuy(amount, currencyCode));
            }
            if (bank2.supportCurrency(currencyCode)) {
                System.out.println(bank2.quoteBuy(amount, currencyCode));
            }
        }
        else {
            if (bank1.supportCurrency(currencyCode)) {
                System.out.println(bank1.quoteBuy(amount, currencyCode));
            }
            if (bank2.supportCurrency(currencyCode)) {
                System.out.println(bank2.quoteBuy(amount, currencyCode));
            }
        }

        //Use the console to prompt the user for another transaction.
            userCont = JOptionPane.showConfirmDialog(null, "Would you like to preform another transaction?", "Contniue", JOptionPane.YES_NO_OPTION);

    } while(userCont == 0); // while ( the user wants to continue performing transactions )



    }
}

银行类

import java.io.*;
import java.util.*;

public class Bank {

    // Five private data fields: The name of the bank, The commission rate, Three fields for the three currencies exchanged by the bank.
    // Each of these should have type Currency.
    private String bankName;
    private double commissionRate;
    private Currency currencyCode1;
    private Currency currencyCode2;
    private Currency currencyCode3;

    public Bank(String fileA) throws FileNotFoundException {
        File bankFile = new File(fileA);
        Scanner keyboard = new Scanner(System.in);

        this.bankName = keyboard.nextLine();
        this.commissionRate = Double.parseDouble((keyboard.nextLine()));
        this.currencyCode1 = new Currency(keyboard.next(), Double.parseDouble(keyboard.next()));
        this.currencyCode2 = new Currency(keyboard.next(), Double.parseDouble(keyboard.next()));
        this.currencyCode3 = new Currency(keyboard.next(), Double.parseDouble(keyboard.next()));

        keyboard.close();
    }

    public boolean supportCurrency(String currencyCode) {
        return currencyCode.equalsIgnoreCase(currencyCode1.getCurrencyCode())
                || currencyCode.equalsIgnoreCase(currencyCode2.getCurrencyCode())
                || currencyCode.equalsIgnoreCase(currencyCode3.getCurrencyCode());
    }

    // Working on getRate method.
    public double getRate(String currencyCode) {
        double bankExchangeRate = -1.0;
                if (currencyCode.equalsIgnoreCase(currencyCode1.getCurrencyCode())) {
                    bankExchangeRate = currencyCode1.getExchangeRate();
                }
                else if (currencyCode.equalsIgnoreCase(currencyCode2.getCurrencyCode())) {
                    bankExchangeRate = currencyCode2.getExchangeRate();
                }
                else if (currencyCode.equalsIgnoreCase(currencyCode3.getCurrencyCode())) {
                    bankExchangeRate = currencyCode3.getExchangeRate();
        }

        return bankExchangeRate; //return -1.0
    }

    public Quote quoteBuy(double amt, String currencyCode) {
        double rate = getRate(currencyCode);
        Quote quote;

        if(rate == -1.0) {
            quote = null;
        }
        else {
            double userOwes = amt / (rate * (1 - commissionRate)); // This calculates the commission amount based on the dollar amount.
            double commission = userOwes * commissionRate;
            quote = new Quote(bankName, "USD", userOwes, currencyCode, amt, commissionRate, commission);
        }

        return quote;
    }

    public Quote quoteSell(double amt, String currencyCode) {
        double rate = getRate(currencyCode);
        Quote quote;

        if(rate == -1.0) {
            quote = null;
        }
        else {
            double base = amt / rate; // This calculates the dollar value of the foreign currency.
            double commission = commissionRate * base; // This calculates the commission amount based on the dollar amount.
            double amtConverted = base - commission; // The bank takes a cut.
            quote = new Quote(bankName, currencyCode, amt, "USD", amtConverted, commissionRate, commission);
        }

        return quote;
    }
}

货币类

public class Currency {

    // Two private data fields, one to hold the currency code and one to hold the exchange rate.
    // Note that the exchange rate for the same currency could be different for different banks
    private final String currencyCode;
    private final double exchangeRate;

    // A constructor that takes two arguments: the currency code and exchange rate.
    // The constructor should assign the private members from the values of the arguments.
    public Currency(String currencyCode, double currencyExchangeRate) {
        this.currencyCode = currencyCode;
        this.exchangeRate = currencyExchangeRate;

    }

    // An accessor method (getter) for each data field.
    public String getCurrencyCode() {
        return currencyCode;
    }

    public double getExchangeRate() {
        return exchangeRate;
    }
}

报价类

import java.text.DecimalFormat;

public class Quote {

        private String bankName;
        private String codeToBank;
        private double amtToBank;
        private String codeFromBank;
        private double amtFromBank;
        private double commissionRate;
        private double commissionAmt$;
        DecimalFormat decimalFormat;

        public Quote(String bankName, String currencyCode, double amt, String usd, double amtConverted, double commissionRate, double commission) {
            this.bankName = bankName;
            this.codeToBank = currencyCode;
            this.amtToBank = amt;
            this.codeFromBank = usd;
            this.amtFromBank = amtConverted;
            this.commissionRate = commissionRate;
            this.commissionRate = commission;
            this.decimalFormat = new DecimalFormat("0.00");
        }

        public String toString() {
            return (this.bankName + " is willing to offer you " + decimalFormat.format(this.amtFromBank)
                    + " " + this.codeFromBank + " for "
                    + decimalFormat.format(this.amtToBank) + " "
                    + this.codeToBank + ", after collecting a commission of "
                    + decimalFormat.format(this.commissionRate)
                    + " USD (" + Math.round(100 * this.commissionRate) + "%)");
        }

}

1 个答案:

答案 0 :(得分:1)

您的问题是,扫描仪应从System.in中读取,而应从输入文件“ bank1.txt”或“ bank2.txt”中读取。

更改银行中的这一行:

  

扫描仪键盘=新的Scanner(System.in);

对此:

  

扫描仪键盘=新的扫描仪(bankFile);

您的代码将按预期工作。