Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add python additional sample #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion C#/STPWithClass/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static void Main(string[] args)
case "show":
for (int i = 0; i < items.Count; i++)
{
Console.WriteLine($"{i}:{items[i].GetIntroTxt()}");
Console.WriteLine($"{i}:{items[i].GetIntroTxt()}");
}
break;
case "buy":
Expand Down
100 changes: 100 additions & 0 deletions Python/STP_advance/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
class Item:
def __init__(self,name,value):
self.name=name
self.value=value
self.count=0

def Buy(self):
self.count+=1

def GetAmountOfMoney(self):
return self.value*self.count

def GetIntroTxt(self):
return f"{self.name}:{self.value}円"

class Cart:
def __init__(self):
self.items=[]
self.item_names=[]

def GetSum(self):
total=0
for item in self.items:
total+=item.GetAmountOfMoney()
return total

def AddItem(self,cmds):
if not cmds[2].isdecimal():
return "3つ目には価格を入れてください"
if cmds[1] in self.item_names:
return "既にその名前のアイテムが存在します"
name=cmds[1]
value=int(cmds[2])
newItem=Item(name,value)
self.item_names.append(name)
self.items.append(newItem)
return f"{value}円の{name}を登録しました"

def CheckOut(self):
for item in self.items:
print(item.GetIntroTxt()+f"*{item.count}")
return f"合計金額:{self.GetSum()}円"

def Show(self):
if len(self.items)==0:
return "まだ何も登録していません"
ret=""
for i,item in enumerate(self.items):
ret+= f"{i}:{item.GetIntroTxt()}\n"
return ret

def Buy(self,cmd):
targetitem=self.SearchItem(cmd[1])
if targetitem==-1:
return "アイテムが見つかりませんでした"
targetitem.Buy()
return f"{targetitem.name}を一個購入しました"

def SearchItem(self,cmd):
for item in self.items:
if item.name==cmd:
return item
if cmd.isdecimal():
index=int(cmd)
if 0<=index<len(self.items):
return self.items[index]
else:
return -1
return -1



def print_help():
print("exit:このプログラムを終了する")
print("add [name] [value]:新たな商品nameをvalue円で登録する")
print("show:今までに登録した賞品の一覧を出力")
print("buy [name/index]:指定した商品を購入、該当しない場合は何も起こらない")
print("checkout:今まで購入した商品で会計を行い、合計金額などを出力")



mycart=Cart()
while True:
cmd=input("STP>")
cmds=cmd.split(" ")
if cmds[0]=="exit":
break
elif cmds[0]=="help":
print_help()
elif cmds[0]=="add":
print(mycart.AddItem(cmds))
elif cmds[0]=="show":
print(mycart.Show())
elif cmds[0]=="buy":
print(mycart.Buy(cmds))
elif cmds[0]=="checkout":
print(mycart.CheckOut())
else:
print("有効なコマンドを指定してください")
print("コマンド一覧はhelpで確認できます")
51 changes: 51 additions & 0 deletions Python/STP_with_class/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class Item:
def __init__(self,name,value):
self.name=name
self.value=value
self.count=0

def Buy(self):
self.count+=1

def GetAmountOfMoney(self):
return self.value*self.count

def GetIntroTxt(self):
return f"{self.name}:{self.value}円"

def GetSum():
total=0
for item in items:
total+=item.GetAmountOfMoney()
return total

def AddItem(cmds):
name=cmds[1]
value=int(cmds[2])
newItem=Item(name,value)
items.append(newItem)
print(f"{value}円の{name}を登録しました")

def CheckOut():
for item in items:
print(item.GetIntroTxt()+f"*{item.count}")
print(f"合計金額:{GetSum()}円")

items=[]

while True:
cmd=input("STP>")
cmds=cmd.split(" ")
if cmds[0]=="exit":
break
elif cmds[0]=="add":
AddItem(cmds)
elif cmds[0]=="show":
for i,item in enumerate(items):
print(f"{i}:{item.GetIntroTxt()}")
elif cmds[0]=="buy":
targetindex=int(cmds[1])
print(f"{items[targetindex].name}を一個購入しました")
items[targetindex].Buy()
elif "checkout":
CheckOut()
14 changes: 7 additions & 7 deletions Python/STP_without_class/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
item_names.append(name)
item_values.append(value)
item_counts.append(0)
print('{}円の{}を登録しました'.format(value, name))
print(f'{value}円の{name}を登録しました')
if cmds[0] == 'show':
for i in range(len(item_names)):
print('{}:{}:{}円'.format(i, item_names[i], item_values[i]))
print(f'{i}:{item_names[i]}:{item_values[i]}円')
if cmds[0] == 'buy':
target_index = int(cmds[1])
item_counts[target_index] += 1
print('{}を購入しました'.format(item_names[target_index]))
print(f'{item_names[target_index]}を購入しました')
if cmds[0] == 'checkout':
sum = 0
total = 0
for i in range(len(item_names)):
sum += item_values[i] * item_counts[i]
print('{} {}円×{}'.format(item_names[i], item_values[i], item_counts[i]))
total += item_values[i] * item_counts[i]
print(f'{item_names[i]} {item_values[i]}円×{item_counts[i]}')
item_counts[i] = 0
print('合計金額:{}円'.format(sum))
print(f'合計金額:{total}円')