課題1 プログラム

Money.java


abstract public class Money {
    private double value;
    protected Money(double value){
	this.value = value;
    }
    abstract protected String getPrefix();
    abstract protected String getPostfix();
    @Override public String toString(){
	return getPrefix()+String.valueOf(value)+getPostfix();
    }
    abstract public double getYenRate();
    public double getValue(){
	return value*getYenRate();
    }
    @Override public boolean equals(Object obj){
	if(this == obj) return true;
	if(!(obj instanceof Money)) return false;
	Money m = (Money) obj;
	return this.getValue() == m.getValue();
    }
    @Override public int hashCode(){
	long v = Double.doubleToLongBits(getValue());
	return (int)(v^(v>>>32));
    }
}

MoneyCollection.java


import java.util.*;
public class MoneyCollection extends ArrayList<Money> {
    public MoneyCollection(){
	super();
    }
    public void show(){
	for(Money m : this){
	    System.out.println(m);
	}
    }
}

Yen.java


public class Yen extends Money {
    public Yen(double value){
	super(value);
    }
    @Override protected String getPrefix(){
	return "";
    }
    @Override protected String getPostfix(){
	return "円";
    }
    @Override public double getYenRate(){
	return 1.0;
    }
}