Refactored #7 out into class files like I should have to begin with.

This commit is contained in:
2022-12-08 23:17:56 -05:00
parent 7ef32aafcb
commit 2139a01252
7 changed files with 155 additions and 106 deletions

58
2022/7/Folder.cpp Normal file
View File

@@ -0,0 +1,58 @@
#include "Folder.h"
#include <algorithm>
#include <iostream>
Folder::Folder(const std::string &filename)
: File(filename, 0)
{
}
unsigned long Folder::Size() const
{
unsigned long size = 0;
for(const auto *file : files)
size += file->Size();
return size;
}
void Folder::AddFile(const std::string &filename,
unsigned long size)
{
// make sure the file doesn't already exist:
if(std::find_if(files.begin(), files.end(),
[&](const File *file) {
return file->Filename() == filename;
}) != files.end())
{
std::cerr << "Folder \"" << this->Filename() << "\" already contains "
<< "the file \"" << filename << "\"." << std::endl;
exit(-1);
}
// increment this folder's size total:
this->size += size;
// add the file:
files.push_back(new File(filename, size));
}
void Folder::AddFolder(const std::string &name)
{
if(std::find_if(files.begin(), files.end(),
[&](const File *file) {
return file->Filename() == name;
}) != files.end())
{
std::cout << "NOTE: Folder \"" << this->Filename() << "\" already contains "
<< "the subfolder \"" << name << "\"." << std::endl;
// exit(-1);
}
files.push_back(new Folder(name));
}
const std::vector<File*>& Folder::Files() const
{
return files;
}