using static System.Console;
/// <summary>
/// # # # # # # # # # # #
/// # Mark Hesser #
/// # Jan 18. 18 #
/// # Library Fines #
/// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
/// # Instructions: #
/// # Create an application for a library and name it FineForOverdueBooks. The 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 displays the library fine, which is #
/// # 10 cents per book per day for the first seven days a book is overdue, then 20 cents per book per day for each additional day. #
/// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
/// </summary>
namespace FineForOvedueBooks
{
class Library
{
static void Main(string[] args)
{
#region Declarations
bool quit = false;
int numBooks;
int[] daysPast;
double totalFine = 0;
#endregion
Title = "Library Fines";
while (!quit)
{
Clear();
WriteLine("\n Please enter the Number of Books that have been returned and how many days each book is Overdue.");
Write("\n Number of Books: ");
while (!Int32.TryParse(ReadLine(), out numBooks))
{
WriteLine(" That is not a number… Please Try Again!");
Write("\n Number of Books: ");
}
//This creates a new emtpy array with the length of the number of books returned
daysPast = new int[numBooks];
//This prompts the user to enter the number of days past due for each book.
for (int i = 0; i < daysPast.Length; i++)
{
Write(" Book {0}, Days Overdue: ", i + 1);
while (!Int32.TryParse(ReadLine(), out daysPast[i]))
{
WriteLine(" That is not a number… Please Try Again!");
Write(" Book {0}, Days Overdue: ", i + 1);
}
}
CalculateFine(ref totalFine, daysPast);
//If there is no fine
if (totalFine == 0)
WriteLine("\n There is no fine due at this Time.", totalFine);
//Otherwise Display the Fine Total
else
WriteLine("\n The total fine is {0:C}.", totalFine);
Quit(ref quit);
}
}
/// <summary>
/// This method calculates the Fine owed based on the number of books (daysPast.Length).
/// Then adds the total for each day based on each book.
/// </summary>
/// <param name="totalFine"></param>
/// <param name="daysPast"></param>
/// <returns></returns>
static double CalculateFine(ref double totalFine, int[] daysPast)
{
const double pricePerDay = .1, priceAfter7 = .2;
totalFine = 0;
//This adds the price for the 1st 7 days to the total for each book.
for (int i = 0; i < daysPast.Length; i++)
{
//If the number of days past is less than 7
if (daysPast[i] <= 7)
totalFine = totalFine + (daysPast[i]*pricePerDay);
//If the number of days past is greater than 7
else if (daysPast[i] > 7)
{
totalFine = totalFine + (7 * pricePerDay) + ((daysPast[i] – 7) * priceAfter7);
}
}
return totalFine;
}
/// <summary>
/// This Method asks the user if they would like to enter another customer
/// </summary>
static void Quit(ref bool quit)
{
ConsoleKeyInfo kb;
//Asks the user if they want to enter another customer
Write("\n Would you like to enter another Customer? (y/n) ");
#region Quit?
kb = ReadKey(); //Listens for Key
if (kb.Key == ConsoleKey.N) //Ends the Program
{
quit = true;
}
else if (kb.Key == ConsoleKey.Y)
{
quit = false;
}
else //Tells the User they are stupid and then Quits the program
{
WriteLine("\n You didn’t hit Y or N. " +
"\n I’m going to assume you said No. " +
"\n Press Any Key Quit…\a");
ReadKey();
quit = true; //Ends Program
}
#endregion
}
}
}
[/code]