1. 程式人生 > >電子寵物加強版

電子寵物加強版

boolean control 不能 電子 () pan his info while

1、Pet 父類

package com.hanqi.maya.model;

import java.util.Date;

/**
 * 寵物類
 * 健康值不能小於5
 * 饑餓值5~95
 * 開心值:如果小於30,每次有活動,健康值-2
 * 
 * @author asus
 *
 */
public abstract class Pet {
    private String name;
    private String sex;
    private int age;
    private Date createtime;
    private int happy;
    
private int healthy; private int hungry; private boolean isAlive; public Pet(String name, String sex) { super(); this.name = name; this.sex = sex; this.age = 1; this.createtime = new Date(); //註意Date this.happy = 90; this.healthy = 100;
this.hungry = 80; this.isAlive = true; } public abstract void eat();//含有抽象方法的類必須聲明為抽象類,所以類名那裏也要加上abstract public abstract void play(); public abstract void hospital(); public void check(){ if(healthy < 5 || hungry < 5 || hungry > 95){ isAlive
= false; } if(happy < 30){ healthy -= 2; } if(healthy > 100){ healthy = 100; } if(happy > 100){ happy = 100; } this.setAge((int)((System.currentTimeMillis() - createtime.getTime()) /1000 /5) + 1); } public void showInfo(){ check(); System.out.println("寵物信息:"); System.out.println("\t名字: "+name+",性別: "+sex+",年齡: "+age);// \t轉義制表符,相當於Tab鍵 System.out.println("\t開心值: "+ happy); System.out.println("\t健康值: "+ healthy); System.out.println("\t饑餓值: "+ hungry); } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getCreatetime() { return createtime; } public void setCreatetime(Date createtime) { this.createtime = createtime; } public int getHappy() { return happy; } public void setHappy(int happy) { this.happy = happy; } public int getHealthy() { return healthy; } public void setHealthy(int healthy) { this.healthy = healthy; } public int getHungry() { return hungry; } public void setHungry(int hungry) { this.hungry = hungry; } public boolean isAlive() { return isAlive; } public void setAlive(boolean isAlive) { this.isAlive = isAlive; } }

2、Rabbit 子類

package com.hanqi.maya.model;

public class Rabbit extends Pet {

    public Rabbit(String name, String sex) {
        super(name, sex);
    }

    @Override
    public void eat() {
        if(this.isAlive()){
            check();
            System.out.println(this.getName() + "拿到一根蘿蔔在吃,饑餓值-20");
            this.setHungry(this.getHungry() - 20);
            check();
        }else{
            System.err.println("你的寵物掛了");
        }    
    }

    public void play() {
        if(this.isAlive()){
            check();
            System.out.println(this.getName() + "玩了一個魔方,饑餓值+5,開心值+10");
            this.setHungry(this.getHungry() + 5);
            this.setHappy(this.getHappy() + 10);
            check();
        }else{
            System.err.println("你的寵物掛了");
        }    
        
    }

    public void hospital() {
        if(this.isAlive()){
            check();
            System.out.println(this.getName() + "在醫院裏哇哇叫,饑餓值+20,健康值+50");
            this.setHungry(this.getHungry() + 20);
            this.setHealthy(this.getHealthy() + 50);
            check();
        }else{
            System.err.println("你的寵物掛了");
        }    
        
    }
    
}

3、Main方法

package com.hanqi.maya.text;

import java.util.Scanner;

import com.hanqi.maya.model.Pet;
import com.hanqi.maya.model.Rabbit;

public class Main {
    public static void main(String[] args) {
        //Pet p = new Rabbit("旺財", "男");
        //p.showInfo();
        Pet p = null;
        
        Scanner scanner = new Scanner (System.in);
        
        boolean selectFlag = true;
        boolean circleFlag = true;
        /*
        System.out.println("請輸入寵物類型");
        System.out.println("1--兔子");
        */
        while(circleFlag){
            if(selectFlag){
                String s = scanner.nextLine();
                System.out.println("輸入寵物的姓名和性別,用逗號分開");
                String info = scanner.nextLine();
                String[] infos = info.split(",");
                if("1".equals(s)){
                    p = new Rabbit(infos[0],infos[1]);
                    selectFlag = false;
                }
            }
            printControl();
            String ss = scanner.nextLine();
            if("1".equals(ss)){
                p.showInfo();
            } else if("2".equals(ss)){
                p.eat();
            } else if("3".equals(ss)){
                
            } else if("4".equals(ss)){
                
            } else if("5".equals(ss)){
                System.out.println("再見");
                circleFlag = false;
            } else{
                System.out.println("無此操作");
            }
        }
        scanner.close();
    }
    private static void printControl(){
        System.out.println("輸入想進行的操作");
        System.out.println("1---顯示信息");
        System.out.println("2---吃飯");
        System.out.println("3---玩遊戲");
        System.out.println("4---去醫院");
        System.out.println("5---退出");
    }
}

電子寵物加強版