java 宠物商店

来源:互联网 发布:铁路计算坐标数据库 编辑:程序博客网 时间:2024/06/05 02:50
/**
 * 抽象的父类:一旦有继承关系存在,此时new无任何意义
 */
public abstract class Animal {
    private String name;
    private int health;
    private int love;

    public Animal(String name, int health, int love) {
        super();
        this.name = name;
        setHealth(health);
        this.love = love;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        if (health >= 1 && health <= 100) {
            this.health = health;
        } else {
            System.out.println("健康状况应该在1-100之间");
            this.health = 10;
        }
    }

    public int getLove() {
        return love;
    }

    public void setLove(int love) {
        this.love = love;
    }

    public void shout() {
        System.out.println("宠物的自白");
        System.out.println("我的名字叫" + name + "\n我的健康值" + health + "我和主人的亲密程度是"
                + love);
    }

}

//Animal的两个子类

public class Dog extends Animal{
    
    private String breed;
    public Dog(String name, int health, String breed, int love) {
        super(name,health,love);
        setBreed(breed);
    }
    
    public void shout() {
        super.shout();
        System.out.println("我的品种是" + breed);
    }

    public String getBreed() {
        return breed;
    }

    public void setBreed(String breed) {
        this.breed = breed;
    }

}


public class Penguin extends Animal {
    private String sex;

    public Penguin(String name, int health, String sex, int love) {
        super(name, health, love);
        setSex(sex);
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void shout() {
        super.shout();
        System.out.println("我的性别是" + sex);
    }
}


//主人

public class Master {
    
    public void cure(Dog dog) {
        // 如果狗的健康状况不佳
        if (dog.getHealth() < 50) {
            System.out.println("去医院看病");
            System.out.println("打针");
            System.out.println("吃药");
            // 治疗后,狗的健康状况变为60
            dog.setHealth(60);
        }
    }

    public void cure(Penguin penguin) {
        // 如果企鹅的健康状况不佳
        if (penguin.getHealth() < 50) {
            System.out.println("去医院看病");
            System.out.println("吃药");
            System.out.println("疗养");
            // 治疗后,企鹅的健康状况变为70
            penguin.setHealth(70);
        }
    }
}


//宠物商店


import java.io.IOException;
import java.util.Scanner;

public class PetShop {
    public static void main(String[] args) {
        Dog dog=null;
        Penguin penguin=null;
        System.out.println("欢迎来到宠物商店");
        System.out.println("请输入要领养的宠物的名字");
        Scanner input = new Scanner(System.in);
        String name = input.next();
        System.out.println("请输入你要领养的宠物的类型:(1 狗狗 2 企鹅)");
        int type = input.nextInt();
        switch (type) {
        case 1:
            System.out.println("请输入狗的品种(1 聪明的拉布拉多犬 2 酷酷的雪纳瑞)");
            int breed = input.nextInt();
            System.out.println("请输入狗的健康值");
            int health = input.nextInt();
            String s = "";
            if (breed == 1) {
                s = "聪明的拉布拉多犬";
            } else {
                s = "酷酷的雪纳瑞";
            }
            dog = new Dog(name, health, s, 20);
            dog.shout();
            break;
        case 2:
            System.out.println("请输入企鹅的性别(1Q仔 2Q妹)");
            int sex = input.nextInt();
            System.out.println("请输入企鹅的健康值");
            health = input.nextInt();
            s = "";
            if (sex == 1) {
                s = "Q仔";
            } else {
                s = "Q妹";
            }
            penguin = new Penguin(name, health, s, 30);
            penguin.shout();

        }
        //创建主人
        Master master=new Master();
        master.cure(dog);
        dog.shout();
        master.cure(penguin);
        penguin.shout();
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

希望可以关注我一下,我是个初学者,请多多指教!

原创粉丝点击