Exemplo n.º 1
0
        static void RentBook(LibraryContext context)
        {
            User userToRent = null;

            Console.WriteLine("╔═════════════╗");
            Console.WriteLine("║ BOOK RENTAL ║");
            Console.WriteLine("╚═════════════╝");

            do
            {
                Console.Write("Enter user's login: "******"Error: User with login " + login + " does not exist in the database. Try again.");
                }
                else if (userToRent.Role == User.RoleType.Administrator)
                {
                    Console.WriteLine("Error: Books cannot be rented to administrators. To rent a book register as a regular user or use another account.\n");
                }
            } while (userToRent == null || userToRent.Role == User.RoleType.Administrator);

            Console.WriteLine("---------------------------------------------");

            foreach (var b in context.Books.Where(b => !b.Rented).ToList())
            {
                Author author = context.Authors.Single(a => a.Id == b.AuthorId);
                Console.WriteLine(b.ISBN + "\t\t| \"" + b.Title + "\" " + author.FirstName + " " + author.LastName);
            }

            Book bookToRent = null;

            do
            {
                Console.Write("\nEnter ISBN of the book for rental: ");
                string ISBN = Console.ReadLine();

                bookToRent = context.Books.SingleOrDefault(b => b.ISBN == ISBN);

                if (bookToRent == null)
                {
                    Console.WriteLine("Error: Invalid ISBN: " + ISBN + ". Try again.");
                }
                else if (bookToRent.Rented)
                {
                    Console.WriteLine("Error: \"" + bookToRent.Title + "\" is already rented.");
                }
            } while (bookToRent == null || bookToRent.Rented);

            bookToRent.RentedDate = DateTime.Now;
            bookToRent.ReturnTerm = bookToRent.RentedDate.Value.AddDays(2);
            bookToRent.Rented     = true;

            if (userToRent.RentedBooks == null)
            {
                userToRent.RentedBooks = new List <Book>();
            }

            userToRent.RentedBooks.Add(bookToRent);
            context.SaveChanges();

            Console.WriteLine("\"" + bookToRent.Title + "\" has been successfully rented to user " + userToRent.Login + ".\n");
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
            Console.Clear();
        }
Exemplo n.º 2
0
        static void Prolong(User loggedUser, LibraryContext context)
        {
            Console.WriteLine("╔═════════╗");
            Console.WriteLine("║ PROLONG ║");
            Console.WriteLine("╚═════════╝");

            context.Books.ToList();

            if (loggedUser.RentedBooks == null)
            {
                Console.WriteLine("You don't have any rented books.");
            }
            else
            {
                foreach (var b in loggedUser.RentedBooks.ToList())
                {
                    Author author = context.Authors.Single(a => a.Id == b.AuthorId);
                    if (b.ReturnTerm.Value.Day - DateTime.Now.Day <= 3 && b.ReturnTerm.Value.Month == DateTime.Now.Month)
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                    }
                    if (DateTime.Compare(b.ReturnTerm.Value, DateTime.Now) == -1)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                    }
                    Console.WriteLine(b.ISBN + "\t\"" + b.Title + "\" " + author.FirstName + " " + author.LastName + "\tRented: " + b.RentedDate.Value.ToShortDateString() + "\tReturn term: " + b.ReturnTerm.Value.ToShortDateString());
                    Console.ForegroundColor = ConsoleColor.Gray;
                }

                Book bookToProlong = null;

                do
                {
                    Console.Write("\nEnter ISBN of the book's rental you want to prolong: ");
                    string ISBN = Console.ReadLine();

                    bookToProlong = loggedUser.RentedBooks.SingleOrDefault(book => book.ISBN == ISBN);

                    if (bookToProlong == null)
                    {
                        Console.WriteLine("ERROR: Invalid ISBN: " + ISBN + ". Try again.");
                    }
                    else if (bookToProlong.Prolonged)
                    {
                        Console.WriteLine("Error: Cannot prolong rental of the same book twice.");
                        Console.Write("Prolong rental of another book?\n[Y]\t[N] No\n> ");
                        if (Console.ReadKey().KeyChar.ToString().ToLower() == "n")
                        {
                            Console.Clear();
                            return;
                        }
                    }
                } while (bookToProlong == null || bookToProlong.Prolonged);

                bookToProlong.Prolonged  = true;
                bookToProlong.ReturnTerm = bookToProlong.ReturnTerm.Value.AddDays(14);

                Console.WriteLine("Prolonged rental of \"" + bookToProlong.Title + "\" by 14 days.");

                context.SaveChanges();
            }

            Console.WriteLine("\nPress any key to continue.");
            Console.ReadKey();
            Console.Clear();
        }
Exemplo n.º 3
0
        static User LogIn(LibraryContext context)
        {
            Console.WriteLine("╔════════════╗");
            Console.WriteLine("║ LOGGING IN ║");
            Console.WriteLine("╚════════════╝");

            string login, pass;
            User   loggedUser = null;

            do
            {
                do
                {
                    Console.Write("Login: "******"Error: Login is required! Try again.");
                    }
                } while (string.IsNullOrEmpty(login));

                do
                {
                    Console.Write("Password: "******"Error: Password is required! Try again.");
                    }
                } while (string.IsNullOrEmpty(pass));



                foreach (var user in context.Users)
                {
                    if (user.Login == login && user.Password == pass)
                    {
                        loggedUser = user;
                        break;
                    }
                }

                if (loggedUser == null)
                {
                    Console.Write("Error: Login or password is incorrect.\nDo you want to try again?\n[Y]\t[N] No\n> ");
                    if (Console.ReadKey().KeyChar.ToString().ToLower() == "n")
                    {
                        break;
                    }
                    else
                    {
                        Console.WriteLine();
                    }
                }
            } while (loggedUser == null);

            if (loggedUser != null)
            {
                Console.WriteLine("\nLogged in.");
                Console.WriteLine("\nPress any key to continue.");
                Console.ReadKey();
                Console.Clear();
                return(loggedUser);
            }
            else
            {
                Console.WriteLine("\nLogging in cancelled.");
                Console.WriteLine("\nPress any key to continue.");
                Console.ReadKey();
                Console.Clear();
                return(null);
            }
        }
Exemplo n.º 4
0
        static Book BookForm(LibraryContext Context, FormMode mode)
        {
            Book book = new Book();

            string ISBN;
            bool   uniqueISBN;

            if (mode == FormMode.New)
            {
                do
                {
                    Console.Write("Enter ISBN of the new book: ");
                    ISBN       = Console.ReadLine();
                    uniqueISBN = true;

                    foreach (var b in Context.Books)
                    {
                        if (b.ISBN == ISBN)
                        {
                            uniqueISBN = false;
                            break;
                        }
                    }

                    if (uniqueISBN && !string.IsNullOrEmpty(ISBN))
                    {
                        book.ISBN = ISBN;
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Error: Entered ISBN is invalid or already taken. Try again.");
                    }
                } while (!uniqueISBN || string.IsNullOrEmpty(ISBN));
            }


            string title;

            do
            {
                Console.Write(mode == FormMode.New ? "Enter the title of the new book: " : "Enter new title of the editted book: ");
                title = Console.ReadLine();

                if (string.IsNullOrEmpty(title))
                {
                    Console.WriteLine("Error: Title is required! Try again.");
                }
            } while (string.IsNullOrEmpty(title));

            book.Title = title;


            Console.Write(mode == FormMode.New ? "Enter the description of the new book: " : "Enter new description of the editted book: ");
            book.Description = Console.ReadLine();

            string authorsFirstName, authorsLastName;

            do
            {
                Console.Write(mode == FormMode.New ? "Enter author's firstname:" : "Enter new author's firstname: ");
                authorsFirstName = Console.ReadLine();

                if (string.IsNullOrEmpty(authorsFirstName))
                {
                    Console.WriteLine("Error: Author's firstname is required! Try again.");
                }
            } while (string.IsNullOrEmpty(authorsFirstName));

            do
            {
                Console.Write(mode == FormMode.New ? "Enter author's lastname:" : "Enter new author's lastname: ");
                authorsLastName = Console.ReadLine();

                if (string.IsNullOrEmpty(authorsLastName))
                {
                    Console.WriteLine("Error: Author's lastname is required! Try again.");
                }
            } while (string.IsNullOrEmpty(authorsLastName));


            Author author = Context.Authors.SingleOrDefault(a => a.FirstName == authorsFirstName && a.LastName == authorsLastName);

            if (author == null)
            {
                author = new Author
                {
                    FirstName = authorsFirstName,
                    LastName  = authorsLastName
                };

                Context.Authors.Add(author);
                Context.SaveChanges();
            }

            book.AuthorId = author.Id;

            Console.WriteLine(mode == FormMode.New ? "Select genre of the new book:" : "Select new genre of the editted book:");
            Console.WriteLine("[1] Tragedy\n[2] Tragic comedy\n[3] Fantasy");
            Console.WriteLine("[4] Mythology\n[5] Adventure\n[6] Mystery");
            Console.WriteLine("[7] Science fiction\n[8] Drama\n[9] Romance");
            Console.Write("[10] Action / Adventure\n[11] Satire\n[12] Horror\n> ");

            int genreId;

            while (!int.TryParse(Console.ReadLine(), out genreId) || genreId < 1 || genreId > 12)
            {
                Console.Write("Error: Invalid input.\nSelect a proper value.\n> ");
            }

            book.GenreId = genreId;

            return(book);
        }
Exemplo n.º 5
0
        static void RegisterUser(LibraryContext context)
        {
            Console.WriteLine("╔══════════════╗");
            Console.WriteLine("║ REGISTRATION ║");
            Console.WriteLine("╚══════════════╝");

            string login, pass, passConfirm;
            bool   loginAvailable;

            do
            {
                loginAvailable = true;

                Console.Write("Enter login: "******"Error: Login is required! Try again.");
                    continue;
                }

                foreach (var user in context.Users)
                {
                    if (user.Login == login)
                    {
                        loginAvailable = false;
                        Console.WriteLine("Login \"" + login + "\" is already taken. Use different one.");
                    }
                }
            } while (string.IsNullOrEmpty(login) || !loginAvailable);

            do
            {
                Console.Write("Enter password: "******"Error: Password is required! Try again.");
                }
            } while (string.IsNullOrEmpty(pass));

            do
            {
                Console.Write("Confirm password: "******"Error: Password confirmation differs from password. Try again.");
                }
            } while (string.IsNullOrEmpty(login) || pass != passConfirm);

            context.Users.Add(new User
            {
                Login    = login,
                Password = pass,
                Role     = User.RoleType.Regular
            });

            context.SaveChanges();
            Console.WriteLine("Account has been registered successfully.");
            Console.WriteLine("\nPress any key to continue.");
            Console.ReadKey();
            Console.Clear();
        }