-2

我需要创建一个描述汇率的类 ExchangeRates(货币表示为字符串(例如“USD”)和数字(例如 5.49))。ExhangeRates 必须有一种方法来设置和获取不同货币的汇率。如果 v 是 ExhangeRates 类的对象,则必须可以调用以下方法:

  • v.setRate(”USD”, 5.49):将货币“USD”的汇率设置为 5.49。如果货币“USD”不存在,则必须添加。如果存在,则必须将其更改为新汇率。
  • x = v.getRate(”GBP”):将 x 设置为货币“GBP”的汇率。如果货币不存在,则返回 0。
  • x = v.amountOfRates():将 x 设置为 v 中存在的汇率数量。
  • x = v.averageRate():将 x 设置为所有速率的平均值。

如何使用数组创建此类?

这是我到目前为止所拥有的:

public class ExchangeRates {
private String name;
private float[] rate;

/**
 * Constructor
 */
public ExchangeRates(String currencyName, float exchangeRate) {
    name = currencyName;
    rate = new float[];
}

public void setExhangeRate (String currencyName, float exchangeRate) {
    name = this.currencyName;
    rate = this.exchangeRate;
}
4

2 回答 2

0

一个糟糕的版本:

public class ExchangeRates {

  private String name;
  private String[] names;
  private float[] rates;

  public ExchangeRates( String name ) {
    this.name = name;
    this.names = {};
    this.rates = {};
  }

  public int amountOfRates() {
    return rates.length;
  }

  public float averageRate() {
    float sum = 0;
    for ( float rate : rates )
      sum += rate;
    return sum / rates.length;
  }

  public float getRate( String name ) {
    for ( int i = names.length - 1; i >= 0; i -- )
      if ( names[ i ].equals( name ) )
        return rates[ i ];
    return 0;
  }

  public void setRate( String name, float rate ) {
    for ( int i = names.length - 1; i >= 0; i -- )
      if ( names[ i ].equals( name ) ) {
        rates[ i ] = rate;
        return;
      }
    names = Array.copyOf( names, names.length + 1 );
    rates = Array.copyOf( rates, rates.length + 1 );
    names[ names.length - 1 ] = name;
    rates[ rates.length - 1 ] = rate;
  }

}

第二个坏版本(更好一点。OOP):

public class ExchangeRates {

  private static class Rate {
    private String name;
    private float rate;
    Rate( String name, float rate ) {
      this.name = name;
      this.rate = rate;
    }
  }

  private String name;
  private Rate[] rates;

  public ExchangeRates( String name ) {
    this.name = name;
    this.rates = {};
  }

  public int amountOfRates() {
    return rates.length;
  }

  public float averageRate() {
    float sum = 0;
    for ( Rate rate : rates )
      sum += rate.rate;
    return sum / rates.length;
  }

  public float getRate( String name ) {
    for ( Rate rate : rates )
       if ( rate.name.equals( name ) )
        return rate.rate;
    return 0;
  }

  public void setRate( String name, float rate ) {
    for ( Rate rate : rates )
      if ( rate.name.equals( name ) ) {
        rate.rate = rate;
        return;
      }
    rates = Array.copyOf( rates, rates.length + 1 );
    rates[ rates.length - 1 ] = new Rate( name, rate );
  }

}

你真的应该使用地图。

public class ExchangeRates {

  private String name;
  private Map<String,Float> rates;

  public ExchangeRates( String name ) {
    this.name = name;
    this.rates = new HashMap<String,Float>();
  }

  public int amountOfRates() {
    return rates.size();
  }

  public float averageRate() {
    float sum = 0;
    for ( Float rate : rates.values() )
      sum += rate;
    return sum / rates.size();
  }

  public float getRate( String name ) {
    Float rate = rates.get( name );
    return rate == null ? 0 : rate.floatValue();
  }

  public void setRate( String name, float rate ) {
    rates.put( name, rate );
  }

}
于 2013-06-09T14:37:36.223 回答
0

您似乎希望每种货币都能够将数字(汇率)映射到货币。为什么不在汇率中使用地图?

public class ExchangeRates {
private String name;
private Map<String, Float> rate;

public ExchangeRates(String currencyName) {
    name = currencyName;
    rate = new HashMap<String, Float>();
}

public void setExhangeRate (String currencyName, float exchangeRate) {
    rate.put(currencyNam, exchangeRate)
}

public Float getRate(String currencyName)
{
     return rate.get(currencyName);
}

这是你要找的吗?

于 2013-06-09T13:21:19.510 回答