Best writers. Best papers. Let professionals take care of your academic papers

Order a similar paper and get 15% discount on your first order with us
Use the following coupon "FIRST15"
ORDER NOW

Given UML, learn to write a class declaration. Learn how to define/call a member function. Learn how to define/call constructor, accessor, mutator or toString( ) 1. Inside your Assignment.cpp, for all those parts where you need to get user input for an int, a double or a char, etc, make sure you use

Question

c++ using code blockscode skeleton included at bottom.

Objectives

  • Given UML, learn to write a class declaration.
  • Learn how to define/call a member function.
  • Learn how to define/call constructor, accessor, mutator or toString( )
  • 1. Inside your Assignment.cpp, for all those parts where you need to get user input for an int, a double or a char, etc, make sure you use stringstream after using getline() function. Do NOT simple use cin >> to get an integer (for example: id), a double (for example, price, amount, etc). Your code may work when testing on your own PC, but it will NOT work on submission server.
  • 2. Make sure you use getline() to get user input when you want to get a string, for example a name.
  • 3. This is because: on a Windows machine, End-of-Line mode contains two marks – CR and LF, where on a Linux/Unix machine it only contains one – LF, so when you submit your file on server, it will treat the End-of-Line after cin>> as two tokens instead of one, that may mass up the input file and show multiple “Unknown command” on submission server although your code may run perfectly on your own PCs.Assignment descriptionAccording to the following UML diagram, design a Product class.
Product
-id: int = 0
-name: string = “?”
-price: double = 0.0
+Product()
+Product(int, string, double)
+getID(): int
+getName(): string
+getPrice(): double
+setID(int): void
+changeName(string):void
+raisePrice(double):void
+discount(double): void
+toString(): string
+equals(Product): bool

Member variables and member functions’ description

Member VariableData TypeDescription
idintThis represents the product’s unique ID, such as 1234
namestringThis represents the product’s name, such as “Dish Washer”.
pricedoubleThis represents the product’s price, such as 394.00
Member FunctionFunction Description
Product()This is the default constructor and it should initialize all member variable by the initial value defined inside the UML. For example, id should be initialized to 0, name should be initialized to “?” and price should be initialized to 0.0.
Product(int newID, string newName, double newPrice)This is the overloadded constructor. It takes three input parameters and initialize the three member variables accordingly with the three input parameters.
int getID()This is the accessor for member variable id
string getName()This is the accessor for member variable name
double getPrice()This is the accessor for member variable price
void setID(int newID)This is the mutator for member variable id. It takes a new id as input and change the member variable id accordingly
void changeName(string newName)This is a mutator for member variable name. It takes a new name as input and change the product’s name to this new name accordingly.
void raisePrice(double increaseAmt)This is a mutator for member variable price. It takes an amount as input parameter and increase the product’s price by that amount.
void discount(double discountRate)This is another mutator for member variable price. It takes a discount rate which is a double value, such as 0.1 and reduces the product’s price by that discount.
string toString()The toString function will display a product’s info. in the following format:
nProduct ID:t id
nProduct Name:t name
nProduct Price:t pricenn
bool equals(Product anotherProd)The function compares the current product’s id and name with another product’s id & name, it will returns true if both products have the same id and name. Otherwise it returns false.

(Note: You should NOT use == to compare two strings since they’re reference variables and will always return false. Instead you should use the compare() function in string class to compare them, for example to check two string objects str1 and str2 are same strings or not, we wrote:
if (str1.compare(str2) == 0)
cout << “Same” << endl;
else
cout << “Different” << endl;

Programming Instructions

1. First, you will need to create two files to represent this class:

  • Product.h : this is the header file which declares the Product class
  • Product.cpp : this is the class implementation file which shall include all function’s implementation

2. Also, you are given a skeleton of Assignment.cpp file which is used to test and check the Product class. Your main program will do the following: Declare and instantiate a Product object, get the product’s id, name and price, it then display the following menu to the user:

Choose an Action
——————-
C Change Name
D Give Discount
R Raise Product Price
F Find the Product
S Show Product Info.
? Display the Menu
Q Exit Program

Then the program should ask “Please enter a command: ”. A user will type in a character of their menu choice. Note: user might enter both upper case or lower case letters. You can use toupper( ) function in <cctype> directive to convert them all to upper case letters. Please find below for each command’s description

CommandCommand Description
‘C’ This option will allow user to change the product’s name. Show a message on screen:
nPlease enter the new product’s name:

The program then get the user’s input and call member function changeName( ) accordingly.
‘D’Ask a user to enter a discount rate and call the discount( ) function in Product.cpp class. Showing, for example, the following message on screen:

nYou gave 10.00% discount in price
nNow the new price is: $ 359.55
‘R’Ask a user to enter an amount, and call the raisePrice( ) function in Product.cpp class to raise its price by entered amount. Showing, for example, the following message on screen:

nYou increased $ 100.00 in price
nThe new price is: $ 459.55
‘F’Ask user to enter a product’s id and name, and set these values to the second Product object that you will need to create. Then call the equals( ) function with these two Product objects (the first one you created and this second one) to check if they have the same id and the name. If they are same, then call toString() function to print out the first Product object info. on screen. Otherwise print the following message on screen:

nSorry we cannot find the product!n
‘S’This will call the toString( ) function and print the product’s info. on screen.
‘?’Display the menu again. Please create the following displayMenu() function in Assignment.cpp and call it from the main function.

void displayMenu()
‘Q’Quit the program and exit.

Functions in Assignment.cpp file

In Assignment.cpp, besides the main( ) function, you’re required to design at least the following two functions inside! Feel free to design other functions whenever you feel necessary.

void getProductInfo(int& prodcutID, string& productName, double& productPrice );
void displayMenu();

Submission

1. Follow this order to complete the assignment: complete Product.h first, then Product.cpp, last finish the Assignment.cpp

2. To make the program to work, you will need to create a project, then add above three source codes inside the project before you can run and test your program. Click here for instruction on how to create a project under Code::Blocks

3. Test your program by using the inputs from the following two input test cases. Read this short description on how to use test cases for your lab or assignments

4. You need to submit Product.h, Product.cpp and Assignment.cpp three files on the following submission website (Be careful about your file’s name, no empty space is allowed!)

Sample run of . User input are in bold and enclosed with [ ]

Welcome to Product Entry Screen
===============================

What’s the product ID: [1234]

Enter the name of the product: [Dish Washer]

What’s the product’s original price: [399.50]

Choose an Action
——————-
C Change Name
D Give Discount
R Raise Product Price
F Find the Product
S Show Product Info.
? Display the Menu
Q Exit Program


Please enter a command: [D]

Please enter discount rate (such as 0.15): [0.10]

You gave 10.00% discount in price
Now the new price is: $ 359.55

Please enter a command: [S]

Product ID: 1234
Product Name: Dish Washer
Product Price: 359.55


Please enter a command: [C]

Please enter the new product’s name: [Whirlpool Dish Washer]

Please enter a command: [r]
Amount to raise: [100.0]

You increased $ 100.00 in price
The new price is: $ 459.55

Please enter a command: [s]

Product ID: 1234
Product Name: Whirlpool Dish Washer
Product Price: 459.55


Please enter a command: [F]

Please enter the product’s ID: [4321]

Please enter the product’s name: [Whirlpool Dish Washer]

Sorry we cannot find the product!

Please enter a command: [f]

Please enter the product’s ID: [1234]

Please enter the product’s name: [Whirlpool Dish Washer]

Find the product!
Product ID: 1234
Product Name: Whirlpool Dish Washer
Product Price: 459.55


Please enter a command: [d]

Please enter discount rate (such as 0.15): [0.15]

You gave 15.00% discount in price
Now the new price is: $ 390.62

Please enter a command: [S]

Product ID: 1234
Product Name: Whirlpool Dish Washer
Product Price: 390.62


Please enter a command: [q]

CODE SKELETON:

// File Name: Assignment6.cpp

// Name: your name

// StudentID: your id

// Description: (write a short description)

//

// Note: (1)//—- is where you need to add your own codes

// (2)User prompts marked with //**** need to be commented out

// when submitting on server. This is to gurantee your program

// output will be same as our solution

#include “Product.h”

#include <iostream>

#include <sstream>

#include <iomanip>

#include <cctype>

using namespace std;

//This function gets initial product’s info. from user

void getProductInfo(int& prodcutID, string& productName, double& productPrice );

void displayMenu();

int main()

{

//variable used to store the product’s id, name and price

int id;

string name;

double price;

//call getProductInfo() function, save the relevant info. inside

//above declared variables – pass by reference

//——

//create a Product object called ‘oneProduct’ by using above

//id, name and price

//—-

//call displayMenu() function to display the menu here

//—-

cout << fixed << showpoint << setprecision(2);

char choice;

string input ;

do

{

cout << “nPlease enter a command: “; //****

getline(cin, input);

stringstream inputStream(input);

inputStream >> choice;

if (toupper(choice) != ‘Q’ )

{

switch (toupper(choice))

{

case ‘C’: //change the product’s name

{

string newName;

cout << “nPlease enter the new product’s name: “; //****

//—-

//call member function changeName() to change it

//—-

}

break;

case ‘D’: //Give discount on the product

{

double rate;

cout << “nPlease enter discount rate (such as 0.15): “; //****

//get the discount rate, use stringstream

//—-

//call member function discount()

//—-

//show the resulting new price on screen by calling

//member function getPrice()

cout << “nYou gave ” << //—- <<“% discount in price”;

cout << “nNow the new price is: $ ” << //—-;

}

break;

case ‘R’: //Raise the price

{

double amount;

cout << “Amount to raise: “; //****

//use stringstream to get amount

//—-

//call member function raisePrice() to change the price

//—-

cout << “nYou increased $ ” << //—- << ” in price”;

cout << “nThe new price is: $ ” << //—- ;

}

break;

case ‘F’: //Find out whether the product same as the current one

{

int id; //second product’s id

string name; //second product’s name

cout << “nPlease enter the product’s ID: “; //****

getline(cin, input);

stringstream inputStream(input);

inputStream >> id;

cout << “nPlease enter the product’s name: “; //****

getline(cin, name);

//create a second Product object called ‘anotherProd’

//by calling the constructor. Initialize the price to be 0.0

//—-

//Check whether ‘anotherProd’ have the same id and name

//as ‘oneProduct’, if yes, show ‘nFind the product!’

//on screen and call toString() on oneProduct

//otherwise show ‘nSorry we cannot find the product!’

//—-multiple lines

}

break;

case ‘S’: //call toString() to show the product’s info. on screen

cout << //—- << endl;

break;

case ‘?’: //display menu again

displayMenu();

break;

default:

cout << “nUnknown commandn” << endl;

break;

}

}

}

while (toupper(choice) != ‘Q’);

return 0;

}

//*****************************************************************************

void getProductInfo(int& productID, string& productName, double& productPrice )

{

string ;

cout << “Welcome to Product Entry Screenn”;

cout << “===============================n”;

cout << “nWhat’s the product ID: “; //****

getline(cin, userInput);

stringstream inputStream(userInput);

inputStream >> productID;

cout << “nEnter the name of the product: “; //****

getline(cin, productName);

cout << “nWhat’s the product’s original price: “; //****

getline(cin, userInput);

//create a new stringstream object, convert string into product price

//—-

//—-

}

//****************************************************

// DisplayMenu() on screen

void displayMenu()

{

cout << “nChoose an Actionn”;

cout << “——————-n”;

cout << ” Ct Change Namen”;

cout << ” Dt Give Discountn”;

cout << ” Rt Raise Product Pricen”;

cout << ” Ft Find the Productn”;

cout << ” St Show Product Info.n”;

cout << ” ?t Display the Menun”;

cout << ” Qt Exit Programnn”;

 
Looking for a Similar Assignment? Order now and Get 10% Discount! Use Coupon Code "Newclient"