Answer to Question #204052 in C# for Nkhululeko Chabala

Question #204052

Create a program for a library whose Main() method asks the user to input the number of books checked out and the number of days they are overdue. Pass those values to a method that calculates and displays the library fine, which is 20 cents per book per day for the first seven days a book is overdue, then 50 cents per book per day for each additional day.


1
Expert's answer
2021-06-06T23:44:12-0400
using System;


namespace Library
{
    public class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Library program");

            Console.WriteLine(new string('-', 50));

            int books;
            int days;

            Console.WriteLine("Input number of books checked out: ");
            while(!int.TryParse(Console.ReadLine(), out books))
                Console.WriteLine("Wrong input. Please, try again.");

            Console.WriteLine("Input number of days books are overdue: ");
            while(!int.TryParse(Console.ReadLine(), out  days))
                Console.WriteLine("Wrong input. Please, try again.");

            Calculate(books, days);

            Console.WriteLine(new string('-', 50));
        }


        private static void Calculate(int books, int days)
        {
            int priceBeforeWeek = books * 20; // days <= 7
            int priceAfterWeek = books * 50; // days >= 7
            int priceInCents = 0;

            for (int i = 1; i <= days; i++)
            {
                if (i <= 7)
                    priceInCents += priceBeforeWeek;
                else
                    priceInCents += priceAfterWeek;
            }

            double result = (double)priceInCents / 100;

            Console.WriteLine($"Total: {result:C}"); //formatting
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS