ATILIM COMPUTER CLUB

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.4 kB
89
Indexable
Never
SORU:

Write a C++ program that calculates the monthly fee for the film system that rents different
types of films. In your program,
Create a class called Film containing the following data members:
➢ name (string)
➢ type (string)
➢ film_registration_fee (double): the monthly fee of the system
and the following member functions:
➢ getName(): returns the name of the film.
➢ setFee(): sets the daily fee of the film..
➢ getType(): returns the type of the film.
➢ getFee(): returns the monthly fee of the film rent.
➢ add(): if the user enters ‘a’ for adding the film to the system, call this function in
the main and add the film.
➢ A friend function named calculateMonthlyFee(Film f). This function first sets the
daily rental fee for each film. If the film type is “Animation” the daily fee is 11.0;
if the film type is “Action” the daily fee is 17.0. Otherwise, the fee is equal to 15.0.
2
Then, this function calculates the monthly fee by multiplying the fee by 30. The
calculated monthly fee is then returned.
➢ The Film class also has a constructor that receives two parameters to initialize the
film name and type of the film. (Hint: You should use the setFee() function to set
the film fee in the calculateMonthlyFee function.)
In the main function,
➢ Create an array of objects from the Film class. (the size can be two)
➢ Your program should ask the user to enter the selection type: (a) for adding film to
the system or (w) for watching the film from the system, or the user can cancel the
system by entering (C).
➢ If the user enters (a), add the film to your archive by using the add() function.
➢ If the user enters (w), the number of the movie will be entered, accordingly, your
program will get the name and type of the movie, and type of the movie, and
calculate the fee using the friend function calculateMonthlyFee(Film f).
➢ If the user enters (C), print out “Exit the program”.


CEVAP:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Film {
    string name;
    string type;
    double film_registration_fee;

public:
    Film(string n, string t) : name(n), type(t) {
        setFee();
    }

    string getName() {
        return name;
    }

    void setFee() {
        if (type == "Animation") {
            film_registration_fee = 11.0;
        } else if (type == "Action") {
            film_registration_fee = 17.0;
        } else {
            film_registration_fee = 15.0;
        }
    }

    string getType() {
        return type;
    }

    double getFee() {
        return film_registration_fee;
    }

    void add() {
        cout << "The film \"" << name << "\" has been added to the system." << endl;
    }

    friend double calculateMonthlyFee(Film f);
};

double calculateMonthlyFee(Film f) {
    f.setFee();
    return f.film_registration_fee * 30;
}

int main() {
    vector<Film> films;
    char selection;

    while (true) {
        cout << "Enter selection (a: Add film, w: Watch film, C: Cancel): ";
        cin >> selection;

        if (selection == 'a') {
            string name, type;
            cout << "Enter film name: ";
            cin.ignore();
            getline(cin, name);
            cout << "Enter film type: ";
            getline(cin, type);

            Film newFilm(name, type);
            films.push_back(newFilm);
            newFilm.add();
        } else if (selection == 'w') {
            int filmIndex;
            cout << "Enter the number of the film: ";
            cin >> filmIndex;

            if (filmIndex < 0 || filmIndex >= films.size()) {
                cout << "Invalid film number." << endl;
            } else {
                Film selectedFilm = films[filmIndex];
                double monthlyFee = calculateMonthlyFee(selectedFilm);
                cout << "Film name: " << selectedFilm.getName() << endl;
                cout << "Film type: " << selectedFilm.getType() << endl;
                cout << "Monthly fee: $" << monthlyFee << endl;
            }
        } else if (selection == 'C') {
            cout << "Exit the program." << endl;
            break;
        } else {
            cout << "Invalid selection. Try again." << endl;
        }
    }

    return 0;
}