課題1ソースコード

Main.java と Game.java をカレントディレクトリに、その他のクラスは garden ディレクト リに入れてください。 そして、Main クラスを起動して下さい。

Main.java


import java.util.*;
class Main {
    public static void main(String[] arg){
	Game g = new Game(3,new Scanner(System.in));
	g.run();
    }
}

garden/Game.java


package garden 
import java.util.*;
import garden.*; 
public class Game {
    protected Hatake hatake;
    protected void showStatus(){
	System.out.println(hatake);
    }
    protected void showUsage(){
	System.out.println("使い方: コマンド [引数]");
	for(String command : usage.keySet()){
	    System.out.println(command+": "+usage.get(command));
	}
	System.out.println("end : 終了");
    }
    protected class ShowList implements Runnable {
	public ShowList(){}
	public void run(){
	    System.out.println(tane.list());
	}
    }
    protected class Age implements Runnable {
	public Age(){}
	public void run(){
	    hatake.age();
	}
    }
    protected class Put implements Runnable {
	public Put(){}
	public void run(){
	    final int index = sc.nextInt();
	    final String name = sc.next();
	    hatake.put(index,tane.getInstance(name));
	}
    }
    private Map<String, Runnable> menu = new HashMap<String, Runnable>();
    private Map<String, String> usage = new HashMap<String, String>();
    protected void addMenu(String command, Runnable obj, String desc){
	menu.put(command, obj);
	usage.put(command, desc);
    }
    protected Scanner sc;
    protected Tane tane;
    public Game(int n, Scanner sc){
	hatake = new Hatake(n);
	this.sc = sc;
	tane = new Tane();
	addMenu("list", new ShowList(), "作物一覧");
	addMenu("age", new Age(), "一日経過させる");
	addMenu("put", new Put(),"位置 作物名 : 作物を指定の位置に植える");
    }
    public void run(){
	while(true){
	    showStatus();
	    try{
		final String command = sc.next();
		if(command.equals("end")) break;
		final Runnable r = menu.get(command);
		if(r == null){
		    showUsage();
		}else{
		    r.run();
		}
	    }catch(Exception e){
		System.err.println(e);
	    }
	}
    }
}

garden/Hatake.java


package garden;
public class Hatake {
    protected Sakumotsu[] sakumotsu;
    public Hatake(int size){
	sakumotsu = new Sakumotsu[size];
    }
    @Override public String toString(){
	StringBuilder sb = new StringBuilder();
	for(int i=0; i<sakumotsu.length; i++){
	    if(i!=0){
		sb.append(", ");
	    }
	    sb.append(i+": "+sakumotsu[i]);
	}
	return sb.toString();
    }
    public void put(int i, Sakumotsu s) {
	if(sakumotsu[i]!=null){
	    throw new AlreadyUsedException(i+"は既に使用しています");
	}
	sakumotsu[i]=s;
    }
    public void age(){
	for(int i=0; i<sakumotsu.length; i++){
	    if(sakumotsu[i]!=null){
		sakumotsu[i].age();
		if(sakumotsu[i].isOverage()){
		    sakumotsu[i]=null;
		}
	    }
	}
    }
}

garden/AlreadyUsedException.java


package garden;
public class AlreadyUsedException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    public AlreadyUsedException(String m){
	super(m);
    }
}

garden/Sakumotsu.java


package garden;
public abstract class Sakumotsu implements Cloneable {
    private int age;
    private String name;
    private Condition condition;
    public Sakumotsu(String name){
	age=0;
	this.name=name;
	condition = getCondition();
    }
    public void age(){
	age++;
    }
    @Override public String toString(){
	return name+"は"+condition.get(age);
    }
    public boolean isOverage(){
	return condition.isOverage(age);
    }
    abstract public Condition getCondition();
    public Sakumotsu clone() {  
	try {  
	    return (Sakumotsu) super.clone();  
	} catch (CloneNotSupportedException e) {  
	    throw (new InternalError(e.getMessage()));  
	}  
    }  
}

garden/Tane.java


package garden;
import java.util.*;
public class Tane {
    protected Map<String,Sakumotsu> itemList= new HashMap<String, Sakumotsu>();
    public Tane(){
	itemList.put("apple", new Apple());
    }
    public Sakumotsu getInstance(String name){
	return itemList.get(name).clone();
    }
    public Set<String> list(){
	return itemList.keySet();
    }
}

garden/Condition.java


package garden;
interface Condition {
    String get(int age);
    boolean isOverage(int age);
}

garden/Kaju.java


package garden;
class Kaju extends Sakumotsu {
    public Kaju(String name){
	super(name);
    }
    @Override public Condition getCondition(){
	return new KajuCondition();
    }
}

garden/Apple.java


package garden;
class Apple extends Kaju {
    public Apple(){
	super("りんご");
    }
}

garden/KajuCondition.java


package garden;
class KajuCondition implements Condition {
    final String[] condition = new String[]{
	"種","育ってます","花が咲きました","実がなりました","葉が散りました",
	"葉が繁りました","花が咲きました","実がなりました","枯れました"};
    public KajuCondition(){
    }
    @Override public String get(int age){
	return condition[age];
    }
    @Override public boolean isOverage(int age){
	return age>=condition.length;
    }
}