Menu
CoddyTech

Riepilogo - Sistema di gestione dei dati degli studenti

Fa parte della sezione Programmazione orientata agli oggetti del percorso GDScript di Coddy. Lezione 30 di 65.

challenge icon

Sfida

Medio

Crea un piccolo sistema per la gestione dei dati degli studenti usando due classi.

Student in student.gd mantiene privati i propri dati:

  • _init(id, new_name) memorizza l'ID in una variabile privata e assegna il nome tramite il setter di student_name.
  • student_id è di sola lettura e restituisce l'ID.
  • student_name ha un setter: un nome più corto di 2 caratteri viene rifiutato (il vecchio nome resta) e last_error diventa Name must be at least 2 characters. Un nome valido cancella last_error.
  • add_grade(course, grade) memorizza il voto per il corso in un Dictionary privato (un nuovo voto per lo stesso corso sostituisce quello precedente) e restituisce true. Un voto inferiore a 0 o superiore a 100 non viene memorizzato e restituisce false.
  • average è una proprietà calcolata di sola lettura: la media dei voti come float, oppure 0.0 se non ci sono voti.
  • course_count() restituisce il numero di corsi con un voto.

StudentRegistry in student_registry.gd conserva gli studenti in un Dictionary privato, usando l'ID come chiave:

  • add_student(student) restituisce false e non modifica nulla se l'ID è già occupato; altrimenti memorizza lo studente e restituisce true.
  • remove_student(id) restituisce se uno studente è stato rimosso.
  • get_student(id) restituisce lo studente oppure null.
  • top_student() restituisce lo studente con la media più alta (in caso di parità, quello aggiunto per primo) oppure null se il registro è vuoto.
  • get_ids() restituisce gli ID nell'ordine in cui sono stati aggiunti gli studenti.

Il codice fornito legge il numero di comandi, poi esegue un comando per riga: add ID NAME, grade ID COURSE GRADE, rename ID NAME, remove ID, show ID, tamper ID (prova a sovrascrivere student_id e average, che non devono cambiare), top e list. Per questo input:

10
add 1001 Alice Smith
add 1002 Bob
grade 1001 Math 90
grade 1001 Art 85
grade 1002 Math 95
grade 1002 Art 120
rename 1002 B
show 1002
top
list

stampa:

Added Alice Smith (1001)
Added Bob (1002)
Alice Smith: Math = 90
Alice Smith: Art = 85
Bob: Math = 95
Invalid grade: 120
Rejected: Name must be at least 2 characters
ID: 1002, Name: Bob, Courses: 1, Average: 95.00
Top: Bob (95.00)
ID: 1001, Name: Alice Smith, Courses: 2, Average: 87.50
ID: 1002, Name: Bob, Courses: 1, Average: 95.00

Il tuo codice va inserito in student.gd e student_registry.gd. main.gd contiene il codice fornito per l'input/output e non può essere modificato.

Provalo tu

extends Node

func _ready():
	# Codice di input/output fornito: lascialo così com'è
	var registry := StudentRegistry.new()
	var count := int(OS.read_string_from_stdin())
	for i in count:
		var parts := OS.read_string_from_stdin().split(" ", true, 2)
		var command := parts[0]
		if command == "top":
			var best := registry.top_student()
			if best == null:
				print("Top: none")
			else:
				print("Top: %s (%.2f)" % [best.student_name, best.average])
			continue
		if command == "list":
			for listed_id in registry.get_ids():
				print_student(registry.get_student(listed_id))
			continue
		var id := int(parts[1])
		if command == "add":
			var student := Student.new(id, parts[2])
			if student.last_error != "":
				print("Rejected: " + student.last_error)
			elif registry.add_student(student):
				print("Added %s (%d)" % [student.student_name, id])
			else:
				print("Duplicate ID: %d" % id)
			continue
		if command == "remove":
			print(("Removed %d" if registry.remove_student(id) else "No student %d") % id)
			continue
		var student := registry.get_student(id)
		if student == null:
			print("No student %d" % id)
		elif command == "grade":
			var grade_parts := parts[2].split(" ")
			if student.add_grade(grade_parts[0], int(grade_parts[1])):
				print("%s: %s = %s" % [student.student_name, grade_parts[0], grade_parts[1]])
			else:
				print("Invalid grade: " + grade_parts[1])
		elif command == "rename":
			student.student_name = parts[2]
			if student.last_error != "":
				print("Rejected: " + student.last_error)
			else:
				print("Renamed to " + student.student_name)
		elif command == "tamper":
			# Tenta di sovrascrivere le proprietà di sola lettura
			student.student_id = 0
			student.average = 100.0
			print_student(student)
		else:
			print_student(student)

func print_student(student: Student) -> void:
	print("ID: %d, Name: %s, Courses: %d, Average: %.2f" % [student.student_id, student.student_name, student.course_count(), student.average])

Tutte le lezioni di Programmazione orientata agli oggetti

Esercitati da solo: Compilatore GDScript online