本文章是 【Python語言基礎】 專欄的文章,主要是上課的隨堂筆記與練習
Python專欄 傳送門
實驗源碼已在Github整理
利用abc庫,設計抽象類Shape,在Shape中定義抽象方法getArea()
和周長getPerimeter()
,之後讓Triangle、Rectangle、Circle分別繼承Shape類並重寫函數,其中Triangle類輸入的三邊需要額外判斷是否滿足兩邊之和永遠大於第三邊。設計好四個類之後,在主函數中新建對象並調用計算周長和面積
""" @Author:張時貳 @Date:2022年05月27日 @CSDN:張時貳 @Blog:zhangshier.vip """
import abc
import math
# 抽象類,用abc庫,只需要聲明函數,不需要寫具體功能,不能夠實例化.類似C+純虛函數
class Shape ( metaclass=abc.ABCMeta ):
# 面積
@abc.abstractmethod
def getArea(self):
pass
# 周長
@abc.abstractmethod
def getPerimeter(self):
pass
# 三角形
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
# 判斷三邊關系
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
# 矩形
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
# 圓
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 ( "請輸入三角形三邊:" ).split ( " " ) )
t1 = Triangle ( a1, a2, a3 )
if t1.judgeInput ():
print ( f"三角形面積={
t1.getArea ()}" )
print ( f"三角形周長={
t1.getPerimeter ()}" )
else:
print ( "輸入的三邊長不能構成三角形" )
b1, b2 = map ( int, input ( "請輸入矩形兩邊:" ).split ( " " ) )
r1 = Rectangle ( b1, b2 )
print ( f"矩形面積={
r1.getArea ()}" )
print ( f"矩形周長={
r1.getPerimeter ()}" )
r1 = int ( input ( "請輸入圓的半徑r1:" ) )
c1 = Circle ( r1 )
print ( "圓面積%.2f"%c1.getArea () )
print ( "圓周長%.2f"%c1.getPerimeter () )
設計一個“超市進銷存管理系統”,要求如下:
分別設計三個類,已售出類,庫存類,管理商品類。在管理商品類中通過兩個列表分別保存已售出和庫存信息。之後通過Menu菜單調用要求的7個功能,其中修改、刪除、賣出功能調用時,需要一個先調用check()函數判斷是否存在該商品
""" @Author:張時貳 @Date:2022年05月27日 @CSDN:張時貳 @Blog:zhangshier.vip """
# 定義庫存類
class Goods:
# 有參構造初始化
def __init__(self, name, num, cin, cout):
self.name = name
self.num = num
self.cin = cin
self.cout = cout
def __str__(self):
state = "已售罄"
if self.num == 0:
return '名稱:%s , 數量:%d %s, 進貨價格:%.2f ,售出價格:%.2f '%(self.name, self.num, state, self.cin, self.cout)
else:
return '名稱:%s , 數量:%d , 進貨價格:%.2f ,售出價格:%.2f '%(self.name, self.num, self.cin, self.cout)
# 已售出類
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 '名稱:%s , 賣出數量:%d , 進貨價格:%.2f ,賣出價格:%.2f '%(self.name, self.gnum, self.gcin, self.gcout)
# 定義管理商品類
class GoodsManager:
go = [ ] # 庫存
js = [ ] # 售出
# 構造方法,初始化加三個商品
def init(self):
self.go.append ( Goods ( '牛奶', 5, 40, 60 ) )
self.go.append ( Goods ( '盒飯', 5, 10, 60 ) )
self.js.append ( gGoods ( '面條', 1, 30, 60 ) )
# 菜單
def Menu(self):
self.init ()
print ( '\"超市進銷存管理系統\"菜單:' )
print ( "1.顯示所有商品" )
print ( "2.添加新的商品" )
print ( "3.修改商品信息" )
print ( "4.刪除商品" )
print ( "5.賣出商品" )
print ( "6.匯總" )
print ( "-1.退出" )
print ( "***********************************" )
while True:
SN = int ( input ( "請輸入操作序號:" ) )
if SN in [ -1, 1, 2, 3, 4, 5, 6 ]:
if SN == -1:
print ( "已經退出" )
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 ( "輸入有誤!" )
# 顯示
def Show_all(self):
for goods in self.go:
print ( str ( goods ) )
# 添加
def Add(self):
goods_name = input ( "請輸入商品名稱:" )
ret = self.check ( goods_name )
if ret != None:
print ( '商品已經存在' )
print ( '是否增加商品數量:(y/n)' )
while True:
pd = input ()
if pd == 'y':
goods_num = int ( input ( "請輸入商品的數量:" ) )
old_goods = Goods ( goods_name, goods_num + ret.num, ret.cin, ret.cout )
self.go.remove ( ret )
self.go.append ( old_goods )
print ( "增加成功" )
break
elif pd == 'n':
print ( "已經返回" )
break
else:
print ( "輸入有誤,重新輸入:" )
else:
goods_num = int ( input ( "請輸入商品的數量:" ) )
goods_cin = float ( input ( "請輸入商品進貨價格:" ) )
goods_cout = float ( input ( "請輸入商品出貨價格:" ) )
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 ( "添加成功" )
else:
print ( "輸入錯誤!" )
# 修改
def Modify(self):
goods_name = input ( "請輸入需要修改的商品名稱:" )
ret = self.check ( goods_name )
if ret != None:
print ( ret )
goods_name1 = input ( "請輸入修改後商品的名稱:" )
goods_num = int ( input ( "請輸入修改後商品的數量:" ) )
goods_cin = float ( input ( "請輸入修改後商品進貨價格:" ) )
goods_cout = float ( input ( "請輸入修改後商品出貨價格:" ) )
old_goods = Goods ( goods_name1, goods_num, goods_cin, goods_cout )
self.go.remove ( ret )
self.go.append ( old_goods )
print ( "修改成功" )
else:
print ( "沒有此商品!" )
# 檢查,修改刪除賣出之前先調用檢查是否存在商品
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
# 刪除
def Delete(self):
goods_name = input ( "請輸入需要刪除的商品名稱:" )
ret = self.check ( goods_name )
if ret != None:
print ( ret )
print ( '是否刪除商品:(y/n)' )
while True:
pd = input ()
if pd == 'y':
self.go.remove ( ret )
print ( "刪除成功" )
break
elif pd == 'n':
print ( "已經返回" )
break
else:
print ( "輸入有誤,重新輸入:" )
else:
print ( "沒有此商品!" )
# 賣出
def Shop(self):
goods_name = input ( "請輸入需要賣出的商品名稱:" )
ret = self.check ( goods_name )
if ret != None:
g_num = int ( input ( "賣出個數:" ) )
if ret.num - g_num < 0:
print ( "該商品數量不足!請補充" )
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 ( "賣出後:", end=' ' )
old_goods = Goods ( ret.name, ret.num - g_num, ret.cin*g_num, ret.cout*g_num )
print ( old_goods )
else:
print ( "沒有此商品!" )
# 匯總當天賣出商品,包括每種銷售商品名稱、數量、進貨總價、銷售總價等。
def Summary(self):
for goods in self.js:
print ( goods )
print ( "售出的物品進貨總價:", end="" )
x = 0
for goods in self.js:
x += float ( goods.gcin )
print ( "售出的物品銷售總價:", end="" )
y = 0
for goods in self.js:
y += float ( goods.gcout )
print ( y )
print ( "利潤:", y - x )
if __name__ == '__main__':
manager = GoodsManager ()
manager.Menu ()
2021 year 12 month Python Anal