练习2:宠物管理系统

<script>

        // 定义一个宠物类

        class Pet{

            constructor(name, type, gender, health, love, master = null){

                // 昵称

                this.name = name

                // 种类

                this.type = type

                // 性别

                this.gender = gender

                // 健康值

                this.health = health

                // 亲密度

                this.love = love

                // 主人(对象)

                this.master = master

            }

            // 介绍宠物方法

            sayHi(){

                console.log(`我是一只${this.type},我叫${this.name},我是${this.gender === 1? '男生':'女生'},我的主人是${this.master.username}。

健康值:${this.health}  亲密度:${this.love}`);

            }

            // 宠物吃方法

            eat(){

                if(this.health < 100){

                    console.log(`我正在吃东西~~`);

                    console.log(`健康值:${this.health} => ${this.health += 5}`);

                    console.log(`真不错,好好吃呀~~`);

                }else{

                    console.log(`芜湖,我已经吃饱了,主人,我们去玩吧~~`);

                    console.log(`健康值:${this.health}`);

                    console.log(`亲密度:${this.love}`)

                }

            }

            // 宠物玩方法

            play(){

                if(this.health > 30){

                    console.log(`我正在做游戏,做呀做游戏~~鬼刀一开看不见,走位走位~~`);

                    console.log(`亲密度:${this.love} => ${this.love += 3}`);

                    console.log(`健康值:${this.health} => ${this.health -= 5}`);

                    console.log(`真不错,我好开心啊~~`);

                }else{

                    console.log(`好累呀,我饿了,我要吃好吃的~~`);

                    console.log(`健康值:${this.health}`);

                    console.log(`亲密度:${this.love}`)

                }

            }

        }

        // 定义一个用户类

        class User{

            constructor(username, loginid, password, pet=null){

                // 昵称

                this.username = username

                // 登录id

                this.loginid = loginid

                // 密码

                this.password = password

                // 宠物

                this.pet = pet

            }

        }

        // 定义一个宠物管理对象

        let petManager = {

            // 创建一个宠物信息数组

            pets: [

                new Pet('油条', '英短', 0, 100, 100),

                new Pet('豆浆', '边牧', 1, 100, 100),

                new Pet('摩登', '拉布拉多犬', 1, 100, 100),

                new Pet('黑珍珠', '藏獒', 1, 100, 100),

                new Pet('佩奇', '宠物猪', 0, 100, 100)

            ],

            // 创建一个用户信息数组

            users: [

                new User('玥歆','153124','123456'),

                new User('阳仔','107625','123456'),

                new User('张三','112233','123456'),

                new User('李四','223344','123456')

            ],

            // 当前登录用户属性

            currentUser: null,

            // 查看宠物方法

            showPet(){

                console.log('*****************查看宠物*****************');

                console.log('昵称\t类型\t性别\t健康值\t亲密度\t主人');

                this.pets.forEach((p, index) => console.log(`${p.name}\t${p.type}\t${p.gender === 1 ? '男生' : '女生'}\t${p.health}\t    ${p.love}\t    ${p.master === null? '暂无' : p.master.username}`))

            },

            // 领养宠物方法

            adoptPet(){

                console.log(`*****************领养宠物*****************`);

                // 定义一个待领养的宠物对象

                let pet = null

                // some()方法,用于表示数组中是否有满足指定规则的元素,有返回true,一个都没有返回false

                // 先判断当前登录用户是否已经领养过宠物

                if(this.pets.some(p => p.master === this.currentUser)){

                    // 获取当前用户领养的那只宠物

                    pet = this.pets.find(p => p.master === this.currentUser)

                    alert(`您已经领养了${pet.name}!不可一心二用哦~~`)

                    console.log(`您已经领养了${pet.name}!`)

                }else{

                    while(true){

                        let petName = prompt('请输入需要领养的宠物昵称:')

                        // 根据昵称找到该宠物对象

                        pet = this.pets.find(p => p.name === petName)

                        if(!pet){

                            alert(`${petName}不存在!请重新输入`)

                            console.log(`${petName}不存在!`)

                        }else{

                            //

                            if(pet.master === null){

                                // 设置该宠物的主人是当前登录用户

                                pet.master = this.currentUser

                                this.currentUser.pet = pet

                                alert(`您已成功领养了${petName},宠物是人类的朋友,请君善待它!`)

                                console.log(`您已成功领养了${petName}`)

                                break

                            }else{

                                alert(`${petName}已经有主人了,请君切勿夺人所爱!`)

                                console.log(`${petName}已经有主人了`)

                            }

                        }

                    }

                }

                console.log(pet.name);

                // 调用宠物菜单方法,注意:该方法需要传一个宠物对象作为参数。

                this.petMenu()

            },

            // 宠物的菜单方法

            petMenu(pet=this.currentUser.pet){

                while(true){

                    console.log('*****************宠物菜单*****************');

                    let no = parseInt(prompt(`宠物技能:\n1.打个招呼  2.喂食  3.溜一圈  4.退出`))

                    switch (no) {

                        case 1:

                            pet.sayHi()

                            break;

                        case 2:

                            pet.eat()

                            break;

                        case 3:

                            pet.play()

                            break;

                        default:

                            alert(`期待下次您与宠物的快乐时光~~`)

                            return

                    }

                }

            },

            // 业务菜单

            menu(){

                console.log(`*****************欢迎使用宠物管理系统*****************`);

                let no = parseInt(prompt('1.查看宠物  2.领养宠物  3.培养宠物  4.退出系统'))

                switch (no) {

                    case 1:

                        this.showPet()

                        break;

                    case 2:

                        this.adoptPet()

                        break;

                    case 3:

                        this.petMenu()

                        break;

                    default:

                        alert(`您已成功退出!`)

                        return

                }

                arguments.callee.call(this)

            },

            // 注册方法

            register(){

                console.log(`*****************用户注册*****************`);

                while(true){

                    let username = prompt('请输入您的昵称:')

                    if(!username){

                        alert('昵称不能为空!')

                    }else{

                        let loginid = prompt('请输入登录ID:')

                        if(!loginid){

                            alert('登录ID不能为空!')

                        }else{

                            if(this.users.find(u => u.loginid === loginid)){

                                alert('登录ID重复!请重新输入!')

                            }else{

                                let password = prompt('请输入密码:')

                                // 创建一个新的用户对象

                                if(password.length < 5){

                                    alert('密码长度不得小于5位!')

                                }else{

                                    let user = new User(username, loginid, password)

                                    this.users.push(user)

                                    alert('恭喜您已注册成功!')

                                    console.log(`${loginid}注册成功!`)

                                    return

                                }

                            }

                        }

                    }

                }

                arguments.callee.call(this)

            },

            // 登录方法

            login(){

                console.log('*****************用户登录*****************');

                while(true){

                    let loginid = prompt('请输入登录ID:')

                    let password = prompt('请输入密码:')

                    // 根据登录名和密码,查询一个用户对象

                    let user = this.users.find(u => u.loginid === loginid && u.password === password)

                    // 判断是否登录成功

                    if(user){

                        // 当前用户的属性值变为user

                        this.currentUser = user

                        alert(`用户${user.username},登录成功!`)

                        if(user.pet !== null){

                            alert(`您家的${user.pet.name}表示强烈的欢迎`)

                        }

                        console.log(`当前用户:${this.currentUser.username}--${this.currentUser.loginid},宠物:${this.currentUser.pet === null? '暂无': this.currentUser.pet.name}`);

                        // 打开业务菜单

                        this.menu()

                        return

                    }else{

                        alert('登录失败!登录ID或者密码错误!')

                        console.log(`${loginid}登录失败!登录ID或者密码错误!`)

                    }

                }

                arguments.callee.call(this)

            },

            // 主菜单方法

            sysMenu(){

                let no = parseInt(prompt(`************************欢迎使用宠物管理系统************************

                                     1.登录  2.注册  3.退出`))

                switch (no) {

                    case 1:

                        this.login()

                        break;

                    case 2:

                        this.register()

                        break;

                    default:

                        alert(`您已成功退出,欢迎下次使用!`)

                        return

                }

                arguments.callee.call(this)

            }

        }

        // 调用系统菜单方法

        petManager.sysMenu()

</script>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容