Answer to Question #345564 in C++ for Varun

Question #345564

A finger-print machine is kept in a school. All the students are required to record their finger-print on their arrival . Expected arrival time is common to all the students. For every one minute of late arrival, 50 paise is charged as fine. For example, if the arrival time of student is 9:03:10 and expected time is 9:00:00 then fine is Rs. 2. Given the details of students, their arrival time on a particular day and the expected arrival time, design an OOP model and write a C++ code to print the rollno and the name of the students who came earlier than the arrival time and calculate fine amount. Understand the precode and application program to do the implementation.

Input Format

Number of students

Roll no of Student1

Name of Student1

Arrival time of Student1

Roll no of Student2

Name of Student2

Arrival time of Student2

...

Roll no of Student-n

Name of Student-n

Arrival time of Student-n

 

All time is given in hours followed by minutes followed by seconds

(Perform Operator Overloading of << and >> operators)



1
Expert's answer
2022-05-27T09:39:21-0400


#include <iostream>
#include <cstring>
using namespace std;
class Student{
    private:
        string name;
        int rollno;
        float fine;
    public:
        Student(){
            fine = 0;
        }
        Student(string studentName, int rollNumber){
            name = studentName;
            rollno = rollNumber;
            fine = 0;
        }
        void setData(string studentName, int rollNumber){
            name = studentName;
            rollno = rollNumber;
        }
        string getName(){
            return name;
        }
        float getFine(){
            return fine;
        }
        int getRollno(){
            return rollno;
        }
        void addFine(int minutes){
            fine+=minutes*50;
        }
        void display(){
            cout<<name<<" - roll number "<<rollno<<" - "<<fine<<" paise";
        }
        


};
class Timer{
    public:
    unsigned int hours;
    unsigned int minutes;
    unsigned int seconds;
    
    bool operator>(const Timer& b){
        if(hours>b.hours) return true;
        if(hours<b.hours) return false;


        if(minutes>b.minutes) return true;
        if(minutes<b.minutes) return false;


        if(seconds>b.seconds) return true;
        return false; 
    }
    bool operator<(const Timer& b){
        if(hours<b.hours) return true;
        if(hours>b.hours) return false;


        if(minutes<b.minutes) return true;
        if(minutes>b.minutes) return false;


        if(seconds<b.seconds) return true;
        return false; 
    }    
};
struct visit{
    Timer arrivalTime;
    Student *student;
};


ostream& operator<<(ostream& os, const Timer& t)
{
    os <<t.hours<< ":" << t.minutes <<":"<<t.seconds;
    return os;
}


istream& operator>>(istream& is, Timer& t){ // TODO: +1 minute if seconds>60, same with minutes
    char someDivider;
   is>>t.hours>>someDivider>>t.minutes>>someDivider>>t.seconds;
    return is;
}


int differenceMinutes(const Timer& a, const Timer& b){
    return (a.hours-b.hours)*60+a.minutes-b.minutes-(a.seconds<b.seconds);   // if the minute is not full -1
}


int main()
{
    int n;
    cout<<"Number of students: ";
    cin>>n;
    Student students[n];
    visit visitLog[n];
    for(int i = 0; i<n; i++){
       
        string tempName;
        int tempRollno; 
        Timer tempTimer;
        
        cout<<i+1<<". Enter name: ";
        cin.ignore();
        getline(cin, tempName);
        cout<<i+1<<". Enter roll number: ";
        cin>>tempRollno;
        students[i].setData(tempName, tempRollno);
        cout<<i+1<<". Enter arrival time: ";
        cin>>tempTimer;
        visitLog[i].arrivalTime = tempTimer;
        visitLog[i].student = &students[i];
    }
    
    Timer commonArrivalTime;
    cout<<"Enter common arrival time: ";
    cin>>commonArrivalTime;
    
    // end of input section
    
    for(int i = 0; i<n; i++){   // set fines
            int minutesLate = differenceMinutes(visitLog[i].arrivalTime, commonArrivalTime);
            if(minutesLate>0)
                visitLog[i].student->addFine(minutesLate);
    }
    
    for(int i = 0; i< n; i++){  // print students who came earlier 
        if(visitLog[i].arrivalTime<commonArrivalTime)   // switch "<" sign to ">" if you want to display the ones who came late
            visitLog[i].student->display();
    }


    
    return 0;
}

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
New on Blog
APPROVED BY CLIENTS