Created
April 17, 2017 23:24
-
-
Save luiscastillocr/186ef47047a8e6c6cf9c30e29c593da8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import fileinput | |
from os import system | |
class Student(object): | |
id = 0 | |
age = 0 | |
name = "" | |
address = "" | |
def fill(self): | |
self.id = input("ID: ") | |
self.name = input("Nombre: ") | |
self.age = int(input("Edad: ")) | |
self.address = input("Dirección: ") | |
def edit_mode(self): | |
self.id = input("ID: (enter to skip) ") or self.id | |
self.name = input("Nombre: (enter to skip) ") or self.name | |
self.age = input("Edad: (enter to skip) ") or self.age | |
self.address = input("Dirección: (enter to skip) ") or self.address | |
def to_csv_line(self): | |
return "{0},{1},{2},{3}".format(self.id, self.name, self.age, self.address) | |
class StudentRepository(object): | |
@classmethod | |
def create(cls, _student): | |
with open("students.csv", "a") as file: | |
file.write("\n") | |
file.write(_student.to_csv_line()) | |
@classmethod | |
def edit(cls, student): | |
with fileinput.FileInput("students.csv", inplace=True, backup=".bak") as file: | |
for _line in file: | |
data = _line.split(",") | |
if data[0] == student.id: | |
print(student.to_csv_line()) | |
return None | |
@classmethod | |
def search(cls, id): | |
student = None | |
with open("students.csv", "r") as file: | |
for line in file: | |
data = line.split(",") | |
if data[0] == id: | |
student = Student() | |
student.id = data[0] | |
student.name = data[1] | |
student.age = int(data[2]) | |
student.address = data[3] | |
return student | |
# Menú principal | |
def menu(): | |
option = input(""" | |
+==========================================+ | |
| | | |
| Control de estudiantes | | |
| | | |
+==========================================+ | |
[?] Seleccione opción: | |
1. Ingresar un estudiante | |
2. Buscar un estudiante | |
3. Salir | |
> """) | |
if option == "1": | |
student = Student() | |
student.fill() | |
StudentRepository.create(student) | |
print(""" | |
[✓] Estudiante creado. | |
[↺] Volviendo al menú en 2s | |
""") | |
time.sleep(1.5) | |
system("clear") | |
return menu() | |
if option == "2": | |
id = input("\nIngresa el ID: ") | |
student = StudentRepository.search(id) | |
if student is not None: | |
return search_menu(student) | |
else: | |
print(""" | |
[✕] Estudiante no encontrado. | |
[↺] Volviendo al menú en 2s | |
""") | |
time.sleep(1.5) | |
system("clear") # limpia la pantalla | |
menu() | |
if option == "3": | |
print("Good Bye") | |
sys.exit(0) | |
# Menú de búsqueda | |
def search_menu(student): | |
option = input(""" | |
[✓] Estudiante encontrado: | |
ID: {0} | |
Nombre: {1} | |
Edad: {2} | |
Dirección: {3} | |
[?] Seleccione opción: | |
1. Editar estudiante | |
3. Volver | |
> """.format(student.id, student.name, student.age, student.address)) | |
if option == "1": | |
student.edit_mode() | |
StudentRepository.edit(student) | |
print(""" | |
[✓] Estudiante actualizado: | |
[↺] Volviendo al menú en 2s | |
""") | |
time.sleep(2) | |
system("clear") | |
return menu() | |
if option == "3": | |
system("clear") | |
return menu() | |
menu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment