lecture 1 & 2 – case study tcp1201: 2013/2014. case study – book store system you are...

10
Lecture 1 & 2 – Case Study TCP1201: 2013/2014

Upload: rudolph-norman

Post on 29-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Lecture 1 & 2 – Case Study

TCP1201: 2013/2014

Page 2: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study – Book Store System

You are assigned to create a simple system for a bookstore that keeps track of the stock of the books. The system should be able to:

Store the following details; title, author (assume only one author for each book), price and number-in-stock for a particular book title.

Store the following details; name, email, gender for a particular author.

Allow the details of the books and authors to be retrieved.

a) Design the UML class diagrams for this system.

b) Write the C++ program for this system.

Page 3: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study– Design of Class Diagrams

Book

-title: string-author: Author-price: double-qtyInStock:int = 0

+Book(name:string, author:Author, price:double, qtyInStock:int)+getName(): string+getAuthor():Author+getPrice():double+setPrice(price:double):void+getQtyInStock():int+setQtyInStock(qtyInStock:int):void+print():void

Author

-name: string-email:string-gender:char

+Author(name:string, email:string, char gender)+setEmail(email:string):void+getName():string+getEmail():string+getGender():string+print():void

11

Page 4: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study– Class Interfaces

class Author { string name; string email; char gender; public: Author(string name, string email, char gender) : name(name), email(email), gender(gender) {} void setEmail(string email); string getName(); string getEmail(); char getGender(); void print();};

Page 5: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study– Class Interfaces

class Book { string title; Author author; double price; int qtyInStock;

public: Book(string title, Author author, double price, int qtyInStock): title(title), author(author), price(price), qtyInStock(qtyInStock) { } void setPrice(double price); void setQtyInStock(int qtyInStock); string getTitle(); Author getAuthor(); double getPrice(); int getQtyInStock(); void print();};

Page 6: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study– Class Implementationsvoid Book::setPrice(double price) { this->price = price; }

void Book::setQtyInStock(int qtyInStock){ this->qtyInStock = qtyInStock; }

string Book::getTitle() { return title; }

double Book::getPrice() { return price; }

int Book::getQtyInStock() { return qtyInStock; }

Author Book::getAuthor() { return author; }

void Book::print() { cout << "\nTitle\t: " << title << endl; cout << "Author\t: " << author.getName() << endl; cout << "Price\t: " << price << endl; cout << "Quantity: " << qtyInStock << endl;}

Page 7: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study– Class Implementations

void Author::setEmail(string email) { this->email = email;

}

string Author::getName() { return name;

}

string Author::getEmail() { return email;

}

void Author::print() { cout << "\n\nAuthor Details:" << endl << endl; cout << "Name: " << name << endl; cout << "Email: " << email << endl;}

Page 8: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study – Using the classes (Driver)

int main() { vector<Book> books;

Author author1("Jane Austen","[email protected]",'F'); books.push_back(Book("Pride and Prejudice", author1, 39, 2)); books.push_back(Book("Sense and Sensibility", author1, 30, 3)); books.push_back(Book("Emma", author1, 50, 1));

Author author2("Charles Dickens","[email protected]",'M'); books.push_back(Book("Oliver Twist", author2, 25, 1)); books.push_back(Book("A Chirstmas Carol", author2, 35, 5));

for (int i=0; i<books.size(); i++) books[i].print();

}

Page 9: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study – Output

Page 10: Lecture 1 & 2 – Case Study TCP1201: 2013/2014. Case Study – Book Store System You are assigned to create a simple system for a bookstore that keeps track

Case Study – Creating a Bookstore

Now, how do we use the Book and Author classes to create a more complete Bookstore system?

What are the functions needed for a Bookstore system?

• Add and delete books• Search for books by Title• Search for books by Author• Change the price for a book• Change the quantity in stock for a

book Can you create a Bookstore system now

using the Book and Author classes?