1. Library Fines A local public library needs a program to calculate the overdue
ID: 3731401 • Letter: 1
Question
1. Library Fines
A local public library needs a program to calculate the overdue fines owed by its patrons. The library needs the clerk to enter the patron’s library card number, age of the patron, the number of days overdue for the item and the type of the item using the follow (enter 1, 2 or 3):
1. Book
2. Magazine
3. DVD
Books that are overdue are assessed a fine of 50 cents per day. Magazines are assessed a fine of 25 cents per day. DVDs are assessed a fine of $1.50 a day. There is a maximum fine assessed of $5.00. Senior Citizens (over the age of 70) do not pay fines. Juvenile patrons (ages 6 through 17) pay a maximum fine of $1.00.
Once the fine has been calculated all information about the patron and the overdue material should be printed to the screen. Here is an example of the report that should be output:
Card number 12345
Age 22
Item Type DVD
Number of Days Overdue 2
Overdue Fine $3.00
Fine adjustment reason (e.g, Senior citizen, Juvenile, exceed max)
Each time the program is executed it calculates the fine for a single item. If the user enters invalid data, then a message must be presented on the screen specifying the invalid information and the calculation is aborted.
a) Run your program multiple times using the following information as input. After each run of the program save your entire console output screen to a text file as discussed in our labs.
· 11111 is 75 years old and has an overdue magazine. The magazine is 1 day overdue.
· 22222 is 30 years old and has an overdue magazine. The magazine is 10 days overdue.
· 33333 is 10 years old and has an overdue DVD. The DVD is 2 days overdue.
· 44444 is 50 years old and has an overdue DVD. The DVD is 2 days overdue.
· 55555 is 20 years old and has an overdue book. The book is 60 days overdue.
· 66666 is 15 years old and has an overdue magazine. The magazine is 3 days overdue.
Explanation / Answer
//Since the language to be used wasn't specified, I used C++. The output of the console window is stored in Output.txt.If you found this helpful,please leave a like.
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
ofstream fp;
fp.open("Output.txt",ios::app); //Opening the output file
string inp,cardnum,age,type,type1,overd,reas="None";
locale loc;
int rage=0,roverd=0;
float due,per;
getline(cin,inp); //Taking input
int i=0;
while(!isdigit(inp[i],loc)) //Processing the input to get the different variables
i++;
while(isdigit(inp[i],loc)) //Card Number
{
cardnum.push_back(inp[i]);
i++;
}
while(inp[i]==' ')
i++;
if(inp.compare(i,3,"is ")!=0)
{
cout<<"Invalid input";
fp<<"Invalid input ";
return 0;
}
i+=3;
while(!isdigit(inp[i],loc))
i++;
while(isdigit(inp[i],loc)) //Age
{
age.push_back(inp[i]);
i++;
}
while(inp[i]==' ')
i++;
if(inp.compare(i,29,"years old and has an overdue ")!=0)
{
cout<<"Invalid input";
fp<<"Invalid input ";
return 0;
}
i+=29;
while(inp[i]==' ')
i++;
while(isalpha(inp[i],loc)) //Item Type
{
type.push_back(inp[i]);
i++;
}
i++;
if(type.compare("book")!=0&&type.compare("magazine")!=0&&type.compare("DVD")!=0)
{
cout<<"Invalid item type";
fp<<"Invalid item type ";
return 0;
}
while(inp[i]==' ')
i++;
if(inp.compare(i,4,"The ")!=0)
{
cout<<"Invalid input";
fp<<"Invalid input ";
return 0;
}
i+=4;
while(inp[i]==' ')
i++;
while(isalpha(inp[i],loc))
{
type1.push_back(inp[i]);
i++;
}
if(type.compare(type1)!=0)
{
cout<<"Invalid information as you have entered different types of items";
fp<<"Invalid information as you have entered different types of items ";
return 0;
}
while(inp[i]==' ')
i++;
if(inp.compare(i,3,"is ")!=0)
{
cout<<"Invalid input";
fp<<"Invalid input ";
return 0;
}
i+=3;
while(isdigit(inp[i],loc)) //Overdue days
{
overd.push_back(inp[i]);
i++;
}
while(inp[i]==' ')
i++;
if(overd[0]=='1'&&overd.size()==1)
{
if(inp.compare(i,12,"day overdue.")!=0)
{
cout<<"Invalid input";
fp<<"Invalid input ";
return 0;
}
}
else
{
if(inp.compare(i,13,"days overdue.")!=0)
{
cout<<"Invalid input";
fp<<"Invalid input ";
return 0;
}
}
for(i=0;i<age.size();i++) //Changing Age to int value
rage=(rage*10)+age[i]-48;
for(i=0;i<overd.size();i++) //Changing Overdue days to int value
roverd=(roverd*10)+overd[i]-48;
switch(type[0])
{
case 'm':per=0.25;break;
case 'D':per=1.5;break;
case 'b':per=0.5;break;
}
if(rage>70) //Calculating fine for Senior Citizen
{
due=0;
reas="Senior citizen";
}
else if(rage>=6&&rage<=17) //Calculating fine for Juvenile
{
due=per*roverd;
if(due>1)
{
due=1;
reas="Juvenile";
}
}
else // Calculating fine for others
{
due=per*roverd;
}
if(due>5)
{
due=5;
reas="exceed max";
}
cout<<setw(30)<<left<<"Card number"<<cardnum<<" "<<setw(30)<<left<<"Age"<<rage<<" ";
fp<<setw(30)<<left<<"Card number"<<cardnum<<" "<<setw(30)<<left<<"Age"<<rage<<" ";
cout<<setw(30)<<left<<"Item Type"<<type<<" "<<setw(30)<<left<<"Number of Days Overdue"<<roverd<<" ";
fp<<setw(30)<<left<<"Item Type"<<type<<" "<<setw(30)<<left<<"Number of Days Overdue"<<roverd<<" ";
cout<<setw(30)<<left<<"Overdue Fine"<<"$"<<setprecision(3)<<due<<" "<<setw(30)<<left<<"Fine adjustment reason"<<reas<<" ";
fp<<setw(30)<<left<<"Overdue Fine"<<"$"<<setprecision(3)<<due<<" "<<setw(30)<<left<<"Fine adjustment reason"<<reas<<" ";
fp<<" ";
fp.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.