{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f1d96317",
   "metadata": {},
   "source": [
    "```{index} homework; booklist\n",
    "```\n",
    "\n",
    "(booklist)=\n",
    "\n",
    "# Homework: Book List\n",
    "\n",
    "**Objectives**:\n",
    "\n",
    "- Complete a simple data storing class (Book), with fields for title, author, and\n",
    "  year of publication.\n",
    "- Complete a class with a Collection (BookList) that uses the public methods\n",
    "  of another class you wrote (Book), and select various data from the list.\n",
    "- Complete a testing program (TestBookList), that creates a\n",
    "  BookList, adds Books to the BookList, and tests BookList\n",
    "  methods clearly and completely for a user looking at the output\n",
    "  of the program and *not* the source code..\n",
    "\n",
    "Copy stub files from the project [books_homework_stub](https://github.com/mstbit/introcs-csharp-examples/tree/master/books_homework_stub) to your own\n",
    "project.\n",
    "Stubs for the assignment files are\n",
    "[book.cs](https://github.com/mstbit/introcs-csharp-examples/blob/master/books_homework_stub/book.cs),\n",
    "[book_list.cs](https://github.com/mstbit/introcs-csharp-examples/blob/master/books_homework_stub/book_list.cs), and\n",
    "[test_book_list.cs](https://github.com/mstbit/introcs-csharp-examples/blob/master/books_homework_stub/test_book_list.cs),\n",
    "\n",
    "Some of the method stubs included are only to be fleshed out if you are doing\n",
    "the corresponding extra credit option. They include a comment,\n",
    "just inside the method, `// code for extra credit`.\n",
    "There are also extra files used by the extra credit portion.\n",
    "They are discussed\n",
    "in the Extra Credit section at the end of the assignment.\n",
    "\n",
    "Complete the first line in each file to show your names. At the top of\n",
    "the Book class include any comments about help in *all* of the classes.\n",
    "\n",
    "Create methods one at a time, and test them. Complete {file}`book.cs` first,\n",
    "preferably testing along the way. (You can write an initial version of the\n",
    "testing program, so it does not depend on BookList.) Then add methods to\n",
    "{file}`book_list.cs`, and concurrently add and run tests in {file}`test_book_list.cs`.\n",
    "Testing the Book class first means that when you get to\n",
    "BookList you can have more confidence that any problems you have are from the latest\n",
    "part you wrote, not parts written earlier in the class Book.\n",
    "\n",
    "Remember to have each individual submit a **log**, {file}`log.txt`,\n",
    "in the same format as the last assignment.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23779a33",
   "metadata": {},
   "source": "## Book class\n\nSee the stub file provided. It should have instance fields for the\nauthor, title, and year (published).\n\nComplete the constructor:\n\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "public Book(string title, string author, int year)\n"
   ],
   "id": "51964313"
  },
  {
   "cell_type": "markdown",
   "id": "3ecaeea7",
   "metadata": {},
   "source": [
    "\n",
    "that initializes the fields.\n",
    "(Be careful, as we have discussed in class, when using the same names\n",
    "for these parameters as the instance variables!)\n",
    "\n",
    "It should have three standard (one line) getter methods:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "public string GetTitle()\n",
    "\n",
    "public string GetAuthor()\n",
    "\n",
    "public int GetYear()\n"
   ],
   "id": "4d87aecc"
  },
  {
   "cell_type": "markdown",
   "id": "a31b65be",
   "metadata": {},
   "source": [
    "\n",
    "and\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "public override string ToString()\n"
   ],
   "id": "3e92bac9"
  },
  {
   "cell_type": "markdown",
   "id": "33943116",
   "metadata": {},
   "source": [
    "\n",
    "`ToString` should return a *single* string spread across three lines,\n",
    "with no newline at the end.\n",
    "For example if the `Book` fields were\n",
    "\"C# Yellow Book\", \"Rob Miles\", and 2011, the string should appear,\n",
    "when printed, as\n",
    "\n",
    "```none\n",
    "Title: C# Yellow Book\n",
    "Author: Rob Miles\n",
    "Year: 2011\n",
    "```\n",
    "\n",
    "The `override` in the heading is important so the compiler knows that this\n",
    "is the official method for the system to used implicitly to convert the object\n",
    "to a string.\n",
    "\n",
    "Remember the use of @ with multi-line string literals.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "233899f6",
   "metadata": {},
   "source": "## BookList class\n\nIt has just one instance variable, already declared:\n\n"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "private List<Book> list;\n"
   ],
   "id": "381e6b8d"
  },
  {
   "cell_type": "markdown",
   "id": "19546e3e",
   "metadata": {},
   "source": [
    "\n",
    "It has a constructor (already written - creating an empty List):\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "public BookList()\n"
   ],
   "id": "808f9e84"
  },
  {
   "cell_type": "markdown",
   "id": "519ada68",
   "metadata": {},
   "source": [
    "\n",
    "It should have public methods:\n",
    "\n",
    "```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    ":end-before: '{'\n",
    ":start-after: AddBook chunk\n",
    "```\n",
    "\n",
    "The regular version should just leave the final `return true;`\n",
    "The extra credit version is more elaborate.\n",
    "\n",
    "Further methods:\n",
    "\n",
    "```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    ":end-before: '{'\n",
    ":start-after: PrintList chunk\n",
    "```\n",
    "\n",
    "```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    ":end-before: '{'\n",
    ":start-after: PrintTitlesByAuthor chunk\n",
    "```\n",
    "\n",
    "```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    ":end-before: '{'\n",
    ":start-after: PrintBooksInYears chunk\n",
    "```\n",
    "\n",
    "For instance if the list included books published in 1807, 1983, 2004,\n",
    "1948, 1990, and 2001, the statement\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "PrintBooksInYears(1940, 1990);\n"
   ],
   "id": "0d8a89ab"
  },
  {
   "cell_type": "markdown",
   "id": "074c742e",
   "metadata": {},
   "source": [
    "\n",
    "would list the books from 1983, 1948, and 1990.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1602d97b",
   "metadata": {},
   "source": "## TestBookList class\n\nIt should have a `Main` program that creates a BookList, adds some books\nto it (more than in the skeleton!), and convincingly displays tests of\neach of BookList's methods that exercise all paths through your code.\nCheck for one-off errors in PrintBookYears. With all the methods that\nprint something, the results are easy to see. *Do print a label*, as in\nthe skeleton, before printing output from each method test, so that the\nuser of the program can see the correctness of the test\n*without any knowledge of the source code*!"
  },
  {
   "cell_type": "markdown",
   "id": "7935447e",
   "metadata": {},
   "source": "## Grading Rubric\n\nBook class\n\n- [1 point] public Book(string title, string author, int year)\n- [1] public string GetTitle()\n- [1] public string GetAuthor()\n- [1] public int GetYear()\n- [2] public override string ToString()\n\nBookList class\n\n- [2] public bool AddBook(Book book)\n- [2] public void PrintList()\n- [2] public void PrintTitlesByAuthor(string author)\n- [2] public void PrintBooksInYears(int firstYear, int lastYear)\n\nTestBookList\n\n- [2] Supply data to screen indicating what test is being done with what\n  data and what results, so it is clear that each test works without\n  looking at the source code.\n- [5] Convincingly display tests of each of BookList's methods that\n  exercise all paths through your code.\n\nOverall:\n\n- [4] Make your code easy to read - follow indenting standards, use\n  reasonable identifier names.... Do not duplicate code when\n  you could call a method already written."
  },
  {
   "cell_type": "markdown",
   "id": "a22f1447",
   "metadata": {},
   "source": "## Extra Credit\n\nYou may do any of the numbered options, except that the last one\nrequires you to do the previous one first.\n\nTo get full credit for any particular option, tests for it must be\n*fully integrated* into TestBookList!\n\n1. [2 points] Complete\n\n   ```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n   :end-before: '{'\n   :start-after: ToString chunk\n   ```\n\n   Also *change* the `PrintList` method body to the one line:"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "   Console.Write(this);\n"
   ],
   "id": "d4d261b1"
  },
  {
   "cell_type": "markdown",
   "id": "79a833cb",
   "metadata": {},
   "source": [
    "\n",
    "   (The `Write` and `WriteLine` methods print objects by using their\n",
    "   `ToString` methods.)\n",
    "\n",
    "   Be sure to make this addition to TestBookList:\n",
    "   Test the `ToString` method by converting the\n",
    "   resulting BookList description string to upper case before printing it\n",
    "   (which should produce a different result than the regular mixed case of\n",
    "   the `PrintList` method test).\n",
    "\n",
    "2. [4 points]\n",
    "\n",
    "   In the Book class, a new constructor:\n",
    "\n",
    "   ```{literalinclude} ../../examples/books_homework_stub/book.cs\n",
    "   :end-before: '{'\n",
    "   :start-after: Book chunk\n",
    "   ```\n",
    "\n",
    "   In class BookList, a new constructor:\n",
    "\n",
    "   ```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    "   :end-before: '{'\n",
    "   :start-after: BookList chunk\n",
    "   ```\n",
    "\n",
    "   For testing we included special files in the right format:\n",
    "   [books_homework_stub/books.txt](https://github.com/mstbit/introcs-csharp-examples/blob/master/books_homework_stub/books.txt) and [books_homework_stub/morebooks.txt](https://github.com/mstbit/introcs-csharp-examples/blob/master/books_homework_stub/morebooks.txt).\n",
    "\n",
    "   You will also want to include a reference to [fio/fio.cs](https://github.com/mstbit/introcs-csharp-examples/blob/master/fio/fio.cs), so the text files are\n",
    "   easy to find.\n",
    "\n",
    "3. [4 points]\n",
    "\n",
    "   In class Book:\n",
    "\n",
    "   ```{literalinclude} ../../examples/books_homework_stub/book.cs\n",
    "   :start-after: IsEqual chunk\n",
    "   ```\n",
    "\n",
    "   It is essential to have the `IsEqual` method working in Book before any of\n",
    "   the new code in BookList, which all depends on the definition of `IsEqual`\n",
    "   for a Book.\n",
    "\n",
    "   NOTE: We chose the name `IsEqual` to distinguish it from the\n",
    "   more general `Equals` override that you could write.\n",
    "   The `Equals` override allows for a parameter of any object type. With\n",
    "   skills from Comp 271 you you be able to write the `Equals` override.\n",
    "\n",
    "   In class BookList:\n",
    "\n",
    "   ```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    "   :end-before: '{'\n",
    "   :start-after: Contains chunk\n",
    "   ```\n",
    "\n",
    "   Caution: Do NOT try to use the `List` method `Contains`: Because we\n",
    "   did *not* override the `Equals` method to specialize it for Books, the `List`\n",
    "   method `Contains` will *fail*. You need to do a litle bit more\n",
    "   work and write your own version with a loop.\n",
    "\n",
    "   Change the `AddBook` method from the regular assignment, so it\n",
    "   satisfies this documentation:\n",
    "\n",
    "   ```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    "   :end-before: '}'\n",
    "   :start-after: Revised AddBook\n",
    "   ```\n",
    "\n",
    "   In TestBookList you need to react to the return value, too.\n",
    "\n",
    "4. [2 points] This one requires the previous elaboration of `AddBook`.\n",
    "   In BookList:\n",
    "\n",
    "   ```{literalinclude} ../../examples/books_homework_stub/book_list.cs\n",
    "   :end-before: '{'\n",
    "   :start-after: AddAll chunk\n",
    "   ```\n",
    "\n",
    "   You might want to code it first without worrying about the correct\n",
    "   return value; then do the complete version. There are multiple\n",
    "   approach to determining the return value, some much easier than\n",
    "   others!\n",
    "\n",
    "   To fully test in TestBookList, you need to react to the return value, too."
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}