程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python experiment tetrahedral object programming

編輯:Python

2022.5.27 Afternoon experiment

Experiment four Object oriented programming

List of articles

    • Preface
    • Topic 1
    • Topic two

Preface

This article is 【Python Language foundation 】 Column articles , Mainly the notes and exercises in class
Python special column Portal
The experimental source code has been in Github Arrangement

Topic 1

  1. Define an abstract class Shape, In the abstract class Shape Area is defined in getArea() And perimeter getPerimeter() Abstract method of
  2. Define inheritance abstract classes respectively Shape Of 3 Subclasses are Triangle、Rectangle and Circle, Here 3 Subclasses Shape The method in getArea() and getPerimeter()
  3. Create a class Triangle、Rectangle、Circle The object of , Yes 3 Methods in classes

Problem analysis

utilize abc library , Design abstract class Shape, stay Shape Define abstract methods in getArea() And perimeter getPerimeter(), Then let Triangle、Rectangle、Circle Inherit separately Shape Class and override functions , among Triangle The three sides of class input need to judge whether the sum of the two sides is always greater than the third side . After designing the four classes , Create a new object in the main function and call to calculate the perimeter and area

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 27 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
import abc
import math
# abstract class , use abc library , Just declare the function , There is no need to write specific functions , It's not enough . similar C+ Pure virtual function 
class Shape ( metaclass=abc.ABCMeta ):
# area 
@abc.abstractmethod
def getArea(self):
pass
# Perimeter 
@abc.abstractmethod
def getPerimeter(self):
pass
# triangle 
class Triangle ( Shape ):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def getArea(self):
return 0.25*math.sqrt ( (self.a + self.b + self.c)*(self.a + self.b - self.c)*(self.b + self.c - self.a)*(
self.a + self.c - self.b) )
def getPerimeter(self):
return self.a + self.b + self.c
# Judge the trilateral relationship 
def judgeInput(self):
a1 = self.a + self.b - self.c
a2 = (self.a - self.b) - self.c
b1 = self.b + self.c - self.a
b2 = (self.b - self.c) - self.a
c1 = self.a + self.c - self.b
c2 = (self.a - self.c) - self.b
if (a1 > 0 and a2 < 0) and (b1 > 0 and b2 < 0) and (c1 > 0 and c2 < 0):
return True
else:
return False
# rectangular 
class Rectangle ( Shape ):
def __init__(self, a, b):
self.a = a
self.b = b
def getArea(self):
return self.a*self.b
def getPerimeter(self):
return (self.a + self.b)*2
# round 
class Circle ( Shape ):
def __init__(self, r):
self.r = r
def getArea(self):
return 3.14*self.r ** 2
def getPerimeter(self):
return 2*math.pi*self.r
if __name__ == '__main__':
a1, a2, a3 = map ( int, input ( " Please enter three sides of triangle :" ).split ( " " ) )
t1 = Triangle ( a1, a2, a3 )
if t1.judgeInput ():
print ( f" Triangle area ={
t1.getArea ()}" )
print ( f" The perimeter of the triangle ={
t1.getPerimeter ()}" )
else:
print ( " The input length of three sides cannot form a triangle " )
b1, b2 = map ( int, input ( " Please enter both sides of the rectangle :" ).split ( " " ) )
r1 = Rectangle ( b1, b2 )
print ( f" Rectangular area ={
r1.getArea ()}" )
print ( f" The perimeter of the rectangle ={
r1.getPerimeter ()}" )
r1 = int ( input ( " Please enter the radius of the circle r1:" ) )
c1 = Circle ( r1 )
print ( " Round area %.2f"%c1.getArea () )
print ( " Circumference %.2f"%c1.getPerimeter () )

result

Topic two

To design a “ Supermarket inventory management system ”, Requirements are as follows :

  1. The system includes 7 In the operation , Namely :1. Check all products ;2. Add the goods ;3. Modify the goods ;4. Delete item ;5. Selling goods ;6. Summary ;-1. Exit the system
  2. Select the operation sequence number “1”, Show all products
  3. Select the operation sequence number “2”, Add a new item ( Include product name 、 Quantity and purchase price )
  4. Select the operation sequence number “3”, Modify the goods
  5. Select the operation sequence number “4”, Delete item
  6. Select the operation sequence number “5”, Selling goods ( Include product name 、 Quantity and selling price )
  7. Select an operation sequence number “6”, Summarize the goods sold on that day , Include the name of each item sold 、 Number 、 Total purchase price 、 Total sales price, etc
  8. Select the operation sequence number “-1”, Exit the system .

Problem analysis

Design three classes respectively , Sold out category , Inventory class , Manage commodity categories . In the managed goods category, you can save the sold and inventory information through two lists . After through Menu Menu call required 7 Features , Which is modified 、 Delete 、 When selling function calls , You need a first call check() Function to determine whether the product exists

Code

""" @Author: Zhang Shier @Date:2022 year 05 month 27 Japan @CSDN: Zhang Shier @Blog:zhangshier.vip """
# Define inventory class 
class Goods:
# Parameterized structure initialization 
def __init__(self, name, num, cin, cout):
self.name = name
self.num = num
self.cin = cin
self.cout = cout
def __str__(self):
state = " It's sold out "
if self.num == 0:
return ' name :%s , Number :%d %s, Purchase price :%.2f , Selling price :%.2f '%(self.name, self.num, state, self.cin, self.cout)
else:
return ' name :%s , Number :%d , Purchase price :%.2f , Selling price :%.2f '%(self.name, self.num, self.cin, self.cout)
# Sold out category 
class gGoods:
def __init__(self, name, gnum, gcin, gcout):
self.name = name
self.gnum = gnum
self.gcin = gcin
self.gcout = gcout
def __str__(self):
return ' name :%s , Quantity sold :%d , Purchase price :%.2f , Selling price :%.2f '%(self.name, self.gnum, self.gcin, self.gcout)
# Define and manage commodity classes 
class GoodsManager:
go = [ ] # stock 
js = [ ] # Sell out 
# Construction method , Initialization plus three items 
def init(self):
self.go.append ( Goods ( ' milk ', 5, 40, 60 ) )
self.go.append ( Goods ( ' Box lunch ', 5, 10, 60 ) )
self.js.append ( gGoods ( ' noodles ', 1, 30, 60 ) )
# menu 
def Menu(self):
self.init ()
print ( '\" Supermarket inventory management system \" menu :' )
print ( "1. Show all products " )
print ( "2. Add a new item " )
print ( "3. Modify product information " )
print ( "4. Delete item " )
print ( "5. Selling goods " )
print ( "6. Summary " )
print ( "-1. sign out " )
print ( "***********************************" )
while True:
SN = int ( input ( " Please enter the operation serial number :" ) )
if SN in [ -1, 1, 2, 3, 4, 5, 6 ]:
if SN == -1:
print ( " Has been withdrawn from " )
break;
if SN == 1:
self.Show_all ()
elif SN == 2:
self.Add ()
elif SN == 3:
self.Modify ()
elif SN == 4:
self.Delete ()
elif SN == 5:
self.Shop ()
elif SN == 6:
self.Summary ()
else:
print ( " Incorrect input !" )
# Show 
def Show_all(self):
for goods in self.go:
print ( str ( goods ) )
# add to 
def Add(self):
goods_name = input ( " Please enter the product name :" )
ret = self.check ( goods_name )
if ret != None:
print ( ' The item already exists ' )
print ( ' Whether to increase the quantity of goods :(y/n)' )
while True:
pd = input ()
if pd == 'y':
goods_num = int ( input ( " Please enter the quantity of the product :" ) )
old_goods = Goods ( goods_name, goods_num + ret.num, ret.cin, ret.cout )
self.go.remove ( ret )
self.go.append ( old_goods )
print ( " Increase success " )
break
elif pd == 'n':
print ( " Has returned " )
break
else:
print ( " Incorrect input , Re input :" )
else:
goods_num = int ( input ( " Please enter the quantity of the product :" ) )
goods_cin = float ( input ( " Please enter the purchase price :" ) )
goods_cout = float ( input ( " Please enter the commodity shipping price :" ) )
if goods_num > 0 and goods_cin > 0 and goods_cout > 0:
new_goods = Goods ( goods_name, goods_num, goods_cin, goods_cout )
self.go.append ( new_goods )
print ( " Add success " )
else:
print ( " Input error !" )
# modify 
def Modify(self):
goods_name = input ( " Please enter the product name to be modified :" )
ret = self.check ( goods_name )
if ret != None:
print ( ret )
goods_name1 = input ( " Please enter the name of the modified product :" )
goods_num = int ( input ( " Please enter the quantity of the modified product :" ) )
goods_cin = float ( input ( " Please enter the purchase price of the goods after modification :" ) )
goods_cout = float ( input ( " Please enter the revised commodity shipping price :" ) )
old_goods = Goods ( goods_name1, goods_num, goods_cin, goods_cout )
self.go.remove ( ret )
self.go.append ( old_goods )
print ( " Modification successful " )
else:
print ( " This item is not available !" )
# Check , Before modifying and deleting a sale, call to check whether there is a product 
def check(self, goods_name):
for goods in self.go:
if goods.name == goods_name:
return goods
else:
return None
def checkjs(self, goods_name):
for goods in self.js:
if goods.name == goods_name:
return goods
else:
return None
# Delete 
def Delete(self):
goods_name = input ( " Please enter the product name to be deleted :" )
ret = self.check ( goods_name )
if ret != None:
print ( ret )
print ( ' Delete item :(y/n)' )
while True:
pd = input ()
if pd == 'y':
self.go.remove ( ret )
print ( " Delete successful " )
break
elif pd == 'n':
print ( " Has returned " )
break
else:
print ( " Incorrect input , Re input :" )
else:
print ( " This item is not available !" )
# sell 
def Shop(self):
goods_name = input ( " Please enter the name of the product to be sold :" )
ret = self.check ( goods_name )
if ret != None:
g_num = int ( input ( " Number of sales :" ) )
if ret.num - g_num < 0:
print ( " The quantity of this product is insufficient ! Please add " )
else:
old_goods = Goods ( ret.name, ret.num - g_num, ret.cin, ret.cout )
self.go.remove ( ret )
self.go.append ( old_goods )
gret = self.checkjs ( goods_name )
if gret == None:
shop_goods = gGoods ( ret.name, g_num, ret.cin*g_num, ret.cout*g_num )
self.js.append ( shop_goods )
else:
shop_goods = gGoods ( gret.name, g_num + gret.gnum, gret.gcin + ret.cin*g_num,
gret.gcout + ret.cout*g_num )
self.js.remove ( gret )
self.js.append ( shop_goods )
print ( " After selling :", end=' ' )
old_goods = Goods ( ret.name, ret.num - g_num, ret.cin*g_num, ret.cout*g_num )
print ( old_goods )
else:
print ( " This item is not available !" )
# Summarize the goods sold on that day , Include the name of each item sold 、 Number 、 Total purchase price 、 Total sales price, etc .
def Summary(self):
for goods in self.js:
print ( goods )
print ( " The total purchase price of the goods sold :", end="" )
x = 0
for goods in self.js:
x += float ( goods.gcin )
print ( " The total sales price of the goods sold :", end="" )
y = 0
for goods in self.js:
y += float ( goods.gcout )
print ( y )
print ( " profits :", y - x )
if __name__ == '__main__':
manager = GoodsManager ()
manager.Menu ()

result


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved