summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--7p2.cpp11
-rw-r--r--7p3.cpp36
-rw-r--r--7p4.cpp13
-rw-r--r--7p5.cpp20
-rw-r--r--person.hpp12
-rw-r--r--sales_data.hpp18
6 files changed, 109 insertions, 1 deletions
diff --git a/7p2.cpp b/7p2.cpp
new file mode 100644
index 0000000..91ad659
--- /dev/null
+++ b/7p2.cpp
@@ -0,0 +1,11 @@
+
+/*
+ *
+ * 7.2
+ *
+ * In sales_data.hpp
+ */
+
+int main () {
+ return 0;
+}
diff --git a/7p3.cpp b/7p3.cpp
new file mode 100644
index 0000000..eee5db3
--- /dev/null
+++ b/7p3.cpp
@@ -0,0 +1,36 @@
+#include <iostream>
+#include "sales_data.hpp"
+
+/*
+ *
+ * 7.3
+ *
+ * This exercise is not correct and honestly i am not sure
+ * what is supposed to be correct. It may be because i misunderstood
+ * The exercise back in the 1.24 exercise.
+ *
+ * I have modified this program now to be more like i think it should be.
+ */
+
+int main () {
+
+
+ SalesData CurItem;
+ if(std::cin >> CurItem.BookNo >> CurItem.UnitsSold >> CurItem.Revenue) {
+ SalesData Item;
+ while(std::cin >> Item.BookNo >> Item.UnitsSold >> Item.Revenue) {
+ if(Item.isbn() == CurItem.isbn()) {
+ CurItem.combine(Item);
+ } else {
+ std::cout << "ISBN: " << CurItem.BookNo << " has " << CurItem.UnitsSold << " units sold"
+ << " and a revenue of: " << CurItem.Revenue << std::endl;
+ CurItem = Item;
+ }
+ }
+
+ std::cout << "ISBN: " << CurItem.BookNo << " has " << CurItem.UnitsSold << " units sold"
+ << " and a revenue of: " << CurItem.Revenue << std::endl;
+ }
+
+ return 0;
+}
diff --git a/7p4.cpp b/7p4.cpp
new file mode 100644
index 0000000..95ab27e
--- /dev/null
+++ b/7p4.cpp
@@ -0,0 +1,13 @@
+
+/*
+ *
+ * 7.4
+ *
+ * Done in person.hpp
+ */
+
+int main () {
+
+
+ return 0;
+}
diff --git a/7p5.cpp b/7p5.cpp
new file mode 100644
index 0000000..8c65760
--- /dev/null
+++ b/7p5.cpp
@@ -0,0 +1,20 @@
+#include <iostream>
+#include "person.hpp"
+
+/*
+ *
+ * 7.5
+ *
+ * Done in person.hpp
+ * We aren't changing the object or anything so const is okay
+ */
+
+int main () {
+
+ Person person1;
+ person1.Name = "John Doe";
+ person1.Address = "Unknown Street 20";
+ std::cout << person1.GetName() << std::endl;
+ std::cout << person1.GetAddress() << std::endl;
+ return 0;
+}
diff --git a/person.hpp b/person.hpp
new file mode 100644
index 0000000..66ec7f0
--- /dev/null
+++ b/person.hpp
@@ -0,0 +1,12 @@
+#ifndef PERSON_H
+#define PERSON_H
+#include <string>
+
+struct Person {
+ std::string Name;
+ std::string Address;
+ std::string GetAddress() const { return Address; }
+ std::string GetName() const { return Name; }
+};
+
+#endif
diff --git a/sales_data.hpp b/sales_data.hpp
index 31929af..7cb3022 100644
--- a/sales_data.hpp
+++ b/sales_data.hpp
@@ -3,14 +3,30 @@
#include <string>
struct SalesData {
std::string isbn() const { return BookNo; }
- SalesData &combine(const SalesData);
+ SalesData &combine(const SalesData&);
double avg_price() const;
std::string BookNo;
unsigned int UnitsSold = 0;
double Revenue = 0.0;
};
+double SalesData::avg_price() const {
+
+ if (UnitsSold)
+ return Revenue / UnitsSold;
+ else
+ return 0;
+}
+
+SalesData& SalesData::combine(const SalesData &rhs) {
+
+ UnitsSold += rhs.UnitsSold;
+ Revenue += rhs.Revenue;
+ return *this;
+}
+
SalesData add(const SalesData&, const SalesData&);
std::ostream &print(std::ostream&, const SalesData&);
std::istream &read(std::istream&, SalesData&);
+
#endif