I have just started my adventure with C#. Below you can find a code written for my school assigment.
The homework task had 3 major elements:
- building a class book
- creating a metod for changing the static genre value and
- creating a way to find specific object by properties.
All those elements are included.
I hope that more experienced members of community will have a short look at my code and share some tips and/or good practices, so I may improve.
class Book
{
public string title { get; set; }
public string Author { get; set; }
public string publisher { get; set; }
public int price { get; set; }
public static string genre;
public override string ToString()
{
string output = String.Format("Title :{0} \n Author :{1} \n Publisher :{2} \n Price {3}", this.title, this.Author, this.publisher, this.price);
return output;
}
public static List<Book> myList = new List<Book>();
private static void changegenre()
{
string mygenre = "Fantasy";
genre = mygenre;
}
static void Main(string[] args)
{
changegenre();
myList.Add(new Book() { title = "Hobbit", Author = "J.R.R Tolkien", publisher = "U & G", price = 34, });
myList.Add(new Book() { title = "Two Towers", Author = "J.R.R Tolkien", publisher = "U & G", price = 55 });
myList.Add(new Book() { title = "Opowiesci z meekhanskiego pogranicza", Author = "Robert M. Wegner", publisher = "Powergraph", price = 10 });
myList.Add(new Book() { title = "A Dance with Dragons", Author = "G.R.R Martin", publisher = "Voyager Books", price = 25 });
Console.WindowWidth = 95;
Console.BackgroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine(" --------Welcome to our bookstore!-------");
do
{
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Please write: \r\n a = if you want to search book by author, \r\n p = if you want to search book by price, \r\n pub - if you want to search by publisher or \r\n t- if you want to search by title");
string input = Console.ReadLine();
switch (input)
{
case "a":
searchbyauthor();
break;
case "p":
searchbyprice();
break;
case "pub":
searchbypublisher();
break;
case "t":
searchbytitle();
break;
}
Console.WriteLine("Do you want to search again?");
} while (Console.ReadLine().ToUpper() == "YES");
}
private static void searchbytitle()
{
Console.WriteLine("Please, write book's title!");
string input = Console.ReadLine();
foreach (Book result in myList.Where(x => x.title == input).ToList())
{
Console.WriteLine(result);
}
}
private static void searchbypublisher()
{
Console.WriteLine("Please, write publisher's name!");
string input = Console.ReadLine();
foreach (Book result in myList.Where(x => x.publisher == input).ToList())
{
Console.WriteLine(result);
}
}
private static void searchbyprice()
{
Console.WriteLine("Please, give me the maximum book price");
string input = Console.ReadLine();
int maxprice = Convert.ToInt32(input);
foreach (Book result in myList.Where(x => x.price <= maxprice).ToList())
{
Console.WriteLine(result);
}
}
private static void searchbyauthor()
{
Console.WriteLine("Please, write author's name!");
string input = Console.ReadLine();
foreach (Book result in myList.Where(x => x.Author == input).ToList())
{
Console.WriteLine(result);
}
}
}