RPGツクールと数学のブログ

RPGツクールと数学についてのブログです。

Javaによるオブジェクト指向講座 おまけ

第3回

f:id:fermiumbay13:20190802021648p:plain

第2回で提示したレストランのやりとりをオブジェクト指向として実装した例です。

登場人物は、お客さん(Customer)、接客(Counter)、料理人(Cook)の3人で、
それぞれBaseCharaを継承して作られます。料理の情報はFoodInfoクラスにまとめられています。実際たかがこれだけの処理にこんなに書く必要ないと思いますけど、
例ということでご了承ください^^;

また、Mainクラスで定義されているshowCharaInfoメソッドをてきとうな所に挿入してもらえば、その段階での各キャラの情報(所持金、持っている食べ物)を表示できます。(Main以外のクラスだと、Main.showCharaInfo();で呼び出せます)

処理の流れを見るのにぜひ活用してください。

// 【Main.java

// メインクラス
class Main{
    
    // 情報取得用のキャラクターリスト
    public static BaseChara[] charaList = new BaseChara[3];
    
    // キャラクターリストに載っているすべてのキャラクターの情報を表示
    public static void showCharaInfo(){
        for(int i = 0; i < charaList.length; i++){
            charaList[i].showInfo();
        }
    }
    
    // mainメソッド
    public static void main(String args[
]){

        // (食べ物リスト)
        // 0番: オムライス(税込972円)
        // 1番: とんかつ(税込1296円)
        // 2番: たこ焼き(税込648円)
        
        // お客さんの名前はベイ助、所持金1000円、食べたいものは0番オムライス
        Customer customer = new Customer("ベイ助", 1000, 0);

        // 接客の名前はレーラ、所持金2万円
        Counter counter = new Counter("レーラ", 20000);

        // 料理人の名前はポン太、所持金なし
        Cook cook = new Cook("ポン太", 0);

        // showCharaInfoメソッドのためにそれぞれのキャラを代入しておく        
        charaList[0] = customer;
        charaList[1] = counter;
        charaList[2] = cook;
        
        System.out.println(customer.getName() + "は、レストランへ行きました。");

        // お客さんが接客に注文 成功したら後の手続きを行う
        if(customer.order(counter)){
            
            // 接客が料理人に料理を作るよう依頼し、もらう
            counter.askToCook(cook);
            
            // 接客がお客さんに料理を提供
            counter.provideDish(customer);
            
            // お客さんがそれを食べる
            customer.eat();
        }
        
        System.out.println(customer.getName() + "はレストランを後にしました。おわり");
        
    }
}

// 【BaseChara.java】

// 各キャラクターのスーパークラス
abstract class BaseChara{
    
    // 名前
    private String name;
    
    // 所持金
    private int money;
    
    // 持っている食べ物ID(-1で何も持っていない)
    protected int foodId;
    
    public BaseChara(String name, int money){
        this.name = name;
        this.money = money;
        this.foodId = -1;
        this.sayHello();
    }
    public String getName(){
        return this.name;
    }
    public int getMoney(){
        return this.money;
    }
    public int getFoodId(){
        return this.foodId;
    }
    
    // あいさつ
    abstract public void sayHello();
    
    // 情報を表示
    public void showInfo(){
        System.out.println("【" + this.getName() + "の情報】");
        System.out.println("所持金:" + this.getMoney());
        if(this.getFoodId() != -1){
            System.out.println("持っている食べ物:" + FoodInfo.getFoodName(this.getFoodId()));
        }else{
            System.out.println("食べ物は何も持っていない……");
        }
    }
    
    // しゃべらせる
    protected void speak(String comment){
        System.out.println(this.getName() + "「" + comment + "」");
    }
    
    // お金をcharaに払う
    public void payMoney(int money, BaseChara chara){
        chara.receiveMoney(money);
        this.money -= money;
    }
    
    // お金をもらう
    private void receiveMoney(int money){
        this.money += money;
    }
    
    // 持っている食べ物をcharaに渡す
    public void giveFood(BaseChara chara){
        chara.receiveFood(this.foodId);
        this.foodId = -1;
    }
    
    // 食べ物をもらう
    private void receiveFood(int foodId){
        this.foodId = foodId;
    }
    
}

// 【Customer.java】

// お客さん
class Customer extends BaseChara{
    
    // 欲しい食べ物
    private int hopeFoodId;
    
    public Customer(String name, int money, int hopeFoodId){
        super(name, money);
        this.hopeFoodId = hopeFoodId;
    }
    public void sayHello(){
        this.speak("お客さんですっ!");
    }
    
    // 欲しい食べ物を注文する
    public Boolean order(Counter counter){
        Boolean orderSucceeded = false;
        this.speak(FoodInfo.getFoodName(this.hopeFoodId) + "食べようかな。お願いします!");
        if(this.getMoney() >= FoodInfo.getFoodPrice(this.hopeFoodId)){
            this.payMoney(FoodInfo.getFoodPrice(this.hopeFoodId), counter);
            counter.ordered(this.hopeFoodId);
            orderSucceeded = true;
        }else{
            this.speak("ああ、お金が足りなくて注文できないや……");
        }
        return orderSucceeded;
    }
    
    // 持っている食べ物を食べる
    public void eat(){
        this.speak("いただきまーす!");
        this.speak("やっぱおいしいねぇ、ごちそうさま!");
        this.foodId = -1;
    }
    
}

// 【Counter.java】

// 接客
class Counter extends BaseChara{
    
    // 注文もらった食べ物のID
    private int orderedFoodId;
    
    public Counter(String name, int money){
        super(name, money);
    }
    public void sayHello(){
        this.speak("接客しまーす!");
    }
    
    // 注文を受ける
    public void ordered(int orderedFoodId){
        this.speak("かしこまりました! 少々お待ちください!");
        this.orderedFoodId = orderedFoodId;
    }
    
    // 注文された料理を料理人に作ってもらう
    public void askToCook(Cook cook){
        this.speak(FoodInfo.getFoodName(orderedFoodId) + "の注文入りましたよ!");
        cook.cook(this, orderedFoodId);
    }
    
    // 料理を提供する
    public void provideDish(Customer customer){
        this.speak("おまちどおさまでした! " + FoodInfo.getFoodName(this.getFoodId()) + "です!");
        this.giveFood(customer);
    }
    
}

// 【Cook.java】

// 料理人
class Cook extends BaseChara{
    
    public Cook(String name, int money){
        super(name, money);
    }
    public void sayHello(){
        this.speak("料理作ってやるよ!");
    }
    
    // 料理する
    public void cook(Counter counter, int cookFoodId){
        this.speak("よーし、いま作るから待ってな");
        System.out.println("5分後...");
        this.foodId = cookFoodId;
        this.speak("できた! それじゃ、持ってってくれよ");
        this.giveFood(counter);
    }
    
}

// 【FoodInfo.java】

// 食べ物の情報を持ったクラス
class FoodInfo{
    
    // 消費税率
    private static final double taxRate = 0.08;
    
    // 食べ物の名前一覧
    private static String[] foodName = {"オムライス", "とんかつ", "たこ焼き"};
    
    // 食べ物の税抜き価格一覧
    private static int[
] foodPrice = {900, 1200, 600};
    
    public static String getFoodName(int foodId){
        String foodName = "";
        if(foodId >= 0 && foodId < FoodInfo.foodName.length){
            foodName = FoodInfo.foodName[foodId];
        }
        else{
            foodName = "(未定義)";
        }
        return foodName;
    }
    
    public static int getFoodPrice(int foodId){
        int foodPrice = 0;
        if(foodId >= 0 && foodId < FoodInfo.foodPrice.length){
            foodPrice = addTax(FoodInfo.foodPrice[foodId]);
        }
        return foodPrice;
    }

    // 税込み価格にする
    public static int addTax(int price){
        return (int)((1.0 + taxRate) * price);
    }
    
}