Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 货币兑换计划_Java - Fatal编程技术网

Java 货币兑换计划

Java 货币兑换计划,java,Java,附件是我的实验室项目的最终代码。我已经完成了所有的工作,但是由于某种原因我的代码处于空闲状态?我认为这可能是“bank1”和“bank2”对象与Bank类之间的问题 很抱歉代码太长,因为我不知道问题出在哪里。请帮忙:( 货币兑换类别(主要) 银行类 import java.io.*; import java.util.*; public class Bank { // Five private data fields: The name of the bank, The commis

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

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

货币兑换类别(主要)

银行类

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) + "%)");
        }

}

货币类别

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) + "%)");
        }

}

报价类

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) + "%)");
        }

}


您的问题是扫描仪正在从System.in读取数据,而此时它应该从输入文件“bank1.txt”或“bank2.txt”读取数据

在银行中更改此行:

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

为此:

扫描仪键盘=新扫描仪(银行文件)


您的代码将按预期工作。

谢谢,现在它没有空闲。但是我得到了一个即时错误…---线程“main”java.util.NoSuchElementException在java.base/java.util.Scanner.throwFor(Scanner.java:937)在java.base/java.util.Scanner.next(Scanner.java:1478)在Bank.(Bank.java:24)在CurrencyExchange.main(CurrencyExchange.java:12)这意味着您的扫描仪需要更多的输入,但没有足够的输入可供读取。这可能是您正在传递银行构造函数的银行文件的内容有问题。一种快速简便的测试方法是在文本文件中添加几行随机数,然后查看程序是否运行。