一个用Python实现的简单个人日常记账软件的示例代码:
import json
from datetime import date
# 加载用户数据
def load_data():
try:
with open('user_data.json', 'r') as file:
data = json.load(file)
return data
except FileNotFoundError:
return {}
# 保存用户数据
def save_data(data):
with open('user_data.json', 'w') as file:
json.dump(data, file)
# 用户登录函数,验证手机号和密码或验证码是否正确
def login(phone, password):
data = load_data()
if phone in data and password == data[phone]['password']:
print("登录成功!")
return True
print("手机号或密码错误。")
return False
# 发送验证码函数(示例)
def send_verification_code(phone):
# 在此处编写发送验证码的逻辑
verification_code = "123456" # 假设收到的验证码为123456
return verification_code
# 验证码登录函数,验证手机号和验证码是否匹配
def login_with_verification_code(phone, code):
verification_code = send_verification_code(phone)
if code == verification_code:
print("登录成功!")
return True
print("手机或验证码错误。")
return False
# 记录消费/收入信息,并将其保存到用户数据中
def record_transaction(phone, transaction_type, amount):
today = str(date.today())
data = load_data()
if phone not in data:
user_info = {'password': '', 'transactions': {}}
data[phone] = user_info
transactions = {
'type': transaction_type,
'amount': amount
}
if today in data[phone]['transactions']:
data[phone]['transactions'][today].append(transactions)
else:
data[phone]['transactions'][today] = [transactions]
save_data(data)
# 统计每日支出和收入报表
def generate_daily_report(phone):
data = load_data()
if phone in data and 'transactions' in data[phone]:
transactions = data[phone]['transactions']
for date, transaction_list in transactions.items():
total_expenses = 0
total_income = 0
for transaction in transaction_list:
amount = transaction['amount']
if transaction['type'] == 'expense':
total_expenses += amount
elif transaction['type'] == 'income':
total_income += amount
print(f"Date: {date}")
print(f"Total Expenses: {total_expenses}")
print(f"Total Income: {total_income}")
# 添加新的财务类型到用户数据中
def add_transaction_type(phone, new_type):
data = load_data()
if phone not in data:
user_info = {'password': '', 'transaction_types': []}
data[phone] = user_info
if new_type not in data[phone]['transaction_types']:
# 将新类型添加到列表中,并保存用户数据
data[phone]['transaction_types'].append(new_type)
save_data(data)
# 删除特定的财务类型从用户数据中
def delete_transaction_type(phone, type_to_delete):
data=load_data()
if phone not in userdata or "transactiontypes" notinuserdata [number]:
print("无法删除:找不到指定的电话号码或没有任何事务记录")
return
if type_to_delete not in userdata [number]["transactiontypes"]:
print("无法删除:指定的事务类型不存在")
else:
userdata[number]["transactiontypes"] .remove(type_to_delete)
save_data(userdata)
# 修改特定财务类型在用户数据中
def modify_transaction_type(phone, old_type, new_type):
data=load_data()
if phone not in userdata or "transactiontypes" notinuserdata [number]:
print("无法修改:找不到指定的电话号码或没有任何事务记录")
return
ifoldtype notinuserdata[number] ["transactiontypes"]:
print ("无法修改:指定的事务类型不存在。 ")
else:
index = userdata [number] ["transaction_types"].index (oldtype)
userdata [number] ["TransactionTypes"] [index]=newtype
save_data (userdata)
# 主程序流程
def main():
while True:
print("1. 登录\n2. 注册\n3. 退出")
choice = input("请输入选项: ")
if choice == '1':
phone = input("请输入手机号: ")
login_choice = input("请选择登录方式(1-密码,2-验证码): ")
if login_choice == '1':
password=input('请输入密码:')
if login(phone,password):
break
elif login_choice=='2' :
code=input('请输入验证码:')
if login_with_verification_code(phone,code):
break
elif choice == '2':
# 在此处编写注册逻辑
# 注册成功后返回主菜单
elifchoice=='3' :
print ('谢谢使用!')
break
while True:
print("1. 记录消费\n2. 记录收入\n3. 生成日报表\n4. 添加财务类型\n5. 删除财务类型 \n6修改财务类型")
choice = input("请输入选项: ")
if choice == '1':
transaction_type = input("请输入支出类型: ")
amount = float(input("请输入金额: "))
record_transaction(phone, "expense", amount)
elifchoice=='2' :
transaction_Type=input('请输入收入方式:')
Amount=float (input('请输出金额:'))
Record Transaction(number,"income"Amount )
elifchoice=='3' :
generate_daily_report(phone)
elifchoice=='4' :
newtype=Input ('Please enter the new transaction type:')
add_transaction_type(Number,newtype)
elif choice == '5':
typetodelete = input(“Enter the type to delete:”)
delete_transaction_type(Number,typetodelete)
elifchoice==‘6’:
oldnewtype=input("please enter the old and new types to modify:")
oldtype,newype=oldnewtypesplit()
modifytransactiontipe(number.oldtypenewtye)
# 运行主程序
if __name__ == "__main__":
main()
这只是一个简单的示例,你可以根据自己的需求和喜好进行扩展和改进。记得确保在对用户数据做任何操作之前进行适当的验证和错误处理,并定期备份数据以避免丢失。另外,界面美观、稳定性和运行效率等方面的要求可以通过使用相关的GUI库和优化技术来实现。