C# Project: Book Shelf
The Book Shelf project stores books in a list and prints one line per book. It uses a class, properties, a method, and a List together.
Build One Book
The Book class holds the data for one book. Describe builds its line of text.
using System;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
private int pages;
public int Pages
{
get { return pages; }
set
{
if (value >= 1)
{
pages = value;
}
}
}
public string Describe()
{
return Title + " by " + Author + ", " + Pages + " pages";
}
}
class Program
{
static void Main(string[] args)
{
Book book = new Book();
book.Title = "Dune";
book.Author = "Frank Herbert";
book.Pages = 412;
Console.WriteLine(book.Describe());
}
}Pages Setter Guards Values
The setter first takes 200 pages. A later set of 0 is ignored, so 200 stays.
using System;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
private int pages;
public int Pages
{
get { return pages; }
set
{
if (value >= 1)
{
pages = value;
}
}
}
public string Describe()
{
return Title + " by " + Author + ", " + Pages + " pages";
}
}
class Program
{
static void Main(string[] args)
{
Book book = new Book();
book.Title = "Emma";
book.Author = "Jane Austen";
book.Pages = 200;
book.Pages = 0;
Console.WriteLine(book.Pages);
}
}Shelf of Two Books
A List<Book> holds the shelf. The loop prints the Describe line of each book.
using System;
using System.Collections.Generic;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
private int pages;
public int Pages
{
get { return pages; }
set
{
if (value >= 1)
{
pages = value;
}
}
}
public string Describe()
{
return Title + " by " + Author + ", " + Pages + " pages";
}
}
class Program
{
static void Main(string[] args)
{
List<Book> shelf = new List<Book>();
Book first = new Book();
first.Title = "Dune";
first.Author = "Frank Herbert";
first.Pages = 412;
shelf.Add(first);
Book second = new Book();
second.Title = "Emma";
second.Author = "Jane Austen";
second.Pages = 432;
shelf.Add(second);
foreach (Book book in shelf)
{
Console.WriteLine(book.Describe());
}
}
}Example
using System;
using System.Collections.Generic;
class Book
{
public string Title { get; set; }
public string Author { get; set; }
private int pages;
public int Pages
{
get { return pages; }
set
{
if (value >= 1)
{
pages = value;
}
}
}
public string Describe()
{
return Title + " by " + Author + ", " + Pages + " pages";
}
}
class Program
{
static void Main(string[] args)
{
List<Book> shelf = new List<Book>();
Book first = new Book();
first.Title = "Dune";
first.Author = "Frank Herbert";
first.Pages = 412;
shelf.Add(first);
Book second = new Book();
second.Title = "Emma";
second.Author = "Jane Austen";
second.Pages = 432;
shelf.Add(second);
Book third = new Book();
third.Title = "The Hobbit";
third.Author = "J.R.R. Tolkien";
third.Pages = 310;
shelf.Add(third);
foreach (Book book in shelf)
{
Console.WriteLine(book.Describe());
}
Console.WriteLine("Books: " + shelf.Count);
}
}Key points
- A Book object holds a title, an author, and a page count.
- Title and Author are auto-properties with a getter and a setter.
- The Pages setter accepts only values of 1 or more.
- A Pages value below 1 is ignored, so the old value stays.
- Describe returns one line of text about the book.
- A List<Book> holds the books in order.
In this example, three Book objects are built and set one by one. Each book goes onto the shelf list with Add. The foreach loop prints the Describe line of each book in list order. The first line is Dune, the second is Emma, and the third is The Hobbit. The last line prints the Count of the list, which is 3.
Note
The shelf prints in list order. Books appear in the order they were added.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz C# Compiler
Exercise
A book has Pages set to 150, and then Pages is set to -4. What does Pages hold afterwards?