Created
May 21, 2022 12:57
-
-
Save yashtibrewal/0b7a80a9b5a7cfd9e1949fcad6f699df to your computer and use it in GitHub Desktop.
Library System in Go (ZTM Course)
This file contains hidden or 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
//--Summary: | |
// Create a program to manage lending of library books. | |
// | |
//--Requirements: | |
//* The library must have books and members, and must include: | |
// - Which books have been checked out | |
// - What time the books were checked out | |
// - What time the books were returned | |
//* Perform the following: | |
// - Add at least 4 books and at least 3 members to the library | |
// - Check out a book | |
// - Check in a book | |
// - Print out initial library information, and after each change | |
//* There must only ever be one copy of the library in memory at any time | |
// | |
//--Notes: | |
//* Use the `time` package from the standard library for check in/out times | |
//* Liberal use of type aliases, structs, and maps will help organize this project | |
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
//--Requirements: | |
//* The library must have books and members, and must include: | |
// - Which books have been checked out | |
// - What time the books were checked out | |
// - What time the books were returned | |
type Book struct { | |
title string | |
} | |
type Member struct { | |
name string | |
libraryId uint64 | |
} | |
type LendingRecord struct { | |
book *Book | |
issueTime time.Time | |
returnTime time.Time | |
} | |
type Library struct { | |
books map[*Book]uint64 | |
lendingRecords map[*Member]LendingRecord | |
} | |
func checkOutBook(book *Book, member *Member, library *Library) { | |
noOfBooks, hasBook := library.books[book] | |
_, hasIssued := library.lendingRecords[member] | |
if !hasBook || noOfBooks == 0 { | |
fmt.Println("Book is out of stock") | |
} else if hasIssued { | |
fmt.Println("You already have a book issued, please return it before issuing a new book") | |
} else { | |
library.books[book] -= 1 | |
lendToMember := LendingRecord{ | |
book: book, | |
issueTime: time.Now(), | |
} | |
library.lendingRecords[member] = lendToMember | |
fmt.Println("Book ", library.lendingRecords[member].book.title, " has been issued to ", | |
member.name) | |
} | |
} | |
func checkInBook(book *Book, member *Member, library *Library) { | |
_, hasIssued := library.lendingRecords[member] | |
if !hasIssued { | |
fmt.Println("Dear", member.name, ",you have no book to return") | |
} else { | |
library.books[book] += 1 | |
library.lendingRecords[member] = LendingRecord{ | |
book: library.lendingRecords[member].book, | |
issueTime: library.lendingRecords[member].issueTime, | |
returnTime: time.Now(), | |
} | |
fmt.Println("Book ", library.lendingRecords[member].book.title, " has been retruned") | |
} | |
} | |
func printLendingRecord(lendingRecord *LendingRecord) { | |
fmt.Println(lendingRecord.book.title, ":", lendingRecord.issueTime, ",", lendingRecord.returnTime) | |
} | |
func printLibrary(library *Library) { | |
fmt.Println("============Library: Start=============") | |
for book, count := range library.books { | |
fmt.Println(book.title, ":", count) | |
} | |
for member, lendingRecord := range library.lendingRecords { | |
fmt.Println(member.name, ":") | |
printLendingRecord(&lendingRecord) | |
} | |
fmt.Println("============Library: End=============") | |
} | |
//* Perform the following: | |
// - Add at least 4 books and at least 3 members to the library | |
// - Check out a book | |
// - Check in a book | |
// - Print out initial library information, and after each change | |
//* There must only ever be one copy of the library in memory at any time | |
func main() { | |
harryPotter1 := Book{ | |
title: "Harry Potter and the Philosopher's stone", | |
} | |
harryPotter2 := Book{ | |
title: "Harry Potter and the chamber of secrets", | |
} | |
harryPotter3 := Book{ | |
title: "Harry Potter and the prisoner of askaban", | |
} | |
harryPotter4 := Book{ | |
title: "Harry Potter and the goblet of fire", | |
} | |
memeber1 := Member{ | |
name: "Yash Tibrewal", | |
libraryId: 1, | |
} | |
memeber2 := Member{ | |
name: "Kushagra Tibrewal", | |
libraryId: 2, | |
} | |
memeber3 := Member{ | |
name: "Nikhil Tibrewal", | |
libraryId: 3, | |
} | |
library := Library{ | |
books: make(map[*Book]uint64), | |
lendingRecords: make(map[*Member]LendingRecord), | |
} | |
fmt.Println("===========Initializing =================================") | |
printLibrary(&library) | |
library.books[&harryPotter1] = 10 | |
library.books[&harryPotter2] = 4 | |
library.books[&harryPotter3] = 7 | |
library.books[&harryPotter4] = 0 | |
fmt.Println("===========Books Stocked=================================") | |
printLibrary(&library) | |
checkOutBook(&harryPotter4, &memeber1, &library) | |
checkOutBook(&harryPotter1, &memeber2, &library) | |
checkOutBook(&harryPotter1, &memeber3, &library) | |
fmt.Println("===========After Books Issued=================================") | |
printLibrary(&library) | |
checkInBook(&harryPotter4, &memeber1, &library) | |
checkInBook(&harryPotter1, &memeber3, &library) | |
fmt.Println("===========After Books Returned=================================") | |
printLibrary(&library) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment