Java:使用数组创建一个类

时间:2013-06-09 12:41:10

标签: java arrays

我需要创建一个描述汇率的类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设置为所有费率的avergae。

如何使用数组创建此类?

这是我到目前为止所做的:

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;
}

2 个答案:

答案 0 :(得分: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);
}

这是你正在寻找的吗?

答案 1 :(得分: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 );
  }

}