from pymysql import connect
class JD(object):
def __init__(self):
self.is_login = False #默认没有登录
# 创建Connection连接
self.conn = connect(host='localhost',port=3306,user='root',password='123456',database='jd',charset='utf8')
# 获得Cursor对象
self.cursor = self.conn.cursor()
def __del__(self):
# 关闭Cursor对象
self.cursor.close()
# 关闭Connection对象
self.conn.close()
def register(self):
username = input("请输入您注册的用户名:")
address = input("请输入地址:")
tel = input("请输入您的手机号:")
pwd = input("请输入您注册的密码:")
sql = "insert into customers values(0,%s,%s,%s,password(%s));"#password 包一下的意思加密
self.cursor.execute(sql,[username,address,tel,pwd])
self.conn.commit()
def login(self):
username=input("请输入用户名:")
pwd = input("请输入您的密码:")
sql = "select id from customers where name=%s and passwd=password(%s)"
self.cursor.execute(sql,[username,pwd])
self.uid = self.cursor.fetchone()
if self.uid:
self.uid = self.uid[0]
self.is_login = True
return self.is_login
def show_all_items(self):
#显示所有商品
sql = "select * from goods;"
self.execute_sql(sql)
def show_cates(self):
sql = "select name from goods;"
self.execute_sql(sql)
def show_brands(self):
sql = "select name from goods_brands;"
self.execute_sql(sql)
def add_cates(self):
item_name = input("请输入新商品分类的名称:")
sql = """insert into goods_cates (name) values("%s");""" %item_name
self.cursor.execute(sql)
self.conn.commit()
def change_cates(self):
old_name = input("请输入您要更改的商品分类的名称:")
new_name = input("请输入您要更改的新商品名称:")
sql = """update goods_cates set name=("%s") where name=("%s");"""%(new_name,old_name)
self.cursor.execute(sql)
self.conn.commit()
def delete_cates(self):
del_id = int(input("请输入您要删除的id号:"))
sql = """delete from goods_cates where id=("%d");"""%del_id
self.cursor.execute(sql)
self.conn.commit()
def get_info_by_name(self):
find_name = input("请输入要查询的商品的名字:")
sql ="select * from goods where name=%s;"
self.cursor.execute(sql,[find_name])
print(self.cursor.fetchall())
def create_order(self):
pid = input("请输入商品的编号:")
num = input('请输入商品的数量:')
sql ="select * from goods where id=%s;"
result = self.cursor.execute(sql,[pid])
if result:
sql = "insert into orders values(0,now(),%s);"
self.cursor.execute(sql,[self.uid])
sql = "insert into order_detail values(0,%s,%s,%s);"
self.cursor.execute(sql,[self.cursor.lastrowid,pid,num])
self.conn.commit()
print("下单成功")
else:
print('你要购买的商品已下架')
@staticmethod
def sys_menu():
print("-----京东-----")
print("1 >>> 注册")
print("2 >>> 登录")
print("3 >>> 退出")
return input("请输入功能对应的序号:")
@staticmethod
def print_menu():
print("")
print("-----系统菜单-----")
print("1:所有的商品")
print("2:所有的商品分类")
print("3:所有的商品品牌")
print("4:添加商品分类")
print("5:更改商品分类")
print("6:删除商品分类")
print("7:查找商品")
print("8:下单")
print("其他任意键:退出")
return input("请输入功能对应的序号:")
def run(self):
while True:
num = self.sys_menu()
if num == "1":
#注册
self.register()
elif num == "2":
#登录
if self.login():
print("登录成功")
break
else:
print("用户名或密码有误,请重新输入......")
elif num== "3":
break
else:
print('输入有误,请重新输入')
if self.is_login:#如果登录成功,显示系统菜单
while True:
num = self.print_menu()
if num == "1":
#查询所有商品
self.show_all_items()
elif num == "2":
#查询分类
self.show_cates()
elif num == "3":
#查询品牌
self.show_brands()
elif num == "4":
#添加商品分类
self.add_cates()
elif num == "5":
#更改商品分类
self.change_cates()
elif num == "6":
#删除商品分类
self.del_cates()
elif num == "7":
#根据名字查询商品
self.get_info_by_name()
elif num == "8":
# 下单
self.create_order()
else:
print("----再见----")
break
else:
print("----再见----")
def main():
#1.创建一个京东商城对象
jd = JD()
#2.调用这个盾想的run方法,让其运行。
jd.run()
if __name__ == '__main__':
main()
网友评论