Menu
CoddyTech

Project Overview

Part of the Object Oriented Programming section of Coddy's GDScript journey. Lesson 56 of 65.

challenge icon

Challenge

Medium

In this project you build a Library Management System over six steps: a library, books and users, borrowing, search, statistics and returns. Every step starts from your files of the previous step.

This step creates the Library class in library.gd. The starter already declares its three properties:

  • library_name: the name of the library
  • books and users: two Array[String] that start empty. For now they hold titles and user names.

Complete the two methods:

  1. The constructor _init(library_name) stores the name in the property library_name.
  2. _to_string() returns NAME Library (books: N, users: M), where N and M are the sizes of the two arrays.

The supplied code reads the library name from the first line, then one command per line: book TITLE and user NAME append to the arrays, info prints the three properties and show prints the library. For this input:

Community
info
book Dune
user Ada
show

the output is:

Name: Community
Books: 0
Users: 0
Community Library (books: 1, users: 1)

Your code goes in library.gd. main.gd holds the supplied input/output code and cannot be edited.

Try it yourself

extends Node

func _ready():
	# Supplied input/output code: keep it as it is
	var library := Library.new(OS.read_string_from_stdin())
	var line := OS.read_string_from_stdin()
	while line != "":
		var parts := line.split(" ", true, 1)
		match parts[0]:
			"book":
				library.books.append(parts[1])
			"user":
				library.users.append(parts[1])
			"info":
				print("Name: ", library.library_name)
				print("Books: ", library.books.size())
				print("Users: ", library.users.size())
			"show":
				print(library)
		line = OS.read_string_from_stdin()

All lessons in Object Oriented Programming

Practice on your own: Online GDScript compiler