Hi guys,
I have created (as partly help to someone question here, in other part to try building it as exercise) and I have few questions.
I am not sure about few (at least) things:
1. How to create multiple instances of an object in my code
should I create
Product1 Product2 etc? How would that be done if user will decide how many products there will be? maybe 1 maybe 100??
2. how to delete Product as added entry by user?
3. my edit function is this how i should edit product, or is a better way?
4. Product overload - If I have similar functions, i use product overloading, however, how should i deal with, edit, view or add product? I want to add product 1, edit product 1 or view product 1.
what do you do with such situation????
void addProduct(int productID);
void editProduct(int productID);
void viewProduct(int productID);
5. Based on answer to first question i think my loop will have be changed to reflect correct way of doing it :D so I expect that I will have more questions.
my code is available here:
https://github.com/xxvms/Products-Add-View-Edit
and below listed
thanks :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
//Product add view modify and delte
#include "stdafx.h"
#include<iostream>
#include<cstdlib>
#include <string>
#include "Product.h"
int main()
{
int id = 0;
int choice = 9;
Product Product;
while (true)
{
std::cout << "Please make your selection: " << std::endl;
std::cout << "Press 1 to Add Product." << std::endl;
std::cout << "Press 2 to Edit Product." << std::endl;
std::cout << "Press 3 to View Product." << std::endl;
std::cout << "Press 4 to Delete Product." << std::endl;
std::cout << "Press 0 to Quit." << std::endl;
std::cin >> choice;
if (choice == 1)
{
std::cout << "Plese type product ID to Add" << std::endl;
std::cin >> id;
Product.addProduct(id);
}
else if (choice == 2)
{
std::cout << "Plese type product ID to edit" << std::endl;
std::cin >> id;
Product.editProduct(id);
}
else if (choice == 3)
{
std::cout << "Plese type product ID to view" << std::endl;
std::cin >> id;
Product.viewProduct(id);
}
else if (choice == 4)
{
//Product.~Product;
}
else if (choice == 0)
{
break;
}
}
system("pause");
return 0;
}
|
Product.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#pragma once
#include <string>
class Product
{
public:
Product();
void addProduct(int);
void viewProduct(char);
void editProduct(int);
~Product();
private:
char name;
int quantity;
double price;
int productID;
std::string description;
};
|
Product.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include "stdafx.h"
#include "Product.h"
#include <iostream>
#include <string>
Product::Product()
{
}
void Product::addProduct(int productID1)
{
system("cls");
productID = productID1;
std::cout << "Please type new Name: ";
std::cin >> name;
std::cout << "Please type new quantity: ";
std::cin >> quantity;
std::cout << "Please type new price: ";
std::cin >> price;
std::cout << "Please type new description: ";
std::cin >> description;
}
void Product::viewProduct(char name_)
{
system("cls");
std::cout << "Product ID: " << productID << std::endl;
std::cout << "Product Name: " << name << std::endl;
std::cout << "Quantity: " << quantity << std::endl;
std::cout << "Price: " << price << std::endl;
std::cout << "Product Description: " << description << std::endl;
}
void Product::editProduct(int productID_)
{
productID = productID_;
system("cls");
std::cout <<"Current name is " << name << " Please type new Name: ";
std::cin >> name;
std::cout << "Current quantity is " << quantity << " Please type new quantity: ";
std::cin >> quantity;
std::cout << "Current price is " << price << " Please type new price: ";
std::cin >> price;
std::cout << "Current description is " << description << std::endl;
std::cout << "Please type new description: ";
std::cin >> description;
}
Product::~Product()
{
}
|