有关于面向对象的综合练习——电子宠物练习

package com.hanqi.maya.model;

public abstract class Pet {
	private String name;
	private int age;
	private String sex;
	private int happy;
	private int healthy;
	private int hungry;
	private boolean isalive;
	public Pet() {
		super();
	
	}
	public Pet(String name,  String sex) {
		super();
		this.name = name;
		this.age = 1;
		this.sex = sex;
		this.healthy = 80;
		this.hungry = 80;
		this.happy = 80;
		this.isalive = true;
	}
	public int getHappy() {
		return happy;
	}
	public void setHappy(int happy) {
		this.happy = happy;
	}
	public abstract void eat();
	public abstract void play();
	public abstract void drug();
	public abstract void hello();
	public abstract void bye();
	public boolean checkState() {
		if (this.happy < 0) {
			System.out.println("不开心");
		}
		if (this.healthy <= 0) {
			isalive = false;
		}
		if (this.hungry <= 50 && this.hungry > 0) {
			System.out.println("请喂食");
		} else if (this.hungry <= 0) {
			isalive = false;
			System.out.println("挂了");
		}
		return isalive;
	}

	public void show() {
		System.out.println("宠物名称: " + this.name);
		System.out.println("宠物性别: " + this.sex);
		System.out.println("宠物年龄: " + this.age);
		System.out.println("饥饿度: " + this.hungry);
		System.out.println("开心值: " + this.happy);
		System.out.println("健康值: " + this.healthy);
	}

	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	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 getIsalive() {
		return isalive;
	}
	public void setIsalive(boolean isalive) {
		this.isalive = isalive;
	}


}

  这个程序为电子宠物的父类文件主要来定义电子宠物的基本属性;

package com.hanqi.maya.model;

public class Cat extends Pet {

	
	public Cat() {
		super();
		
	}

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

	@Override
	public void eat() {
		System.out.println("吃了大力丸瞬间感觉不饿了! 饥饿值+15 ,开心值+15 ");
         super.setHungry(getHungry()+15);
         super.setHappy(getHappy()+15);
	}

	@Override
	public void play() {
		System.out.println("铲屎君朕累了!你跪安吧。 开心值+5,健康值+5");
        super.setHappy(getHappy()+1+5);
        super.setHealthy(getHealthy()+5);
	}

	@Override
	public void drug() {
		System.out.println("朕偶感风寒快传御医!  健康值+10,开心值-5");
        super.setHealthy(getHealthy()+10);
        super.setHappy(getHappy()-5);
	}

	@Override
	public void hello() {
		System.out.println("你就是我的铲屎君吗?我叫"+super.getName());

	}

	@Override
	public void bye() {
		System.out.println("我累了你跪安吧2"
				+ "");

	}

}

  

package com.hanqi.maya.model;

public class Dog extends Pet {

	public Dog() {
		super();
		
	}

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

	@Override
	public void eat() {
		System.out.println("吃了大力丸瞬间感觉不饿了! 饥饿值+15 ,开心值+15 ");
         super.setHungry(getHungry()+15);
         super.setHappy(getHappy()+15);
	}

	@Override
	public void play() {
		System.out.println("谢谢主人陪我玩。我好高兴呀! 开心值+5,健康值+5");
        super.setHappy(getHappy()+1+5);
        super.setHealthy(getHealthy()+5);
	}

	@Override
	public void drug() {
		System.out.println("生病了好难受给我吃药吧!  健康值+10,开心值-5");
        super.setHealthy(getHealthy()+10);
        super.setHappy(getHappy()-5);
	}

	@Override
	public void hello() {
		System.out.println("主人您好!我叫"+super.getName());

	}

	@Override
	public void bye() {
		System.out.println("主人再见");

	}

}

 这两个程序为猫和狗的类为电子宠物的子类用于定义猫和狗的详细的基本属性。

package com.hanqi.maya.test6;

import java.util.Scanner;
import java.util.Timer;

import com.hanqi.maya.model.Dog;
import com.hanqi.maya.model.Cat;
import com.hanqi.maya.model.Pet;
import com.hanqi.maya.util.MyTimeTask;

public class Main {
    public static Timer timer; 
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		Pet p = null;
		boolean flag = true;
		System.out.println("请选择你的宠物类型: ");
		System.out.println("1--忠诚的狗, 2--高冷的猫");
		String s = sc.nextLine(); // 获取需要的宠物
		System.out.println("请输入宠物的昵称:");
		String name = sc.nextLine();
		System.out.println("请输入宠物的性别:");
		String sex = sc.nextLine();
		if("1".equals(s)) {
			p=new Dog(name,sex);
			p.hello();
		}
		if("2".equals(s)) {
			p=new Cat(name,sex);
			p.hello();
		}
		 timer = new Timer();
		MyTimeTask task = new MyTimeTask(p);
		timer.schedule(task, 0, 1000);
		while (flag) {
			flag = p.checkState();
			System.out.println("请输入你要进行的操作:");
			System.out.println("0-显示信息, 1-吃饭, 2-吃药, 3-玩耍, 5-退出");
			String opera = sc.nextLine();
			if ("0".equals(opera)) {
				p.show();
				flag = p.checkState();
			} else if ("1".equals(opera)) {
				p.eat();
				flag = p.checkState();
			} else if ("2".equals(opera)) {
				p.drug();
				flag = p.checkState();
			} else if ("3".equals(opera)) {
				p.play();
				flag = p.checkState();
			} else if ("5".equals(opera)) {
				flag = false;
				p.bye();
			   timer.cancel();
			} else {
				System.out.println("请输入正确的操作 !");
			}
		}
		sc.close();
	}
	public static void change() {
		timer.cancel();
		System.exit(0);
	}

	}

  电子宠物的Main方法用来控制电子宠物的各种反应。

package com.hanqi.maya.util;

import java.util.TimerTask;

import com.hanqi.maya.model.Pet;
import com.hanqi.maya.test6.Main;

public class MyTimeTask extends TimerTask {
   private Pet pet;
   public static boolean flag = true;

	public  MyTimeTask() {
		super();
	}

	public MyTimeTask(Pet pet) {
		super();
		this.pet = pet;
	}

	@Override
	public void run() {
		pet.setHungry(pet.getHungry() - 1);
		pet.setAge(pet.getAge() + 1);
		pet.setHappy(pet.getHappy() - 1);
		flag = pet.checkState();
		if (!flag) {
			Main.change();
		}
	}

	public Pet getPet() {
		return pet;
	}

	public void setPet(Pet pet) {
		this.pet = pet;
	}

	public boolean getFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

}

  这段程序为电子宠物的时间线控制程序,为的是使整个电子宠物显得更加合理可以随着时间变化而变化。

 

posted @ 2017-11-30 14:48  军师联盟  阅读(289)  评论(0编辑  收藏  举报