In: Computer Science
A customer can order one or more items in one transaction (طلبية) . Every item has a unique ID, Name, Price, and Description. Given the following item class, assume it is already programmed and ready for use.
Class Item {
public:
Item();
void SetID(int); int GetID();
void SetPrice(double); double GetPrice();
void SetName(string); string GeName();
void SetDesc(string); string GetDesc();
private:
int id;
String name, description;
double price;
};
Define a new class called Transaction that represents a customer order, where it contains 1 or more items. The Transaction class should have: (4 Points)
Define the following member functions:
1) Default constructor that initializes the dynamic arrays to null and the numItems and size to zero, also it sets the id and increase count. (4 Points)
2) Constructor that receives array of items, array of quantities of items, and the number of items. Then initializes the Items, Qtys, and numItems accordingly(وفقا لذلك). It should set the transaction to contain these items and quantities. And it sets the id and increase count. (8 Points)
NOTE: you should reserve (يحجز) an array size that equals the number of items + 5.
3) int Exists(int ItemID), checks if the item exists then returns its index, otherwise returns -1. (5 points)
4) void resize(), which increases the size of items and QTY arrays with 5 more slots. (5 points)
5) void AddItem(Item i, int Qty), which should add item i to the current items. If the item is already exists only the quantity of this item is increased by Qty. Also, if the current array is full, a new bigger array (with 5 more locations) should be created where all old items are put in the new array. (8 Points)
6) void RemoveItem( int itemID), which should remove item with id itemID, and then shifts all items after it. For example Items: 1,3,5,6,7, remove 3 should result of 1,5,6,7. (6 Points)
7) double TotalValue() which returns the total value a customer should pay for the current transaction (5 Points)
8) Destructor, should decrease count, and free allocated memory. (2 Points)
9) int getID(), which returns the transaction’s ID. (2 Points)
10) bool hasMoreItems(Transaction &t2) which returns true if original transaction has more item than Transaction t2. Otherwise, return false. (8 Points)
11) int similarItems(Transaction &t2), which compares original transaction, with transaction t2, and return number of similar items. (8 Points)
12) Write a program that uses classes Transaction and Item, , which does the following:
Item.hpp
#include <string>
class Item {
public:
Item();
void SetID(int);
int GetID();
void SetPrice(double);
double GetPrice();
void SetName( std::string);
std::string GeName();
void SetDesc( std::string);
std::string GetDesc();
private:
int id;
std::string name, description;
double price;
};
Transaction.hpp
#include <string>
#include <vector>
#include <item.hpp>
class Transaction{
public:
Transaction();
~ Transaction();
Transaction(std::vector<Item> items, std::vector<int> qtys,int numItems);
std::vector<Item> items;
std::vector<int> qtys;
int numItems;
int size;
static int count;
int Exists(int itemID);
void resize();
void AddItem(Item i, int Qty);
void RemoveItem( int itemID);
double TotalValue();
int getID();
bool hasMoreItems(Transaction &t2);
int similarItems(Transaction &t2);
int id; };
Transaction.cpp
#include "Transaction.hpp"
Transaction::Transaction()
{
numItems = 0;
size = 0;
count = count+1;
id = count;
}
Transaction::~Transaction()
{
count = count-1;
}
Transaction::Transaction( std::vector<Item> items, std::vector<int> qtys,int numItems){
items = this->items;
qtys = this->qtys;
numItems = this->numItems;
count = count+1;
id = count;
}
int Transaction::Exists(int itemId){
for(int i=0;i<items.size();i++){
if(items.at(i).GetID() ==itemId)
return i;
}
return -1;
}
void Transaction::resize(){
items.resize(items.size()+5);
qtys.resize(qtys.size()+5);
}
void Transaction::AddItem(Item item,int qty){
items.push_back(item);
qtys.push_back(qty);
}
void Transaction::RemoveItem(int itemId){
for(int i=0;i<items.size();i++){
if(items.at(i).GetID() ==itemId){
//remove
}
}
}
double Transaction::TotalValue(){
double totalitem = 0;
for(int i=0;i<items.size();i++){
totalitem = totalitem+ qtys.at(i)*items.at(i).GetPrice();
}
return totalitem;
}
int Transaction::getID(){
return id;
}
bool Transaction::hasMoreItems(Transaction &t2){
if(items.size()<t2.items.size()){
return true;
}
return false;
}
int Transaction::similarItems(Transaction &t2){
int similaritems = 0;
for(int i=0;i<items.size();i++){
for(int j=0;j<t2.items.size();j++){
if(items.at(i).GetID() ==t2.items.at(j).GetID()){
similaritems = similaritems+1;
}
}
}
return similaritems;
}
main.cpp
#include <Transaction.hpp>
#include <Item.hpp>
#include<stdio.h>
#include<vector>
int main (){
std::vector<Transaction>myTransctions;
for(int i=0;i<5;i++){
int j;
cout << "Enter the items in transaction - "<< i;
cin >> j;
for(int k=0;k<j;k++){
std::string name;
int id;
int qty;
double price;
cout << "Enter item name for - "<< k;
cin << name;
cout << "Enter item id for - "<< k;
cin << id;
cout << "Enter quantity for item - "<< k;
cin << qty;
cout << "Enter price for item - "<< k;
cin << price;
Item item;
item.SetID(id);
item.SetName(name);
item.SetPrice(price);
Transaction transaction;
transaction.items.push_back(item);
transaction.qtys.push_back(qty);
myTransctions.push_back(transaction);
}
}
double totlavalue;
for(int i=0;i<5;i++){
totlavalue= totlavalue + myTransctions.at(i).TotalValue();
}
cout<<"Total value --" << totlavalue;
Transaction highestvalue = myTransctions.at(0);
for(int i=0;i<5;i++){
if(myTransctions.at(i).TotalValue()>highestvalue.TotalValue()){
highestvalue = myTransctions.at(i);
}
}
cout<< "Details about highest transaction";
cout << "Number of items = "<< highestvalue.items.size();
cout << "Transaction Id = "<< highestvalue.id;
}