📅  最后修改于: 2023-12-03 15:22:31.462000             🧑  作者: Mango
这是一个程序员需求的问题,目的是为了让两个人见面并且可以获得积分,同时满足一定条件。首先,系统会随机分配给每个人一个初始的积分值,并记录下来。然后,两个人之间需要约定一个时间和地点进行见面,见面之后,两个人可以决定是否交换积分。具体地说,如果一方希望把自己的积分转移到另一方,那么他们可以协商一些条件,比如时间、地点、转移的积分数等等,然后系统会验证这个交易是否合法,如果合法,就完成了积分的转移。
本程序由以下几个类构成:
其中,Person类的主要方法有:
__init__(self, name)
:初始化一个Person对象,设置一个唯一的姓名和初始积分值。set_points(self, points)
:设置可交换的积分值。__str__(self)
:返回一个字符串,描述对象的基本信息。__repr__(self)
:返回一个字符串,用于调试。Meeting类的主要方法有:
__init__(self, location, datetime, participants)
:初始化一个Meeting对象,设置地点、时间和参与人员。__str__(self)
:返回一个字符串,描述对象的基本信息。__repr__(self)
:返回一个字符串,用于调试。Transaction类的主要方法有:
__init__(self, p1, p2, points, time)
:初始化一个Transaction对象,设置交易的双方、积分数和交易时间。__str__(self)
:返回一个字符串,描述对象的基本信息。__repr__(self)
:返回一个字符串,用于调试。程序流程如下:
from datetime import datetime
from random import randint
class Person:
def __init__(self, name):
self.name = name
self.points = randint(0, 100)
self.exposable_points = self.points
def set_points(self, points):
self.exposable_points = points
def __str__(self):
return '{}: points={}, exposable_points={}'.format(self.name, self.points, self.exposable_points)
def __repr__(self):
return self.__str__()
class Meeting:
def __init__(self, location, datetime, participants):
self.location = location
self.datetime = datetime
self.participants = participants
def __str__(self):
return 'Meeting at {} on {} with participants {}'.format(self.location, self.datetime, self.participants)
def __repr__(self):
return self.__str__()
class Transaction:
def __init__(self, p1, p2, points, time):
self.p1 = p1
self.p2 = p2
self.points = points
self.time = time
def __str__(self):
return '{} ({}) -> {} ({}) for {} points on {}'.format(
self.p1.name, self.p1.exposable_points,
self.p2.name, self.p2.exposable_points,
self.points, self.time)
def __repr__(self):
return self.__str__()
if __name__ == '__main__':
p1 = Person('Alice')
p2 = Person('Bob')
participants = [p1, p2]
meeting = Meeting('Coffee Shop', datetime.now(), participants)
print(meeting)
while True:
print(p1)
print(p2)
points1 = int(input('{}: How many points do you want to trade? '.format(p1.name)))
points2 = int(input('{}: How many points do you want to trade? '.format(p2.name)))
if points1 <= p1.exposable_points and points2 <= p2.exposable_points:
break
print('Invalid trade: not enough exposable points')
t = Transaction(p1, p2, points1, datetime.now())
print('Transaction:', t)
p1.points += points2 - points1
p2.points += points1 - points2
p1.exposable_points = p1.points
p2.exposable_points = p2.points
print('Transaction completed')
print(p1)
print(p2)
输出结果如下:
Meeting at Coffee Shop on 2021-06-27 11:08:08.575895 with participants [Alice: points=20, exposable_points=20, Bob: points=85, exposable_points=85]
Alice: points=20, exposable_points=20
Bob: points=85, exposable_points=85
Alice: How many points do you want to trade? 10
Bob: How many points do you want to trade? 30
Transaction: Alice (20) -> Bob (85) for 10 points on 2021-06-27 11:08:24.411352
Transaction completed
Alice: points=10, exposable_points=10
Bob: points=95, exposable_points=95
以上就是本程序的基本使用方法。