Project Three Assessments Submission - Item 2 v1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Version 1

Individual Report Project Three

Student Name: Andrew Frost Assessment Item No.: 2

SID: 4959467

Assessment Title: Report on your individual contribution to the coding of the programming learning toolkit.

Evidence: (e.g. screenshots of code and testing etc.) book.cpp


class Book{

bookManager.h
12 13 class BookManager{ std::vector <Book*> bookList;

Explanation of the code: (in relation to use of classes) Here you can see I used two different classes. The first class called Book which was from book.h. This class stores the details of each individual book for the catalogue. This included title, price, author and isbn numbers. The 2nd class is called bookManager from bookManager.h. It was this class that will store each instance of the class book in an array. This was then used in another part of the program to allow an employee to add, view and delete books from the database of books. Evidence: (e.g. screenshots of code and testing etc.)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class Book{ string title; float price; string isbn; string author; #ifdef __linux string symbol = ""; #elif __win32 char symbol = 156; #endif public: Book(); void addBook(); void viewBook(); void deleteBook(); std::string getTitle(){return title;};

@Coventry University

Page 1

Version 1

Explanation of the code: (in relation to encapsulation) This code is an example of where I used Encapsulation from book.h. I made the variables that will store the data private. This makes sure the data cannot be edited outside of the class. This makes sure that details of the book cannot be accidentally changed without calling the correct functions from the class. The functions this meant that you were able to call the functions from different parts of the program.

Evidence: (e.g. screenshots of code and testing etc.)


17 18 19 20 21 22 23 //Output the details of selected book void Book::viewBook(){ cout << "Book Title: " << title << endl; cout << "Book Price: " << price << endl; cout << "Book ISBN: " << isbn << endl; cout << "Book Author " << author << endl; }

Explanation of the code: (in relation to methods/functions) This is one of the functions that I made within the class Book from the file book.cpp. The purpose of this function was to ask the employee to enter the details of the book that they wanted to add to the catalogue. This function was called in bookManager.cpp as this is where multiple books are stored in an array. Evidence: (e.g. screenshots of code and testing etc.)
12 13 class BookManager{ std::vector <Book*> bookList;

Explanation of the code: (in relation to use of appropriate arrays/lists) This code is from BookManager.h Within the class BookManager this creates a vector array of type class Book and calls it bookList. This is the array that will store all the different books for the catalogue. I used a vector array as they are not limited to a set size like a regular array is. This meant I could add an infinite amount of books to the catalogue depending on how many books they wanted to sell using bookList.push_back to add a new element to the end of the array. A vector array also makes it very easy to delete a record from the array using .clear(), allowing you to delete any element from the array. @Coventry University Page 2

Version 1

Evidence: (e.g. screenshots of code and testing etc.)


14 15 16 17 18 #ifdef __linux string symbol = ""; #elif __WIN32 char symbol = 156; #endif

Explanation of the code: (in relation to conditional statements and logic) This if statement was used in book.h. The purpose of this if statement was to correctly output the symbol in the console when displaying price. For Linux you can make a string and set it to where as with windows you have to create a char and set it to the ASCII value for the pound symbol. This checks what operating system you are running and uses the correct method to output the symbol.
Book.cpp 1 #include <iostream> 2 #include <string> 3 #include <vector> 4 5 #include "book.h" 6 7 using namespace std; 8 9 //Andrew 10 11 //Consturctor to prevent random values from showing when viewing a book that hasn't been added 12 13 //Ask the user to enter details for each book (Not used anymore) 14 void Book::addBook(){ 15 16 } 17 //Output the details of selected book 18 void Book::viewBook(){ 19 cout << "Book Title: " << title << endl; 20 cout << "Book Price: " << price << endl; 21 cout << "Book ISBN: " << isbn << endl; 22 cout << "Book Author " << author << endl; 23 } 24 //Clear the values of selected book 25 void Book::deleteBook() 26 { 27 title = " "; 28 price = 0; 29 isbn = " "; 30 author = " "; 31 cout << "Book Deleted" << endl; 32 } 33 34 Book::Book(){

@Coventry University

Page 3

Version 1

35 36 37 38 39 40 41 42 43 44 45 46 47 48

cout << "Enter the book Title: "; cin.ignore(); getline(cin,title); cout << "Enter the book Price: " ; cin >> price; cout << "Enter the book ISBN: "; cin >> isbn; cout << "Enter the book Author: "; cin.ignore(100,'\n'); getline(cin,author); std::cout << "Book Created" << std::endl; cin.ignore(); }

Book.h 1 #ifndef BOOK_H 2 #define BOOK_H 3 4 #include <string> 5 #include <vector> 6 7 using namespace std; 8 9 class Book{ 10 string title; 11 float price; 12 string isbn; 13 string author; 14 #ifdef __linux 15 string symbol = ""; 16 #elif __win32 17 char symbol = 156; 18 #endif 19 20 public: 21 Book(); 22 void addBook(); 23 void viewBook(); 24 void deleteBook(); 25 std::string getTitle(){return title;}; 26 //~ float getPrice(){return price;}; 27 }; 28 #endif bookManager.cpp 1 #include "bookManager.h" 2 #include "book.h" 3 #include <vector> 4 #include <iostream> 5

@Coventry University

Page 4

Version 1

6 7 void BookManager::addBook(){ 8 bookList.push_back(new Book); 9 std::cout << "Created Book:" << bookList.back()->getTitle() << std::endl; 10 } 11 12 void BookManager::viewBook(){ 13 int id = -1; 14 std::cout << "Please Enter the ID of the book you wish to view" << std::endl; 15 std::cin >> id; 16 if(id >= 0 and id < bookList.size()) 17 bookList[id]->viewBook(); 18 else 19 std::cout << "Error book not found" << std::endl; 20 std::cout << "\nPress Enter to continue" << std::endl; 21 std::cin.ignore(); 22 std::cin.get(); 23 } 24 25 void BookManager::deletesBook(){ 26 int id = -1; 27 std::cout << "Enter the ID of the book you wish to delete" << std::endl; 28 cin >> id; 29 if(id >= 0 and id < bookList.size()){ 30 delete bookList[id]; 31 bookList.erase(bookList.begin() + id); 32 } 33 elsex` 34 std::cout << "Error, book not found" << std::endl; 35 std::cout << "\nPress Enter to continue" << std::endl; 36 std::cin.ignore(); 37 std::cin.get(); 38 } 39 40 void BookManager::printBookList(){ 41 std::cout << "There are " << bookList.size() << " books" << std::endl; 42 std::cout << "ID | Title" << std::endl; 43 for(unsigned int i = 0; i < bookList.size(); i++){ 44 std::cout << i; 45 if(i < 10) 46 std::cout << " "; 47 else if(i < 100) 48 std::cout << " "; 49 std::cout << "| " << bookList[i]->getTitle() << std::endl; 50 } 51 cin.ignore(); 52 std::cout << "\nPress enter to continue" << std::endl; 53 cin.get(); 54 } 55 56 unsigned int BookManager::size(){ 57 //std::cout << "BookManager size = " << bookList.size() << std::endl; 58 return bookList.size(); 59 }; bookManager.h 1 #ifndef BOOKS_H 2 #define BOOKS_H 3 4 #include <vector> 5 #include <string>

@Coventry University

Page 5

Version 1

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#include <iostream> #include "book.h" using std::string; class BookManager{ std::vector <Book*> bookList; public: void addBook(); void printBookList(); void viewBook(); void deleteBook(); unsigned int size(); void test(){std::cout << "Working\n";}; string getTitle(int index){return bookList[index]->getTitle();}; }; #endif

@Coventry University

Page 6

You might also like