Answer to Question #155619 in C++ for Emmy

Question #155619

Each student can open an account in the cafeteria. It has the following features:

·       The system can hold data for 20 students.

·       The student can deposit any amount in his/her account.

·       The student can use the available amount in his/her account to buy lunches or snacks.

·       Menu price list is as below:

Item Cost in PKR

Lunch 150

Snack 100

Drink 50

·       Each student account should have the following details stored:

1.     Roll no (Data type: int, format: BBRRRR, e.g. 201406)

2.     Account No (Data type: int)

3.     Balance (Data type: double)

4.     Student Name (Data type: char array)

{Hint: Use 2D arrays for storing student first and last names: char Student_Name [20][30]}

Functionality

1. Create_Account()

Assign each student a unique account number.

Get student Roll No and Name.

Ask the user for the initial Deposit amount.

2. Display()

Gives all details of a single Account.

3.Deposit_amount()

Updates the Account Balance.

4. Purchase_Item()

Following functionality must be implemented:

·       Displays menu of available items for student to choose

·       The student can select more than one item for purchase

·       Updates the account balance if item can be purchased with the available amount, else notifies if amount balance is not sufficient

5. Store_in_File ()

Writes data of the accounts in use into a file “Record_sys.txt”

6.Display_Sorted()

Sort the entire data on the basis of Roll No. and display it in an ascending order

7. Read_from_file()

At the beginning of your program update all the previously saved data from the file “Record_sys.txt”


1
Expert's answer
2021-01-15T18:16:32-0500
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <fstream>
using namespace std;


class Student {
    
    public: 
        int rollNumber;
        int AccountNumber;
        double balance;
        string studentName;
        
        void Create_Account() {
            srand(time(0));
            // Account number length is 10
            AccountNumber = rand () % 900000000 + 100000000;
            // roll number length is 6 
            rollNumber = rand() % 900000 + 100000;
            cout << "Enter initial deposit amount: ";
            cin >> balance;
            cout << "Enter student name: ";
            cin >> studentName;
        }
        void Display() {
            cout << setw(28) << studentName << " |"<< setw(23) << rollNumber << " |" << setw(23) << AccountNumber << " |" << setw(22) << balance << "$ |\n";
        }
        void Deposit_amount() {
            cout << setw(28) << studentName << " |"<< setw(23) << rollNumber << " |" << setw(23) << AccountNumber << " |" << setw(22) << balance + 120 << "$ |\n";
        }
        void Purchase_Item() {
            cout << "You can purchase in a following prices:\nSelect numbers: |=|=|To stop the order enter 0|=|=|\n";
            cout << "1. Lunch 150\n";
            cout << "2. Snack 100\n";
            cout << "3. Drink 50\n";
            int n = 0, Array[n];
            while (true) {
                int select;
                cin >> select;
                Array[n++] = select;
                if (select == 0) {
                    break;
                }
            }
            float total = 0;
            for (int i = 0; i < n; i++) {
                if (Array[i] == 1) {
                    total += 150;
                }
                if (Array[i] == 2) {
                    total += 100;
                }
                if (Array[i] == 3) {
                    total += 50;
                }
            }
            cout << "Your must pay: " << total << "$ \n";
            if (balance + 120 - total > 0) {
                cout << "Your balance is " << balance + 120 - total << "$ \n";
            }
            if (balance + 120 - total < 0) {
                cout << "Insufficient balance: \n";
            }
        }
        void Store_in_File() {
            ofstream myfile;
            myfile.open ("Record_sys.txt", ios::app);
            myfile << setw(28) << studentName << " |"<< setw(23) << rollNumber << " |" << setw(23) << AccountNumber << " |" << setw(22) << balance << "$ |\n";
            myfile.close();
        }
};


int main () {
    Student student[5];
    
    for (int i = 0; i < 5; i++) {
        cout << "Enter " << i + 1 << " - student details:\n";
        student[i].Create_Account();
    }
    
    cout << setw(30) << "Student Name |" << setw(25) << "Roll Number |" << setw(25) << "Account Number |" << setw(25) << "Student balance |" << endl;
    for (int i = 0; i < 5; i++) {
        student[i].Display();
    }
    
    cout << "\nThe next seen of table each balance of student increases 120$: \n";
    cout << setw(30) << "Student Name |" << setw(25) << "Roll Number |" << setw(25) << "Account Number |" << setw(25) << "Student balance |" << endl;
    for (int i = 0; i < 5; i++) {      
        student[i].Deposit_amount();
    }
    
    cout << "\nField of purchasing:\n";
    for (int i = 0; i < 5; i++) {      
        student[i].Purchase_Item();
    }
    
    for (int i = 0; i < 5; i++) {
        student[i].Store_in_File();
    }
    
    // sorting in a ascending order:
    cout << "\n";
    for (int i = 0; i < 5; i++) {
        for (int j = i + 1; j < 5; j++) {
            if (student[i].rollNumber > student[j].rollNumber) {
                int temp = student[i].rollNumber;
                student[i].rollNumber = student[j].rollNumber;
                student[j].rollNumber = temp;
            }
        }
    }
    for (int i = 0; i < 5; i++) {
        cout << student[i].rollNumber << endl;
    }
    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

Assignment Expert
18.01.21, 09:26

Dear Haider Ali rana, Questions in this section are answered for free. We can't fulfill them all and there is no guarantee of answering certain question but we are doing our best. And if answer is published it means it was attentively checked by experts. You can try it yourself by publishing your question. Although if you have serious assignment that requires large amount of work and hence cannot be done for free you can submit it as assignment and our experts will surely assist you.

Haider Ali rana
17.01.21, 09:11

As I don't have a knowledge of class and structure so can you please help me to how to do this code by only using functions

Leave a comment

LATEST TUTORIALS
New on Blog