Product information management system (Python) full version

Table of contents

functional module:

Implementation ideas:

Run the functional demo:

Specific implementation process:

Define product categories:

Define product management class

Define a function to display product information

Enter the sales function:

Add product information:

Delete product information

Modify product information

Import product information

Export product information

Find the average sales volume of a product

Find the maximum sales volume of a product

Find the minimum sales volume of a product

Finally define the menu function and main function:

Complete code:


functional module:

Basic product information management and product sales statistics.

The main functions of the basic information management module include adding, deleting, modifying, displaying basic product information and importing and exporting data.

The main functions of the product sales statistics management module are to count the highest sales volume, lowest sales volume and average sales volume of the product.

Implementation ideas:

  1. Design a product category that includes data members such as product number, name, first quarter sales, second quarter sales, and third quarter sales.
  2. Introducing the OS module for importing and exporting path file information
  3. Design a product management class to define the specific functions of each module.
  4. Design a main menu and two submenus to manage basic product information and sales information respectively.

Run the functional demo:

After the product information management system is started, it first enters the main interface of the system and waits for the user to enter commands to select the corresponding functions.

If the user enters the "info" command, the product basic information management sub-functional module is entered.

In the basic product information management interface, users can add, delete, modify, display and other operations of basic product information by entering corresponding commands.

Add product information

Delete product information

Modify product information

 Show product information

If the user enters the "sale" command, the product sales management sub-functional module is entered.

In the product sales management interface, users can select corresponding functions for sales processing.

Average sales volume:

Highest sales volume:

Minimum sales volume:

Specific implementation process:

Define product categories:

class Goods:
    def __init__(self,no,name,first=0,second=0,third=0):
        self.no = no
        self.name = name
        self.first = int(first)
        self.second = int(second)
        self.third = int(third)     

Define product management class

class GoodsList:
    def __init__(self):
        self.gdslist = []

Define a function to display product information

Define the display product information function below the product management class:

  def show(self):
        #显示信息
        print('{}\t{}\t{}\t{}\t{}'
              .format('编号','名称','一季度','二季度','三季度'))
        for gd in self.gdslist:            
            print('{}\t{}\t{}\t{}\t{}'
              .format(gd.no,gd.name,gd.first,gd.second,gd.third))

Enter the sales function:

  def __enterSale(self,message):
        #销量输入
        while True:
            try:
                sale = input(message)
                if 0 <= int(sale) <= 10000:
                    break
                else:
                    print("输入错误,应在0到10000之间")
            except:
                print("输入错误,应在0到10000之间")
        return sale 
    def __exists(self,no):
        #判断编号是否存在
        for gd in self.gdslist:
            if gd.no == no:
                return True
        else:
            return False

Add product information:

def insert(self):
        #添加信息
        while True:
            no = input('编号:')
            if self.__exists(no):
                print('该编号已存在')
            else:
                name = input('名称:')
                first = self.__enterSale('一季度:')
                second = self.__enterSale('二季度:')
                third = self.__enterSale('三季度:')
                gd = Goods(no,name,first,second,third)
                self.gdslist.append(gd)
            choice = input('继续添加(y/n)?').lower()
            if choice == 'n':
                break

Delete product information

   def delete(self):
        #删除学生信息
        while True:
            no = input('请输入要删除的学生学号:')                
            for stu in self.stulist[::]:
                if stu.no == no:
                    self.stulist.remove(stu)
                    print('删除成功')
                    break
            else:
                print('该学号不存在')
            choice = input('继续删除(y/n)?').lower()
            if choice == 'n':
                break

Modify product information

 def delete(self):
        #删除信息
        while True:
            no = input('请输入要删除的商品编号:')                
            for gd in self.gdslist[::]:
                if gd.no == no:
                    self.gdslist.remove(gd)
                    print('删除成功')
                    break
            else:
                print('该编号不存在')
            choice = input('继续删除(y/n)?').lower()
            if choice == 'n':
                break

Import product information

   def load(self,fn):
        #导入信息
        if os.path.exists(fn):
            try:
                with open(fn,'r',encoding = 'utf-8') as fp:
                    while True:
                        fs = fp.readline().strip('\n')
                        if not fs:
                            break
                        else:
                            gd = Goods(*fs.split(','))
                            if self.__exists(gd.no):
                                print('该编号已存在')
                            else:
                                self.gdslist.append(gd)
                print('导入完毕')
            except:
                print('error...')
        else:
            print('要导入的文件不存在')
        

Export product information

    def save(self,fn):
        #导出信息
        with open(fn,'w',encoding = 'utf-8') as fp:
            for gd in self.gdslist:
                fp.write(gd.no + ',')
                fp.write(gd.name + ',')
                fp.write(str(gd.first) + ',')
                fp.write(str(gd.second) + ',')                    
                fp.write(str(gd.third) + '\n')                    
            print("导出完毕")

Find the average sales volume of a product

   def sale_avg(self):
        #求平均销量
        length = len(self.gdslist)
        if length > 0:
            first_avg = sum([gd.first for gd in self.gdslist])/length
            second_avg = sum([gd.second for gd in self.gdslist])/length
            third_avg = sum([gd.third for gd in self.gdslist])/length
            print('一季度平均销量是:%.2f'%first_avg)
            print('二季度平均销量是:%.2f'%second_avg)
            print('三季度平均销量是:%.2f'%third_avg)
        else:
            print('尚没有商品销量...')

Find the maximum sales volume of a product

   def sale_max(self):
        #求最高
        if len(self.gdslist) > 0:
            first_max = max([gd.first for gd in self.gdslist])
            second_max = max([gd.second for gd in self.gdslist])
            third_max = max([gd.third for gd in self.gdslist])
            print('一季度最高销量是:%d'%first_max)
            print('二季度最高销量是:%d'%second_max)
            print('三季度最高销量是:%d'%third_max)
        else:
            print('尚没有商品销量...')

Find the minimum sales volume of a product

    def sale_min(self):
        #求最低
        if len(self.gdslist) > 0:
            first_min = min([gd.first for gd in self.gdslist])
            second_min = min([gd.second for gd in self.gdslist])
            third_min = min([gd.third for gd in self.gdslist])
            print('一季度最低销量是:%d'%first_min)
            print('二季度最低销量是:%d'%second_min)
            print('三季度最低销量是:%d'%third_min)
        else:
            print('尚没有商品销量...')

Finally define the menu function and main function:

def infoprocess(self):
        #基本信息管理
        print('商品基本信息管理'.center(20,'-'))
        print('load----------导入商品信息')
        print('insert--------添加商品信息')
        print('delete--------删除商品信息')
        print('update--------修改商品信息')
        print('show----------显示商品信息')
        print('save----------导出商品信息')
        print('return--------返回')
        print('-'*28)
        while True:
            s = input('info>').strip().lower()
            if s == 'load':
                fn = input('请输入要导入的文件名:')
                self.load(fn)
            elif s == 'show':
                self.show()
            elif s == 'insert':
                self.insert()
            elif s == 'delete':
                self.delete()
            elif s == 'update':
                self.update()
            elif s == 'save':
                fn = input('请输入要导出的文件名:')
                self.save(fn)
            elif s =='return':
                break
            else:
                print('输入错误')
    
    def saleprocess(self):
        #销量统计
        print('商品销量统计'.center(24,'='))
        print('avg    --------平均销量')
        print('max    --------最高销量')
        print('min    --------最低销量')        
        print('return --------返回')
        print(''.center(30,'='))
        while True:
            s = input('sale>').strip().lower()
            if s == 'avg':                
                self.sale_avg()
            elif s == 'max':                
                self.sale_max()
            elif s == 'min':                
                self.sale_min()
            elif s == 'return':
                break
            else:
                print('输入错误')
                
    def main(self):
        #主控函数               
        while True:
            print('商品信息管理系统V1.0'.center(24,'='))
            print('info  --------商品基本信息管理')
            print('sale  --------商品销量统计')
            print('exit  --------退出系统')
            print(''.center(32,'='))
            s = input('main>').strip().lower()
            if s == 'info':
                self.infoprocess()
            elif s == 'sale':
                self.saleprocess()
            elif s == 'exit':
                break
            else:
                print('输入错误')   
        

if __name__ == '__main__':
    gds = GoodsList()
    gds.main()

Complete code:

'''
导入导出文件的编码是utf-8
'''
import os

class Goods:
    def __init__(self,no,name,first=0,second=0,third=0):
        self.no = no
        self.name = name
        self.first = int(first)
        self.second = int(second)
        self.third = int(third)        
    
class GoodsList:
    def __init__(self):
        self.gdslist = []

    def show(self):
        #显示信息
        print('{}\t{}\t{}\t{}\t{}'
              .format('编号','名称','一季度','二季度','三季度'))
        for gd in self.gdslist:            
            print('{}\t{}\t{}\t{}\t{}'
              .format(gd.no,gd.name,gd.first,gd.second,gd.third))
            
    def __enterSale(self,message):
        #销量输入
        while True:
            try:
                sale = input(message)
                if 0 <= int(sale) <= 10000:
                    break
                else:
                    print("输入错误,应在0到10000之间")
            except:
                print("输入错误,应在0到10000之间")
        return sale 

    def __exists(self,no):
        #判断编号是否存在
        for gd in self.gdslist:
            if gd.no == no:
                return True
        else:
            return False
        
    def insert(self):
        #添加信息
        while True:
            no = input('编号:')
            if self.__exists(no):
                print('该编号已存在')
            else:
                name = input('名称:')
                first = self.__enterSale('一季度:')
                second = self.__enterSale('二季度:')
                third = self.__enterSale('三季度:')
                gd = Goods(no,name,first,second,third)
                self.gdslist.append(gd)
            choice = input('继续添加(y/n)?').lower()
            if choice == 'n':
                break


    def delete(self):
        #删除信息
        while True:
            no = input('请输入要删除的商品编号:')                
            for gd in self.gdslist[::]:
                if gd.no == no:
                    self.gdslist.remove(gd)
                    print('删除成功')
                    break
            else:
                print('该编号不存在')
            choice = input('继续删除(y/n)?').lower()
            if choice == 'n':
                break


    def update(self):
        #修改信息
        while True:
            no = input('请输入要修改的商品编号:')
            if self.__exists(no):
                for gd in self.gdslist:
                    if gd.no == no:
                        gd.name = input('名称:')
                        gd.salary = int(self.__enterSale('一季度:'))
                        gd.bonus = int(self.__enterSale('二季度:'))
                        gd.tax = int(self.__enterSale('三季度:'))
                        print('修改成功')
                        break
            else:
                print('该编号不存在')
            choice = input('继续修改(y/n)?').lower()
            if choice == 'n':
                break

    def load(self,fn):
        #导入信息
        if os.path.exists(fn):
            try:
                with open(fn,'r',encoding = 'utf-8') as fp:
                    while True:
                        fs = fp.readline().strip('\n')
                        if not fs:
                            break
                        else:
                            gd = Goods(*fs.split(','))
                            if self.__exists(gd.no):
                                print('该编号已存在')
                            else:
                                self.gdslist.append(gd)
                print('导入完毕')
            except:
                print('error...')
        else:
            print('要导入的文件不存在')
        

    def save(self,fn):
        #导出信息
        with open(fn,'w',encoding = 'utf-8') as fp:
            for gd in self.gdslist:
                fp.write(gd.no + ',')
                fp.write(gd.name + ',')
                fp.write(str(gd.first) + ',')
                fp.write(str(gd.second) + ',')                    
                fp.write(str(gd.third) + '\n')                    
            print("导出完毕")

    def sale_avg(self):
        #求平均销量
        length = len(self.gdslist)
        if length > 0:
            first_avg = sum([gd.first for gd in self.gdslist])/length
            second_avg = sum([gd.second for gd in self.gdslist])/length
            third_avg = sum([gd.third for gd in self.gdslist])/length
            print('一季度平均销量是:%.2f'%first_avg)
            print('二季度平均销量是:%.2f'%second_avg)
            print('三季度平均销量是:%.2f'%third_avg)
        else:
            print('尚没有商品销量...')

    def sale_max(self):
        #求最高
        if len(self.gdslist) > 0:
            first_max = max([gd.first for gd in self.gdslist])
            second_max = max([gd.second for gd in self.gdslist])
            third_max = max([gd.third for gd in self.gdslist])
            print('一季度最高销量是:%d'%first_max)
            print('二季度最高销量是:%d'%second_max)
            print('三季度最高销量是:%d'%third_max)
        else:
            print('尚没有商品销量...')

    def sale_min(self):
        #求最低
        if len(self.gdslist) > 0:
            first_min = min([gd.first for gd in self.gdslist])
            second_min = min([gd.second for gd in self.gdslist])
            third_min = min([gd.third for gd in self.gdslist])
            print('一季度最低销量是:%d'%first_min)
            print('二季度最低销量是:%d'%second_min)
            print('三季度最低销量是:%d'%third_min)
        else:
            print('尚没有商品销量...')

    def infoprocess(self):
        #基本信息管理
        print('商品基本信息管理'.center(20,'-'))
        print('load----------导入商品信息')
        print('insert--------添加商品信息')
        print('delete--------删除商品信息')
        print('update--------修改商品信息')
        print('show----------显示商品信息')
        print('save----------导出商品信息')
        print('return--------返回')
        print('-'*28)
        while True:
            s = input('info>').strip().lower()
            if s == 'load':
                fn = input('请输入要导入的文件名:')
                self.load(fn)
            elif s == 'show':
                self.show()
            elif s == 'insert':
                self.insert()
            elif s == 'delete':
                self.delete()
            elif s == 'update':
                self.update()
            elif s == 'save':
                fn = input('请输入要导出的文件名:')
                self.save(fn)
            elif s =='return':
                break
            else:
                print('输入错误')
    
    def saleprocess(self):
        #销量统计
        print('商品销量统计'.center(24,'='))
        print('avg    --------平均销量')
        print('max    --------最高销量')
        print('min    --------最低销量')        
        print('return --------返回')
        print(''.center(30,'='))
        while True:
            s = input('sale>').strip().lower()
            if s == 'avg':                
                self.sale_avg()
            elif s == 'max':                
                self.sale_max()
            elif s == 'min':                
                self.sale_min()
            elif s == 'return':
                break
            else:
                print('输入错误')
                
    def main(self):
        #主控函数               
        while True:
            print('商品信息管理系统V1.0'.center(24,'='))
            print('info  --------商品基本信息管理')
            print('sale  --------商品销量统计')
            print('exit  --------退出系统')
            print(''.center(32,'='))
            s = input('main>').strip().lower()
            if s == 'info':
                self.infoprocess()
            elif s == 'sale':
                self.saleprocess()
            elif s == 'exit':
                break
            else:
                print('输入错误')   
        

if __name__ == '__main__':
    gds = GoodsList()
    gds.main()

Guess you like

Origin blog.csdn.net/agelee/article/details/128289588