宠物游戏例题

package model;

import java.util.Date;

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;
    protected boolean isAlive;
      
    public Pet(String name, String sex) {
        super();
        this.name = name;
        this.sex = sex;
        this.age = 1;
        this.createtime = new Date();
        this.happy = 90;
        this.healthy = 100;
        this.hungry = 80;
        this.isAlive = true;
    }
    
    public abstract void eat();
    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;
        }
        long l = System.currentTimeMillis()-createtime.getTime();
        this.setAge((int)(l/1000/5)+1);
    }
    
    public void showInfo(){
        check();
        System.out.println("宠物信息:");
        System.out.println("	名字:"+name+",性别"+sex+",年龄:"+age);
        System.out.println("	开心值:"+happy);
        System.out.println("	健康值:"+healthy);
        System.out.println("	饥饿值:"+hungry);    
    }
    
    
    public boolean isAlive() {
        return isAlive;
    }
    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }
    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 l) {
        this.age = l;
    }
    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;
    }
    
    
}
package 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("你的宠物挂了");
        }
    }
 
    @Override
    public void play() {
        if(this.isAlive){
            check();
            System.out.println(this.getName()+"玩了一个魔方,饥饿度+20,开心值+10");
            this.setHungry(this.getHungry()+20);
            this.setHappy(this.getHappy()+10);
            check();
        }else{
            System.err.println("你的宠物挂了");
        }
        
    }
    
    @Override
    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("你的宠物挂了");
        }
        
    }
}
package test;

import java.util.Scanner;
import model.Pet;
import model.Rabbit;

public class Main {

    public static void main(String[] args){
        /*Pet p = new Rabbit("花花","女");
        p.showInfo();*/
        
        Pet pet = 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();
                String s = scanner.nextLine();
                System.out.println("输入宠物的姓名和性别,用逗号分开");
                String info = scanner.nextLine();
                String[] infos = info.split(",");
                if("1".equals(s)){
                     pet = new Rabbit(infos[0],infos[1]);
                     selectFlag = false;
                }
            }
            printControl();
            String ss =scanner.nextLine();
            if("1".equals(ss)){
                pet.showInfo();
            }else if("2".equals(ss)){
                pet.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---显示信息");
    }
}
原文地址:https://www.cnblogs.com/jgjk/p/7219701.html