Skip to content
Snippets Groups Projects
Commit 0496e27e authored by W. Spencer Smith's avatar W. Spencer Smith
Browse files

Updates to L27 Spec using UML - strategy design pattern etc

parent 344877e4
No related branches found
No related tags found
No related merge requests found
Showing
with 315 additions and 13 deletions
No preview for this file type
#include "PointT.h"
#include "BankAccount.h"
#include "DataSetInterface.h"
#include <iostream>
......@@ -7,12 +8,13 @@ using namespace std;
int main()
{
DataSetInterface ds;
//Test Data Set Inteface with PointT
PointT p1(0.0, 0.0);
PointT p2(3.0, 4.0);
PointT p3(7.0, 24.0);
PointT p4(5.0, 12.0);
DataSetInterface ds;
ds.add(&p1);
ds.add(&p2);
ds.add(&p3);
......@@ -20,13 +22,30 @@ int main()
cout << "average = " << ds.getAverage() << "\n";
Measurable* maxM = ds.getMaximum();
Measurable *maxM = ds.getMaximum();
PointT* maxP = (PointT*) maxM;
PointT *maxP = (PointT*) maxM;
cout << "maximum = " << *maxP << "\n";
//Test Data Set Inteface with Bank Account
BankAccount b1;
b1.deposit(300);
BankAccount b2;
b2.deposit(100);
DataSetInterface dsBank;
dsBank.add(&b1);
dsBank.add(&b2);
cout << "average = " << dsBank.getAverage() << "\n";
maxM = dsBank.getMaximum();
BankAccount *maxB = (BankAccount*) maxM;
cout << "maximum = " << *maxB << "\n";
//maxP = dynamic_cast<PointT*>(maxM);
cout << "maximum = " << maxP << "\n";
return 0;
}
#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include "Measurable.h"
#include <iostream>
class BankAccount : public Measurable
{
public:
BankAccount();
void deposit(double amount);
void withdraw(double amount);
double getBalance();
friend std::ostream& operator<< (std::ostream &out, const BankAccount &ba);
double getMeasure();
private:
double balance;
};
#endif
......@@ -7,14 +7,14 @@ class DataSetInterface
{
private:
double sum;
Measurable* maximum;
Measurable *maximum;
int count;
public:
DataSetInterface();
void add(Measurable* x);
void add(Measurable *x);
double getAverage();
Measurable* getMaximum();
Measurable *getMaximum();
};
#endif
#include "BankAccount.h"
BankAccount::BankAccount()
{
this->balance = 0.0;
}
void BankAccount::deposit(double amount)
{
this->balance += amount;
}
void BankAccount::withdraw(double amount)
{
this->balance -= amount;
}
double BankAccount::getBalance()
{
return balance;
}
double BankAccount::getMeasure()
{
return balance;
}
std::ostream& operator<< (std::ostream &out, const BankAccount &ba)
{
//From http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
out << "BankAccount(" << ba.balance << ") ";
return out;
}
......@@ -7,7 +7,7 @@ DataSetInterface::DataSetInterface() : maximum()
this->count = 0;
}
void DataSetInterface::add(Measurable* x)
void DataSetInterface::add(Measurable *x)
{
this->sum += x->getMeasure();
if (count == 0 || maximum->getMeasure() < x->getMeasure()) maximum = x;
......@@ -20,7 +20,7 @@ double DataSetInterface::getAverage()
else return sum/count;
}
Measurable* DataSetInterface::getMaximum()
Measurable *DataSetInterface::getMaximum()
{
return maximum;
}
SRC_DIRS := src
C_SRCS := $(foreach srcdir,$(SRC_DIRS),$(wildcard $(srcdir)/*.c))
CXX_SRCS := $(foreach srcdir,$(SRC_DIRS),$(wildcard $(srcdir)/*.cpp))
C_OBJS := ${C_SRCS:.c=.o}
CXX_OBJS := ${CXX_SRCS:.cpp=.o}
OBJS := $(C_OBJS) $(CXX_OBJS)
INCLUDE_DIRS := include
LIBRARY_DIRS :=
LIBRARIES :=
prog_NAME := experiment
prog_DIR := bin
prog_FULL := $(prog_DIR)/$(prog_NAME)
prog_SRC_DIRS := experimentation
prog_C_SRCS := $(foreach srcdir,$(prog_SRC_DIRS),$(wildcard $(srcdir)/*.c))
prog_CXX_SRCS := $(foreach srcdir,$(prog_SRC_DIRS),$(wildcard $(srcdir)/*.cpp))
prog_C_OBJS := ${prog_C_SRCS:.c=.o}
prog_CXX_OBJS := ${prog_CXX_SRCS:.cpp=.o}
prog_OBJS := $(prog_C_OBJS) $(prog_CXX_OBJS)
prog_INCLUDE_DIRS :=
prog_LIBRARY_DIRS :=
prog_LIBRARIES :=
all_OBJS := $(OBJS) $(prog_OBJS) $(test_OBJS)
DEP := $(all_OBJS:%.o=%.d)
CXXFLAGS += -std=c++11
CXXFLAGS += $(foreach includedir,$(INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(LIBRARIES),-l$(library))
.PHONY: experiment src clean
experiment: CXXFLAGS += $(foreach includedir,$(prog_INCLUDE_DIRS),-I$(includedir))
experiment: LDFLAGS += $(foreach librarydir,$(prog_LIBRARY_DIRS),-L$(librarydir))
experiment: LDFLAGS += $(foreach library,$(prog_LIBRARIES),-l$(library))
experiment: $(prog_FULL)
./$(prog_FULL)
$(prog_FULL): $(prog_OBJS) $(OBJS)
$(LINK.cc) $^ -o $@
-include $(DEP)
%.o: %.cpp
$(CXX) $(CXXFLAGS) -MMD -c $< -o $@
clean:
@- $(RM) $(prog_FULL)
@- $(RM) $(prog_OBJS)
@- $(RM) $(OBJS)
@- $(RM) $(DEP)
File added
#include "PointMT.h"
#include "DataSetInterface.h"
#include <iostream>
using namespace std;
int main()
{
//Test Data Set Inteface with PointT
PointMT p1(0.0, 0.0);
PointMT p2(3.0, 4.0);
PointMT p3(7.0, 24.0);
PointMT p4(5.0, 12.0);
DataSetInterface ds;
ds.add(&p1);
ds.add(&p2);
ds.add(&p3);
ds.add(&p4);
cout << "average = " << ds.getAverage() << "\n";
Measurable *maxM = ds.getMaximum();
PointMT *maxP = (PointMT*) maxM;
cout << "maximum = " << *maxP << "\n";
return 0;
}
#ifndef DATASETINTERFACE_H
#define DATASETINTERFACE_H
#include "Measurable.h"
class DataSetInterface
{
private:
double sum;
Measurable *maximum;
int count;
public:
DataSetInterface();
void add(Measurable *x);
double getAverage();
Measurable *getMaximum();
};
#endif
#ifndef MEASURABLE_H
#define MEASURABLE_H
class Measurable
{
public:
virtual double getMeasure() = 0;
};
#endif
#ifndef POINTMT_H
#define POINTMT_H
#include "PointT.h"
#include "Measurable.h"
class PointMT : public PointT, public Measurable
{
public:
PointMT(double x, double y);
double getMeasure();
};
#endif
#ifndef POINTT_H
#define POINTT_H
#include <iostream>
class PointT
{
public:
PointT(double x, double y);
double xcoord();
double ycoord();
double distToOrigin();
friend std::ostream& operator<< (std::ostream &out, const PointT &point);
private:
double xc;
double yc;
};
#endif
#include "DataSetInterface.h"
#include "Measurable.h"
DataSetInterface::DataSetInterface() : maximum()
{
this->sum = 0.0;
this->count = 0;
}
void DataSetInterface::add(Measurable *x)
{
this->sum += x->getMeasure();
if (count == 0 || maximum->getMeasure() < x->getMeasure()) maximum = x;
count++;
}
double DataSetInterface::getAverage()
{
if (count == 0) return 0;
else return sum/count;
}
Measurable *DataSetInterface::getMaximum()
{
return maximum;
}
#include "PointMT.h"
#include "Measurable.h"
PointMT::PointMT(double x, double y) : PointT(x, y)
{
}
double PointMT::getMeasure()
{
return this->distToOrigin();
}
#include "PointT.h"
#include <cmath>
PointT::PointT(double x, double y)
{
this->xc = x;
this->yc = y;
}
double PointT::xcoord()
{
return this->xc;
}
double PointT::ycoord()
{
return this->yc;
}
double PointT::distToOrigin()
{
double dx;
double dy;
dx = this->xc;
dy = this->yc;
return sqrt(pow(dx,2.0) + pow(dy,2.0));
}
std::ostream& operator<< (std::ostream &out, const PointT& point)
{
//From http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
// Since operator<< is a friend of the Point class, we can access Point's members directly.
out << "PointT(" << point.xc << ", " << point.yc << ")";
return out;
}
File added
<mxfile userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0" version="8.4.6" editor="www.draw.io" type="device"><diagram id="51d45c3b-694b-ad9c-a1ca-de0ff4b50e78" name="Page-1">1VZdb8IgFP01fV3aslZ99HN7WbJEk7lHVq4tsS0GUdv9+lGhtohdzDJd5oPhHuDAOZdjdNA4K5443iQvjEDq+C4pHDRxfD8YhPK7AkoFoIGvgJhToiCvAeb0EzToanRHCWyNhYKxVNCNCUYszyESBoY5Zwdz2Yql5qkbHIMFzCOc2ugbJSJRaD9wG/wZaJzUJ3uunvnA0TrmbJfr8xwfrY4fNZ3hmkuv3yaYsEMLQlMHjTljQo2yYgxpZW1tm9o365g93ZtDLq7ZoNuyx+lOS59ggecgXhmVDOqOoqx9SUSWypHnoJHeCFxA0Xm4d5IkXwqwDAQv5ZLC7HZplofGca/GkpbbdRewbnJ8Im6EyoHWelk3snQfBS9urNh3/07yoyXZEgs5GVbpkRWhOGM5WSQ0l9LlxIxWzBNXVTqykhSNWhZJG3i5bBfv1Z6HoCoLKpY1gxw3M+oeQKxMntkq78p2PAKjhQLzGETrNdvmt90NtZMcUizo3jzxkr2aTueh4+n2z/qj7tlkaNZF0/ueR4mzeI6tPgm8qvtBV9BHOF8Po7tn/Z4vP7S0K9GR/J2+tW4r8fcU3vtvkW/nO7TzHVzt/u0j7/1W5i2iH4dels0/B7W8+XeGpl8=</diagram></mxfile>
\ No newline at end of file
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment