欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用Python模拟一个手机操作系统——手机本体模块的实现

程序员文章站 2022-06-28 17:11:03
上篇文章我们完成了手机联系人模块的编写这篇文章我们来完善一下手机的本体首先呢 我们要将之前编写的联系人模块导入进来from dorrigo_contact import dorrigo_contact然后我们初始化手机本体手机应有的几个属性最大联系人数量、是否开机、剩余电量、是否联网、手机信号、还有就是默认会自带一个联系人def __init__(self, max_contacts): self.max_contacts = max_contacts self.turn_o...

上篇文章我们完成了手机联系人模块的编写
这篇文章我们来完善一下手机的本体

首先呢 我们要将之前编写的联系人模块导入进来

from dorrigo_contact import dorrigo_contact

然后我们初始化手机本体
手机应有的几个属性
最大联系人数量、是否开机、剩余电量、是否联网、手机信号、还有就是默认会自带一个联系人

def __init__(self, max_contacts):
    self.max_contacts = max_contacts
    self.turn_on = False
    self.battery_life = 25
    self.connected = False
    self.signal_strength = 0
    self.owner_first_name = "Dorrigo"
    self.owner_last_name = "Incorporated"
    self.owner_phone_number = "180076237867"
    self.owner_contect = dorrigo_contact(
        self.owner_first_name,
        self.owner_last_name,
        self.owner_phone_number
    )
    self.owner_contect.messages.append("Dorrigo: Thank you for choosing Dorrigo products")
    self.contacts = [None for i in self.max_contacts]

判断手机是否开机

    def is_phone_on(self):
        return True if self.turn_on else False

手机开关机
on为传入的参数 ,我们准备执行开机还是关机操作

    def set_phone_on(self, on):
        if on:
            if self.battery_life <= 5:
                return False
            else:
                self.battery_life -= 5
                self.turn_on = True
                self.connected = False
                return True
        else:
            self.turn_on = False
            self.connected = False
            return True

获取当前的手机剩余电量

    def get_battery_life(self):
        return self.battery_life if self.turn_on else 0

更换电池

    def change_battery(self, new_battery_level):
        if 0 <= new_battery_level <= 100:
            self.turn_on = False
            self.battery_life = new_battery_level
            return True
        else:
            return False

是否已经联网

    def is_connected_network(self):
        if self.turn_on:
            if self.connected:
                return True
            else:
                return False
        else:
            return False

断开网络连接

    def disconnect_network(self):
        if self.turn_on:
            if not self.connected:
                self.signal_strength = 0
        return True

连接网络

    def connect_network(self):
        if self.turn_on:
            if self.connected:
                return True
            else:
                self.connected = True
                if self.battery_life <= 2:
                    self.battery_life = 0
                    self.turn_on = False
                    return False
                if self.signal_strength == 0:
                    self.signal_strength = 1
                self.battery_life -= 2
                return True
        else:
            return False

获取当前的手机信号

    def get_signal_strength(self):
        if self.turn_on:
            return self.signal_strength if self.connected else 0
        else:
            return 0

更新手机信号

    def set_signal_strength(self, new_strength):
        if self.turn_on:
            if new_strength == 0:
                self.connected = False
                self.signal_strength = new_strength
                return True
            elif 0 < new_strength < 5:
                self.connected = True
                self.signal_strength = new_strength
                return True
            else:
                return False
        else:
            return False

给手机充电,一次10%

    def charge_phone(self):
        if self.battery_life + 10 <= 100:
            self.battery_life += 10
            return True
        else:
            return False

使用手机

    def use_phone(self, k):
        if k:
            if self.battery_life - k > 0:
                self.battery_life -= k
                return True
            else:
                self.battery_life = 0
                self.turn_on = False
                return False
        else:
            return True

复制一个本人的联系人信息

    def get_copy_of_owner_contact(self):
        if self.turn_on:
            owner_copy = dorrigo_contact(
                self.owner_first_name,
                self.owner_last_name,
                self.owner_phone_number
            )
            owner_copy.messages.append("Dorrigo: Thank you for choosing Dorrigo products")
            return owner_copy
        else:
            return None

添加联系人

    def add_contact(self, contact):
        if self.turn_on:
            flag = 0
            while flag < self.max_contacts:
                if self.contacts[flag] == None:
                    flag += 1
                    continue
                if contact.fname == self.contacts[flag].fname \
                        and contact.lname == self.contacts[flag].lname \
                        and contact.pnumber == self.contacts[flag].pnumber:
                    return False
                flag += 1
            flag = 0
            while flag < self.max_contacts:
                if self.contacts[flag] == None:
                    self.contacts[flag] = contact
                    return True
                flag += 1
            return False
        else:
            return False

删除联系人

    def remove_contact(self, contact):
        if self.turn_on:
            flag = 0
            while flag < self.max_contacts:
                if self.contacts[flag] == None:
                    flag += 1
                    continue
                if contact.fname == self.contacts[flag].fname \
                        and contact.lname == self.contacts[flag].lname \
                        and contact.pnumber == self.contacts[flag].pnumber:
                    self.contacts[flag] = None
                    return True
                flag += 1
            return False
        else:
            return False

获取所有联系人的手机号

    def get_number_of_contacts(self):
        if self.turn_on:
            flag = 0
            contacts = []
            while flag < len(self.contacts):
                if self.contacts[flag] == None:
                    flag += 1
                    continue
                contacts.append(self.contacts[flag])
                flag += 1
            return len(contacts)
        else:
            return -1

查找联系人

    def search_contact(self, name):
        if self.turn_on:
            flag = 0
            contacts = []
            while flag < len(self.contacts):
                if self.contacts[flag] is None:
                    flag += 1
                    continue
                if self.contacts[flag].fname == name or self.contacts[flag].lname == name:
                    contacts.append(self.contacts[flag])
                flag += 1
            if contacts:
                return contacts

完整代码如下:

from dorrigo_contact import dorrigo_contact


class dorrigo_mobile:

    def __init__(self, max_contacts):
        self.max_contacts = max_contacts
        self.turn_on = False
        self.battery_life = 25
        self.connected = False
        self.signal_strength = 0
        self.owner_first_name = "Dorrigo"
        self.owner_last_name = "Incorporated"
        self.owner_phone_number = "180076237867"
        self.owner_contect = dorrigo_contact(
            self.owner_first_name,
            self.owner_last_name,
            self.owner_phone_number
        )
        self.owner_contect.messages.append("Dorrigo: Thank you for choosing Dorrigo products")
        self.contacts = [None for i in self.max_contacts]

    def get_copy_of_owner_contact(self):
        if self.turn_on:
            owner_copy = dorrigo_contact(
                self.owner_first_name,
                self.owner_last_name,
                self.owner_phone_number
            )
            owner_copy.messages.append("Dorrigo: Thank you for choosing Dorrigo products")
            return owner_copy
        else:
            return None

    def add_contact(self, contact):
        if self.turn_on:
            flag = 0
            while flag < self.max_contacts:
                if self.contacts[flag] == None:
                    flag += 1
                    continue
                if contact.fname == self.contacts[flag].fname \
                        and contact.lname == self.contacts[flag].lname \
                        and contact.pnumber == self.contacts[flag].pnumber:
                    return False
                flag += 1
            flag = 0
            while flag < self.max_contacts:
                if self.contacts[flag] == None:
                    self.contacts[flag] = contact
                    return True
                flag += 1
            return False
        else:
            return False

    def remove_contact(self, contact):
        if self.turn_on:
            flag = 0
            while flag < self.max_contacts:
                if self.contacts[flag] == None:
                    flag += 1
                    continue
                if contact.fname == self.contacts[flag].fname \
                        and contact.lname == self.contacts[flag].lname \
                        and contact.pnumber == self.contacts[flag].pnumber:
                    self.contacts[flag] = None
                    return True
                flag += 1
            return False
        else:
            return False

    def get_number_of_contacts(self):
        if self.turn_on:
            flag = 0
            contacts = []
            while flag < len(self.contacts):
                if self.contacts[flag] == None:
                    flag += 1
                    continue
                contacts.append(self.contacts[flag])
                flag += 1
            return len(contacts)
        else:
            return -1

    def search_contact(self, name):
        if self.turn_on:
            flag = 0
            contacts = []
            while flag < len(self.contacts):
                if self.contacts[flag] is None:
                    flag += 1
                    continue
                if self.contacts[flag].fname == name or self.contacts[flag].lname == name:
                    contacts.append(self.contacts[flag])
                flag += 1
            if contacts:
                return contacts

    def is_phone_on(self):
        return True if self.turn_on else False

    def set_phone_on(self, on):
        if on:
            if self.battery_life <= 5:
                return False
            else:
                self.battery_life -= 5
                self.turn_on = True
                self.connected = False
                return True
        else:
            self.turn_on = False
            self.connected = False
            return True

    def get_battery_life(self):
        return self.battery_life if self.turn_on else 0

    def change_battery(self, new_battery_level):
        if 0 <= new_battery_level <= 100:
            self.turn_on = False
            self.battery_life = new_battery_level
            return True
        else:
            return False

    def is_connected_network(self):
        if self.turn_on:
            if self.connected:
                return True
            else:
                return False
        else:
            return False

    def disconnect_network(self):
        if self.turn_on:
            if not self.connected:
                self.signal_strength = 0
        return True

    def connect_network(self):
        if self.turn_on:
            if self.connected:
                return True
            else:
                self.connected = True
                if self.battery_life <= 2:
                    self.battery_life = 0
                    self.turn_on = False
                    return False
                if self.signal_strength == 0:
                    self.signal_strength = 1
                self.battery_life -= 2
                return True
        else:
            return False

    def get_signal_strength(self):
        if self.turn_on:
            return self.signal_strength if self.connected else 0
        else:
            return 0

    def set_signal_strength(self, new_strength):
        if self.turn_on:
            if new_strength == 0:
                self.connected = False
                self.signal_strength = new_strength
                return True
            elif 0 < new_strength < 5:
                self.connected = True
                self.signal_strength = new_strength
                return True
            else:
                return False
        else:
            return False

    def charge_phone(self):
        if self.battery_life + 10 <= 100:
            self.battery_life += 10
            return True
        else:
            return False

    def use_phone(self, k):
        if k:
            if self.battery_life - k > 0:
                self.battery_life -= k
                return True
            else:
                self.battery_life = 0
                self.turn_on = False
                return False
        else:
            return True

简易的手机操作系统完成
如果有什么功能想添加,可以评论下面,我后面再去更新
一起学习python,小白指导,教学分享记得私信我

本文地址:https://blog.csdn.net/Miku_wx/article/details/111999156

相关标签: python 编程语言