Скачиваний:
0
Добавлен:
11.01.2026
Размер:
6.1 Кб
Скачать
#include "class.h"
#include <string.h>
#include <cmath>
#include <ctime>


bool OrgTechnic::IsWarrantyValid()
{
    return MonthsInUse() <= warrantyMonths;
}

float OrgTechnic::CurrentValue()
{
    int monthsUsed = MonthsInUse();
    if(monthsUsed <= warrantyMonths) {
        return price * (1.0f - (monthsUsed * 0.005f));
    } else {
        int monthsAfterWarranty = monthsUsed - warrantyMonths;
        float depreciation = (warrantyMonths * 0.005f) + (monthsAfterWarranty * 0.02f);
        float value = price * (1.0f - depreciation);
        return (value > price * 0.1f) ? value : price * 0.1f;
    }
}

int OrgTechnic::MonthsInUse()
{
    int day, month, year;
    sscanf(purchaseDate, "%d.%d.%d", &day, &month, &year);

    time_t t = time(NULL);
    struct tm *currentTime = localtime(&t);
    int currentYear = currentTime->tm_year + 1900;
    int currentMonth = currentTime->tm_mon + 1;

    return (currentYear - year) * 12 + (currentMonth - month);
}

void OrgTechnic::SetCoordinates(int x, int y)
{
    coordX = x;
    coordY = y;
}

void OrgTechnic::SaveToFile(FILE *file)
{
    fprintf(file, "%d|%s|%s|%s|%d|%f|%s|%d|%d\n",
            id, category, model, purchaseDate, warrantyMonths,
            price, additionalInfo, coordX, coordY);
}

void OrgTechnic::LoadFromFile(FILE *file)
{
    fscanf(file, "%d|%[^|]|%[^|]|%[^|]|%d|%f|%[^|]|%d|%d\n",
           &id, category, model, purchaseDate, &warrantyMonths,
           &price, additionalInfo, &coordX, &coordY);
}


void Supply::SaveToFile(FILE *file)
{
    fprintf(file, "%s|%s|%s|%s\n",
            supplier, category, model, supplyDate);
}

void Supply::LoadFromFile(FILE *file)
{
    char line[300];
    if (fgets(line, sizeof(line), file)) {
        char* token = strtok(line, "|\n");
        if (token) strcpy(supplier, token);

        token = strtok(NULL, "|\n");
        if (token) strcpy(category, token);

        token = strtok(NULL, "|\n");
        if (token) strcpy(model, token);

        token = strtok(NULL, "|\n");
        if (token) strcpy(supplyDate, token);
	}
}


void Repair::SaveToFile(FILE *file)
{
    fprintf(file, "%d|%s|%s|%s|%s\n",
            deviceId, category, model, repairStartDate, condition);
}

void Repair::LoadFromFile(FILE *file)
{
    char line[300];
    if (fgets(line, sizeof(line), file)) {
        char* token = strtok(line, "|\n");
        if (token) deviceId = atoi(token);

        token = strtok(NULL, "|\n");
        if (token) strcpy(category, token);

        token = strtok(NULL, "|\n");
        if (token) strcpy(model, token);

        token = strtok(NULL, "|\n");
        if (token) strcpy(repairStartDate, token);

        token = strtok(NULL, "|\n");
        if (token) strcpy(condition, token);
    }
}



// Конструктор
ReportCalculator::ReportCalculator(OrgTechnic* devArray, int count, bool* filter, char** cats, int catCount)
{
    devices = devArray;
    deviceCount = count;
    categoryFilter = filter;
    categories = cats;
    categoryCount = catCount;
}

// Проверка проходит ли устройство через фильтр категорий
bool ReportCalculator::DevicePassesFilter(int index)
{
    // Если фильтр не установлен - пропускаем все устройства
    if (categoryFilter == NULL || categories == NULL || categoryCount == 0) {
        return true;
    }

    // Ищем категорию устройства в фильтре
    for (int i = 0; i < categoryCount; i++) {
        if (strcmp(devices[index].category, categories[i]) == 0) {
            // Нашли категорию - проверяем выбран ли её чекбокс
            return categoryFilter[i];
        }
    }

    // Категория не найдена в фильтре - не показываем
    return false;
}

// Общая первоначальная стоимость (с фильтром)
float ReportCalculator::CalculateTotalOriginalValue()
{
    float total = 0.0f;
    for(int i = 0; i < deviceCount; i++) {
        if (DevicePassesFilter(i)) {
            total += devices[i].price;
        }
    }
    return total;
}

// Общая текущая стоимость (с фильтром)
float ReportCalculator::CalculateTotalCurrentValue()
{
    float total = 0.0f;
    for(int i = 0; i < deviceCount; i++) {
        if (DevicePassesFilter(i)) {
            total += devices[i].CurrentValue();
        }
    }
    return total;
}

// Общая амортизация (с фильтром)
float ReportCalculator::CalculateTotalDepreciation()
{
    return CalculateTotalOriginalValue() - CalculateTotalCurrentValue();
}

// Процент амортизации (с фильтром)
float ReportCalculator::CalculateDepreciationPercentage()
{
    float original = CalculateTotalOriginalValue();
    if(original == 0) return 0.0f;
    return (CalculateTotalDepreciation() / original) * 100.0f;
}

// Количество устройств с гарантией (с фильтром)
int ReportCalculator::CountDevicesWithWarranty()
{
    int count = 0;
    for(int i = 0; i < deviceCount; i++) {
        if (DevicePassesFilter(i) && devices[i].IsWarrantyValid()) {
            count++;
        }
    }
    return count;
}

// Количество устройств без гарантии (с фильтром)
int ReportCalculator::CountDevicesWithoutWarranty()
{
    int count = 0;
    for(int i = 0; i < deviceCount; i++) {
        if (DevicePassesFilter(i) && !devices[i].IsWarrantyValid()) {
            count++;
        }
    }
    return count;
}

// Общее количество устройств (с фильтром)
int ReportCalculator::GetTotalDevicesCount()
{
    int count = 0;
    for(int i = 0; i < deviceCount; i++) {
        if (DevicePassesFilter(i)) {
            count++;
        }
    }
    return count;
}
Соседние файлы в папке Курсовая работа Армашев 3 семестр. Список оргтехники предприятия. С++