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.
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
}
}
}
Comments
Leave a comment