0% found this document useful (0 votes)
11 views

20201216-Task2

The document outlines a class diagram for a library management system, detailing classes such as Book, User, Librarian, Loan, and Catalog, along with their attributes and methods. It also includes test-driven development (TDD) examples using JUnit for each class, demonstrating how to implement and test functionalities like borrowing books, user registration, and managing library operations. The document serves as a comprehensive guide for developing a library system with a focus on TDD practices.

Uploaded by

baderahed21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

20201216-Task2

The document outlines a class diagram for a library management system, detailing classes such as Book, User, Librarian, Loan, and Catalog, along with their attributes and methods. It also includes test-driven development (TDD) examples using JUnit for each class, demonstrating how to implement and test functionalities like borrowing books, user registration, and managing library operations. The document serves as a comprehensive guide for developing a library system with a focus on TDD practices.

Uploaded by

baderahed21
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

20201216/ ‫بدر عاهد الغصين‬

Q1-Class Diagram:

Classes:

1- Book: Represents a book in the library.

-Attributes: title, author, isAvailable

-Methods: borrowBook(), returnBook ()

2- User: Represents library users.

-Attributes: userId, name, email

-Methods: register (), borrowBook (), returnBook ()

3- Librarian: manage library

- Attributes: librarianId, name, shift

- Methods: addBook (), removeBook (), manageUser ()

4- Loan: borrowing books and tracking it.

- Attributes: loanId, book, user, borrowDate, returnDate

- Methods: createLoan (), endLoan ()

5- Catalog: search for books by catalog

- Attributes: books []

- Methods: searchByTitle (), searchByAuthor (), listAvailableBooks ()


Q2-Test-Driven Development:

1- TDD for Class Book:

- Book Class Code:

public class Book {


public String title;
public String author;
private boolean isAvailable;

public Book(String title, String author) {


this.title = title;
this.author = author;
this.isAvailable = true;
}

public boolean borrowBook() {


if (isAvailable) {
isAvailable = false;
return true;
}
return false; }
public boolean returnBook() {
if (!isAvailable) {
isAvailable = true;
return true;
}
return false;
}

public boolean isAvailable() {


return isAvailable;
}
}

- TDD for Book Class Using JUnit:

import static org.junit.Assert.*;


import org.junit.Test;

public class BookTest {


@Test
public void testBorrowBook() {
Book book = new Book("Effective Java", "Joshua Bloch",
"123456789");
assertTrue("Book should be available to borrow",
book.borrowBook());
assertFalse("Book should not be available after borrowing",
book.isAvailable());
}

public void testReturnBook() {


Book book = new Book("Effective Java", "Joshua Bloch",
"123456789");
book.borrowBook();
assertTrue("Book should be returnable", book.returnBook());
assertTrue("Book should be available after returning",
book.isAvailable());
}
}
2- TDD for Class User:

- Class User Code:

public class User {


public String userId;
public String name;
public String email;

public User(String userId, String name, String email) {


this.userId = userId;
this.name = name;
this.email = email;
}

public boolean register() {


return userId!= null && name != null && email != null;
}

public String getName() {


return name;
}
}

- TDD for User Class Using JUnit:

import static org.junit.Assert.*;


import org.junit.Test;
public class UserTest {
@Test
public void testRegisterUser() {
User user = new User("1", "bader ", "[email protected]");
assertTrue(user.register());
}
public void testRegisterUserWithMissingInfo() {
User user = new User(null, "ahmed", "[email protected]");
assertFalse(user.register());
}
public void testGetName() {
User user = new User("232", "jamal", "[email protected]");
assertEquals("John Doe", user.getName());
}
}

3- TDD for Librarian Class:

- Class Librarian Code:

import java.util.ArrayList;
import java.util.List;

public class Librarian {


public String librarianId;
public String name;
public List<Book> books;

public Librarian(String librarianId, String name) {


this.librarianId = librarianId;
this.name = name;
this.books = new ArrayList<>();
}

public boolean addBook(Book book) {


return books.add(book);
}

public boolean removeBook(Book book) {


return books.remove(book);
}

public int getBookCount() {


return books.size();
}
}
- TDD for Librarian Class Using JUnit:

import static org.junit.Assert.*;


import org.junit.Test;

public class LibrarianTest {

public void testAddBook() {


Librarian librarian = new Librarian("321", "bader");
Book book = new Book("java programming", "Robert C. Martin",
"67890");
assertTrue(librarian.addBook(book));
assertEquals(1, librarian.getBookCount());
}
public void testRemoveBook() {
Librarian librarian = new Librarian("L001", "Alice");
Book book = new Book("Clean Code", "Robert C. Martin", "67890");
librarian.addBook(book);
assertTrue(librarian.removeBook(book));
assertEquals(0, librarian.getBookCount()); }}

4- TDD for Class Loan:

- Class Loan Code:

import java.time.LocalDate;

public class Loan {


public String loanId;
public Book book;
public User user;
public LocalDate borrowDate;
public LocalDate returnDate;

public Loan(String loanId, Book book, User user) {


this.loanId = loanId;
this.book = book;
this.user = user;
this.borrowDate = LocalDate.now();
}

public boolean endLoan() {


if (book.isAvailable()) {
return false;
}
book.returnBook();
this.returnDate = LocalDate.now();
return true;
}
}

- TDD for Loan Class Using JUnit:

import static org.junit.Assert.*;


import org.junit.Test;

public class LoanTest {


@Test
public void testCreateLoan() {
Book book = new Book("python programing", "adam", "67890");
User user = new User("1", "bader", "[email protected]");
Loan loan = new Loan("112", book, user);
assertFalse(book.isAvailable());
}

@Test
public void testEndLoan() {
Book book = new Book("python programing ", " adam ", "67890");
User user = new User("1", " bader ", " [email protected]");");
Loan loan = new Loan("112", book, user);
assertTrue(loan.endLoan());
assertTrue(book.isAvailable());
}
}
5- TDD for Class Catalog:

- Class Catalog Code:

import java.util.ArrayList;
import java.util.List;

public class Catalog {


private List<Book> books;

public Catalog() {
books = new ArrayList<>();
}

public void addBook(Book book) {


books.add(book);
}

public List<Book> searchByTitle(String title) {


List<Book> results = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
results.add(book);
}
}
return results;
}
}

- TDD for Catalog Class Using JUnit:

import static org.junit.Assert.*;


import org.junit.Test;

import java.util.List;
public class CatalogTest {
public void testSearchByTitle() {
Catalog catalog = new Catalog();
Book book = new Book("C# programing", "Joshua", "12345");
catalog.addBook(book);
List<Book> results = catalog.searchByTitle("programming");
assertEquals(1, results.size());
}
}

Resources : Test Driven Developmetn for Java using Junit by xentonstack

You might also like