Menu
Coddy logo textTech

Project Overview & Structure

Part of the Object Oriented Programming section of Coddy's GO journey — lesson 98 of 107.

challenge icon

Challenge

Easy

Let's kick off the Library Management System project by setting up the foundational structure! In this first step, you'll create the basic scaffolding that will support all the features we'll build in upcoming lessons.

You'll organize your code across three files to establish clean separation of concerns:

  • book.go: Define a Book struct that represents a library book. Each book should have an ID, Title, Author, and Available status (boolean). Create a constructor function NewBook that takes the ID, title, and author as parameters and returns a pointer to a new Book with Available set to true by default. Also add a String() method that returns the book's information in the format: [ID] Title by Author
  • user.go: Define a User struct representing a library member with ID, Name, and Email fields. Create a constructor function NewUser that takes all three values and returns a pointer to a new User. Add a String() method that returns: User: Name (Email)
  • main.go: Bring everything together by reading input to create books and users, then display their information.

    Read the number of books to create. For each book, read its ID, title, and author on separate lines. Then read the number of users, and for each user read their ID, name, and email on separate lines.

    After creating all items, print each book's string representation on its own line, followed by each user's string representation on its own line.

The following inputs will be provided:

  • Number of books
  • For each book: ID, title, and author (each on separate lines)
  • Number of users
  • For each user: ID, name, and email (each on separate lines)

For example, given:

2
B001
The Go Programming Language
Alan Donovan
B002
Clean Code
Robert Martin
1
U001
Alice Smith
alice@library.com

Your output should be:

[B001] The Go Programming Language by Alan Donovan
[B002] Clean Code by Robert Martin
User: Alice Smith (alice@library.com)

And given:

1
B100
Design Patterns
Gang of Four
2
U001
Bob Jones
bob@email.com
U002
Carol White
carol@email.com

Your output should be:

[B100] Design Patterns by Gang of Four
User: Bob Jones (bob@email.com)
User: Carol White (carol@email.com)

This foundation establishes the core data models you'll expand throughout the project. In the next lesson, you'll enhance these structs and add the borrowing functionality!

Try it yourself

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	// Read number of books
	numBooksStr, _ := reader.ReadString('\n')
	numBooks, _ := strconv.Atoi(strings.TrimSpace(numBooksStr))

	// TODO: Create a slice to store books

	// TODO: Read each book's information (ID, title, author on separate lines)
	// and create Book objects using NewBook
	for i := 0; i < numBooks; i++ {
		id, _ := reader.ReadString('\n')
		id = strings.TrimSpace(id)
		title, _ := reader.ReadString('\n')
		title = strings.TrimSpace(title)
		author, _ := reader.ReadString('\n')
		author = strings.TrimSpace(author)

		// TODO: Create a new book and add it to your slice
	}

	// Read number of users
	numUsersStr, _ := reader.ReadString('\n')
	numUsers, _ := strconv.Atoi(strings.TrimSpace(numUsersStr))

	// TODO: Create a slice to store users

	// TODO: Read each user's information (ID, name, email on separate lines)
	// and create User objects using NewUser
	for i := 0; i < numUsers; i++ {
		id, _ := reader.ReadString('\n')
		id = strings.TrimSpace(id)
		name, _ := reader.ReadString('\n')
		name = strings.TrimSpace(name)
		email, _ := reader.ReadString('\n')
		email = strings.TrimSpace(email)

		// TODO: Create a new user and add it to your slice
	}

	// TODO: Print each book's string representation

	// TODO: Print each user's string representation
	fmt.Println("TODO: Print output here")
}

All lessons in Object Oriented Programming