Python学生通讯录管理系统案例(文件版)
时间: 2024-04-09 21:21:10 浏览: 150
下面是一个简单的 Python 学生通讯录管理系统案例(文件版)。
首先,我们需要定义一个学生类,包含学号、姓名和电话号码:
```python
class Student:
def __init__(self, id, name, phone):
self.id = id
self.name = name
self.phone = phone
```
接下来,我们需要定义一个通讯录类,包含对学生信息进行增加、删除、修改和查询的方法:
```python
import csv
class AddressBook:
def __init__(self, filename):
self.filename = filename
self.students = []
self.load()
def add(self, student):
self.students.append(student)
def delete(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
def update(self, id, name, phone):
for student in self.students:
if student.id == id:
student.name = name
student.phone = phone
def search(self, id):
for student in self.students:
if student.id == id:
return student
return None
def load(self):
with open(self.filename, 'r') as file:
reader = csv.reader(file)
for row in reader:
id = int(row[0])
name = row[1]
phone = row[2]
student = Student(id, name, phone)
self.add(student)
def save(self):
with open(self.filename, 'w', newline='') as file:
writer = csv.writer(file)
for student in self.students:
writer.writerow([student.id, student.name, student.phone])
```
这个通讯录类使用了 CSV 文件来存储学生信息。在初始化方法中,它会从文件中读取学生信息并将其存储在内存中。在保存方法中,它会将内存中的学生信息写入文件。
接下来,我们可以编写一个控制台程序来使用这个通讯录类:
```python
def main():
address_book = AddressBook('students.csv')
while True:
print('1. Add student')
print('2. Delete student')
print('3. Update student')
print('4. Search student')
print('5. List all students')
print('6. Save and exit')
choice = input('Enter your choice: ')
if choice == '1':
id = int(input('Enter student id: '))
name = input('Enter student name: ')
phone = input('Enter student phone: ')
student = Student(id, name, phone)
address_book.add(student)
elif choice == '2':
id = int(input('Enter student id: '))
address_book.delete(id)
elif choice == '3':
id = int(input('Enter student id: '))
name = input('Enter student name: ')
phone = input('Enter student phone: ')
address_book.update(id, name, phone)
elif choice == '4':
id = int(input('Enter student id: '))
student = address_book.search(id)
if student:
print(student.id, student.name, student.phone)
else:
print('Student not found')
elif choice == '5':
for student in address_book.students:
print(student.id, student.name, student.phone)
elif choice == '6':
address_book.save()
break
else:
print('Invalid choice')
```
这个程序使用一个循环来不断接收用户输入的命令,并根据命令调用通讯录类的相应方法。当用户选择保存并退出时,程序会将学生信息保存到文件并退出。
完整代码如下:
```python
import csv
class Student:
def __init__(self, id, name, phone):
self.id = id
self.name = name
self.phone = phone
class AddressBook:
def __init__(self, filename):
self.filename = filename
self.students = []
self.load()
def add(self, student):
self.students.append(student)
def delete(self, id):
for student in self.students:
if student.id == id:
self.students.remove(student)
def update(self, id, name, phone):
for student in self.students:
if student.id == id:
student.name = name
student.phone = phone
def search(self, id):
for student in self.students:
if student.id == id:
return student
return None
def load(self):
with open(self.filename, 'r') as file:
reader = csv.reader(file)
for row in reader:
id = int(row[0])
name = row[1]
phone = row[2]
student = Student(id, name, phone)
self.add(student)
def save(self):
with open(self.filename, 'w', newline='') as file:
writer = csv.writer(file)
for student in self.students:
writer.writerow([student.id, student.name, student.phone])
def main():
address_book = AddressBook('students.csv')
while True:
print('1. Add student')
print('2. Delete student')
print('3. Update student')
print('4. Search student')
print('5. List all students')
print('6. Save and exit')
choice = input('Enter your choice: ')
if choice == '1':
id = int(input('Enter student id: '))
name = input('Enter student name: ')
phone = input('Enter student phone: ')
student = Student(id, name, phone)
address_book.add(student)
elif choice == '2':
id = int(input('Enter student id: '))
address_book.delete(id)
elif choice == '3':
id = int(input('Enter student id: '))
name = input('Enter student name: ')
phone = input('Enter student phone: ')
address_book.update(id, name, phone)
elif choice == '4':
id = int(input('Enter student id: '))
student = address_book.search(id)
if student:
print(student.id, student.name, student.phone)
else:
print('Student not found')
elif choice == '5':
for student in address_book.students:
print(student.id, student.name, student.phone)
elif choice == '6':
address_book.save()
break
else:
print('Invalid choice')
if __name__ == '__main__':
main()
```
在运行这个程序之前,需要先创建一个名为 `students.csv` 的文件,并将其放在程序所在的目录中。文件格式应该如下:
```
1,John,1234567890
2,Jane,0987654321
```
每一行表示一个学生的信息,用逗号分隔学号、姓名和电话号码。
阅读全文