Copy and paste this code to answer the question. for the question, just click the title.
using System;
using System.Collections.Generic;
using System.Text;
namespace Exam01
{
class Program
{
static void Main(string[] args)
{
Book book1 = new Book();
book1.Type = BookType.Science;
book1.Pages = 150;
book1.Title = "Practical Science";
book1.Author = "Professor X1"; // Displays an error since the author name has a digit
Console.WriteLine(book1.Author); // Displays 'Unknown' since the author field is blank
book1.Author = "Professor X";
Console.WriteLine(book1); // Display's 'Type: Science, Title: Practical Science, Author: Professor X, Pages: 150'
Book book2 = new Book(BookType.History, "World History", "Magellan");
book2.Borrow(); // Sets the book status to borrowed
Console.WriteLine(book2.IsBorrowed); // Displays true
book2.Borrow(); // Displays an error since book is already borrowed
book2.Return(); // Sets the book status to available
Console.WriteLine(book2.IsBorrowed); // Displays false
book2.Return(); // Displays an error since book is already returned
Console.ReadLine();
}
}
public enum BookType
{
Undefined,
Fiction,
NonFiction,
History,
Science
}
public enum StatusType
{
Undefined,
Available,
Borrowed
}
public class Book
{
#region "fields"
private int _pages;
private string _title;
private string _author;
private BookType _bookType;
private StatusType _statusType;
#endregion
#region "Constructors"
public Book(BookType bookType, string title, string author, int pages)
{
_pages = pages;
_title = title;
_author = author;
_bookType = bookType;
_statusType = StatusType.Available;
}
public Book(BookType bookType, string title, string author)
{ }
public Book(string title,string author)
{ }
public Book()
{ }
#endregion
#region "Propertys"
public string Title
{
get{return _title;}
set{_title = value;}
}
public string Author
{
get
{
if (_author == "")
{
return "'Unknown' since the author field is blank";
}
else
return _author;
}
set
{
if (value.Contains("1") || value.Contains("2") || value.Contains("3") || value.Contains("4") || value.Contains("5") || value.Contains("6") || value.Contains("7") || value.Contains("8") || value.Contains("9") || value.Contains("0"))
{
Console.WriteLine("error since the author name has a digit");
_author = "";
}
else
_author = value;
}
}
public int Pages
{
get { return _pages; }
set { _pages = value; }
}
public BookType Type
{
get { return _bookType; }
set { _bookType = value; }
}
public bool IsBorrowed
{
get
{
if (_statusType == StatusType.Borrowed)
return true;
else
return false;
}
}
#endregion
#region "methods"
public void Borrow()
{
if (IsBorrowed == true)
Console.WriteLine("error since book is already borrowed");
else
_statusType = StatusType.Borrowed;
}
public void Return()
{
if(_statusType==StatusType.Available)
Console.WriteLine("error since book is already returned");
else
_statusType = StatusType.Available;
}
#endregion
public override string ToString()
{
return "Type: "+_bookType+", Title: "+_title+", Author: "+_author+", Pages: "+_pages;
}
}
}
No comments:
Post a Comment