Newer
Older
ASS2021Java / src / Onigiri.java
@Motoki Miura Motoki Miura on 5 Oct 2021 2 KB init
import java.util.ArrayList;

public class Onigiri {
    static int count = 0; //つくったおにぎりの数をカウントする
    static void printCount(){
        System.out.println(count + "個のおにぎりをつくりました");
    }
    static ArrayList<Onigiri> all;
    static {
        all = new ArrayList<Onigiri>();
    }

    public static void main(String[] args) {
        Onigiri ume = new Onigiri("梅", true);
        System.out.println("ume = " + ume.toString());
        System.out.println(count + "個のおにぎりをつくりました");

        System.out.println( new YakiOnigiri().toString() );
    }

    // フィールド/メンバ=データの詳細
    String gu; /* おにぎりの具名 */
    boolean withNori; /* 海苔がついているか? true / false */
    int shape; /* 0:丸, 1:俵型, 3:三角形 */

    // コンストラクタ=データ製造機
    public Onigiri() {
        gu = "無し(塩むすび)";
        withNori = false;
        shape = 3;
        Onigiri.count++;
        Onigiri.all.add(this);
    }

    public Onigiri(String _gu) {
        this();
        gu = _gu;
    }

    public Onigiri(String gu, int shape) {
        this(gu);
        this.shape = shape;
    }

    public Onigiri(String gu, boolean wnori) {
        this(gu);
        withNori = wnori;
    }

    public Onigiri(String gu, boolean wnori, int shape) {
        this(gu, wnori);
        this.shape = shape;
    }

    // メソッドの役割:(1) データの修正・更新をする (2) データに関する情報を出力 (3) データに関する処理
    public void setWithNori(boolean wnori) {
        withNori = wnori;
    }

    public String toString() {
        String str = "具は " + gu + " で、";
        switch (shape) {
            case 0:
                str = str + "丸型で、";
                break;
            case 1:
                str = str + "俵型で、";
                break;
            case 3:
                str = str + "三角形で、";
                break;
        }
        str = str + (withNori ? "海苔付き" : "海苔なし");
        return str;
    }
}