I'm trying to learn coding by actually coding my first working and hopefully expandable in future app. Yesterday I asked about optimizing my menu and got some answers about my take on it. Obviously and as expected I got picked bout my casing, code structure and naming. I fully understand and accept criticism as my goal is to learn from more experienced coders.
I want to create this "question" as I'd love to get some feedback about my code. I especially would love some feedback about my access modifiers and usage (actually lack of) of static methods. I also think that I'm trying too much to separate my code into single class files.
BOOK.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Library
{
public class Book
{
public string BookName
{
get; set;
}
public string BookAuthor
{
get; set;
}
public int ReleaseDate
{
get; set;
}
public bool available = true;
public Book(string name, string author, int releaseDate)
{
BookName = name;
BookAuthor = author;
ReleaseDate = releaseDate;
}
}
}
BOOKLIST.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
List to store books
*/
namespace Library
{
class BookList
{
List<Book> books = new List<Book>();
public void addBook(string bookName, string bookAuthor, int releaseDate) // adding book
{
books.Add(new Book(bookName, bookAuthor, releaseDate));
}
public void deleteBook(string name) // deleting book
{
books.RemoveAll(Book => Book.BookName == name);
}
}
}
I'm aware that I abandoned few of methods, I decided to concentrate on quality instead of quantity.
PROGRAM.CS - Main method to start app
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
Simple library program that handles small library with adding/deleting/borrowing/returning/prolinging books. All books are stored in list and objects' statuses can be saved and read to/from textfile.
Right now it is considered to be a console app, but in near future I'll rewrite it for window app.
*/
namespace Library
{
class Program
{
static void Main(string[] args)
{
UserInterface LibraryWindow = new UserInterface();
LibraryWindow.startApp(400,200);
}
}
}
USERINTERFACE.CS - I tried to separate user input logic from methods themselves. That was one of main problems most users here pointed out.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Library
{
interface ICommand
{
string Description { get; }
void Execute(BookList bookList);
}
class AddBookCommand : ICommand
{
public string Description => "Add a book.";
public void Execute(BookList books)
{
Console.Clear();
Console.WriteLine("Please enter name of the book.");
string Name = Console.ReadLine();
Console.WriteLine("Please enter author of the book.");
string Author = Console.ReadLine();
Console.WriteLine("Please enter date of release.");
string ReleaseDateString = Console.ReadLine();
int ReleaseDate;
do
{
bool TryToParseDate = int.TryParse(ReleaseDateString, out ReleaseDate);
if (TryToParseDate == false)
{
Console.WriteLine("Wrong value, please enter correct year.");
ReleaseDateString = Console.ReadLine();
}
else
{
bool CorrectDate = Enumerable.Range(0, DateTime.Today.Year).Contains(int.Parse(ReleaseDateString));
if (CorrectDate)
{
ReleaseDate = int.Parse(ReleaseDateString);
break;
}
else
{
Console.WriteLine("Wrong value, please enter correct year.");
ReleaseDateString = Console.ReadLine();
}
}
}
while (true);
books.addBook(Name, Author, ReleaseDate);
Console.WriteLine("Book added.");
}
}
class DeleteBookCommand : ICommand
{
public string Description => "Delete book.";
public void Execute(BookList books)
{
Console.Clear();
Console.WriteLine("Please enter name of the book.");
string Name = Console.ReadLine();
books.deleteBook(Name);
Console.WriteLine("Book deleted.");
}
}
class ExitCommand : ICommand
{
public string Description => "Exit.";
public void Execute(BookList books) { Environment.Exit(0); }
}
class UserInterface
{
public void startApp(int sizeX, int sizeY)
{
this.mainMenu();
}
public void mainMenu()
{
var commands = new ICommand[]
{
new AddBookCommand(),
new DeleteBookCommand(),
new ExitCommand()
};
var books = new BookList();
while (true)
{
Console.WriteLine("Welcome to Library.");
Console.WriteLine("What do you want to do?");
for (int i = 0; i < commands.Length; i++)
{
Console.WriteLine("{0}. {1}", i + 1, commands[i].Description);
}
var userChoice = string.Empty;
var commandIndex = -1;
do
{
userChoice = Console.ReadLine();
}
while (!int.TryParse(userChoice, out commandIndex) || commandIndex > commands.Length);
commands[commandIndex - 1].Execute(books);
}
}
}
}