diff --git a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Order.h b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Order.h new file mode 100644 index 0000000..4030fb6 --- /dev/null +++ b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Order.h @@ -0,0 +1,34 @@ +#ifndef ORDER_H +#define ORDER_H +#include +#include +#include "Product.h" +using namespace std; + +class Order{ +private: + int orderID; + string customerName; + vector orderedProducts; + +public: + Order(int id, const string& name) + : orderID(id), customerName(name) {} + + void addProduct(Product* product) { + orderedProducts.push_back(product); + } + double calculateOrderTotal() const { + double total=0.0; + for(const auto& product : orderedProducts){ + total+=product->getProductPrice(); + } + return total; + } + + int getOrderID() const { return orderID; } + string getCustomerName() const { return customerName; } + const vector& getOrderedProducts() const { return orderedProducts; } +}; + +#endif \ No newline at end of file diff --git a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Product.h b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Product.h new file mode 100644 index 0000000..8f988f3 --- /dev/null +++ b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/Product.h @@ -0,0 +1,22 @@ +#ifndef PRODUCT_H +#define PRODUCT_H + +#include +using namespace std; + +class Product{ +private: + int productID; + string productName; + double productPrice; + +public: + Product(int id, const string& name, double price) + : productID(id), productName(name), productPrice(price) {} + + int getProductID() const { return productID; } + string getProductName() const { return productName; } + double getProductPrice() const { return productPrice; } +}; + +#endif \ No newline at end of file diff --git a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-01-library-management-system.cpp b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-01-library-management-system.cpp index 4a06271..daea554 100644 --- a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-01-library-management-system.cpp +++ b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-01-library-management-system.cpp @@ -37,7 +37,28 @@ using namespace std; /* Solution */ - +class Book{ +private: + string title; + string author; + int publicationYear; + string isbn; + +public: + void setBookDetails(string t, string a, int year, string i){ + title=t; + author=a; + publicationYear=year; + isbn=i; + } + + void displayBookDetails(){ + cout<<"Title: "<0){ + balance+=amount; + cout<<"Deposit:"<0 && amount<=balance){ + balance=balance-amount; + cout<<"Withdraw:"< - +#include "Product.h" +#include "Order.h" using namespace std; /* @@ -65,9 +66,9 @@ int main() { Order order(1, "John Doe"); // Add products to the order - order.addProduct(p1); - order.addProduct(p2); - order.addProduct(p3); + order.addProduct(&p1); + order.addProduct(&p2); + order.addProduct(&p3); // Calculate the order total double total = order.calculateOrderTotal(); @@ -77,8 +78,8 @@ int main() { cout << "Customer Name: " << order.getCustomerName() << endl; cout << "Ordered Products:" << endl; - for (const Product& product : order.getOrderedProducts()) { - cout << " - " << product.getProductName() << endl; + for (const Product* product : order.getOrderedProducts()) { + cout << " - " << product->getProductName() << endl; } cout << "Total: $" << total << endl; diff --git a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-06-library-oop-principles.cpp b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-06-library-oop-principles.cpp index 2738acb..e53af76 100644 --- a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-06-library-oop-principles.cpp +++ b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-06-library-oop-principles.cpp @@ -1,8 +1,6 @@ - #include - +#include using namespace std; - /* Exercise: Create a program to manage a library with books using Object-Oriented Programming (OOP) principles in C++. @@ -40,6 +38,29 @@ using namespace std; class Book { // TODO: +private: + string title; + string author; + int year; + +public: + Book(){ + title=""; + author=""; + year=0; + } + + Book(const string& t, const string& a, int y){ + title=t; + author=a; + year=y; + } + + void display() const { + cout<<"Title:"<> year; + library[i]=Book(title, author, year); } cout << endl << "Library Contents:" << endl; for (int i = 0; i < librarySize; ++i) { cout << "Book " << i + 1 << ":" << endl; // TODO: + library[i].display(); + cout << endl; } } \ No newline at end of file diff --git a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-07-bank-account-basics.cpp b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-07-bank-account-basics.cpp index d8aa3b2..fb3324f 100644 --- a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-07-bank-account-basics.cpp +++ b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-07-bank-account-basics.cpp @@ -35,7 +35,53 @@ using namespace std; Solution */ class BankAccount { -} +private: + int accountNumber; + double balance; + static int accountCount; + +public: + BankAccount(int accNumber, double initialBalance=0.0){ + accountNumber=accNumber; + balance=initialBalance; + accountCount++; + cout<<"BankAccount created. Account Number: "<0){ + balance+=amount; + cout<<"Deposited "<0 && amount<=balance){ + balance=balance-amount; + cout<<"Withdrew "<next=head; + head=newNode; + if(tail==nullptr){ + tail=newNode; + } + count++; + return *this; + + } + + LinkedList& display(){ + Node* temp = head; + while(temp!=nullptr){ + cout<<"["<data<<" | "<next<<"]"; + if (temp->next!=nullptr){ + cout<<" ---> "; + } + temp=temp->next; + } + cout<data == val){ + return true; + } + temp=temp->next; + } + return false; + } + + LinkedList& remove(int val){ + Node* prev=nullptr; + Node* current=head; + + while(current!=nullptr){ + if(current->data == val){ + if(prev==nullptr){ + head = current->next; + if(head==nullptr){ + tail = nullptr; + } + } + else{ + prev->next = current->next; + if(current==tail){ + tail=prev; + } + } + cout<<"delete on "<data<next; + } + return *this; +} + + LinkedList& append(int val){ + Node* newNode=new Node(val); + if(!head){ + head=tail=newNode; + } else { + tail->next=newNode; + tail=newNode; + } + count++; + return *this; + } + + bool isEmpty() { return head == nullptr; } + + + int size() { return count; } + + ~LinkedList(){ + cout<<"\nDestructor: "<data<next; + delete toDelete; + } + } +}; int main() { diff --git a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-13-stack.cpp b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-13-stack.cpp index 8418a26..56c0c0d 100644 --- a/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-13-stack.cpp +++ b/1-CLASSES-OBJECTS-AND-DATA-STRUCTURES/homework/homework-13-stack.cpp @@ -1,5 +1,6 @@ -#include "LinkedList.h" -#include "LinkedList.h" +#include + +using namespace std; /* Definition of Stack: @@ -65,6 +66,119 @@ /* Solution */ +struct Node{ + int data; + Node* next; + Node(int val){ + data=val; + next=nullptr; + } +}; + + +class LinkedList{ +private: + Node* head; +public: + LinkedList() : head(nullptr){} + + Node* getHead() {return head;} + + void insertAtHead(int val){ + Node* newNode=new Node(val); + newNode->next=head; + head=newNode; + } + + void deleteHead() { + if(head==nullptr){ + return; + } + Node* temp = head; + head = head->next; + delete temp; + } + + bool isEmpty() { return head == nullptr; } + + void display(){ + if(isEmpty()){ + cout<<"Stack is empty!"<data<<" | "<next<<"]"; + if(temp->next){ + cout<<" ---> "; + } + temp=temp->next; + } + cout<next; + cout<<" delete on "<data<data<<" | "<next<<"]"<next=newNode; + tail=newNode; + } + } + + int deleteHead(){ + if(head==nullptr){ + throw underflow_error("Queue underflow"); + } + Node* temp=head; + int val=temp->data; + head=head->next; + if(head==nullptr){ + tail=nullptr; + } + delete temp; + return val; + } + + bool isEmpty() {return head==nullptr;} + + ~LinkedList(){ + while(head){ + Node* temp=head; + head=head->next; + delete temp; + } + } +}; + +class Queue{ +private: + LinkedList list; + int count; +public: + Queue() : count(0) {} + void enqueue(int val){ + list.insertAtTail(val); + count++; + } + + int dequeue(){ + if(isEmpty()){ + cout<<"Nothing to dequeue (^_^)"<data; + } + + bool isEmpty() {return list.isEmpty();} + + int size() {return count;} +}; int main() {