Help with matrix that stores info/interactive?

1 view (last 30 days)
I'm trying to create a matrix/code for the following stipulations. First, create a matrix that stores the name of products and price. 1st and 2nd column should be the product name and its price, respectively. The number of product items should be flexible. Your program will ask to input product name, and it should display the price by looking at the matrix you defined. If the input product name is not in the list, it will display “Item is not in our store. Please retry.” This query should be repeated until you type in “Bye”.
How can I make a matrix interactive like this?

Accepted Answer

Will Fritz
Will Fritz on 18 Jul 2018
It is my understanding that you want to create some way of storing product/pricing information with the following stipulations:.
  • There should be no limit on how many products there are
  • The program will ask user to provide a potential product
  • If available the price of that object will be displayed
  • If 'Bye' is entered the program will exit
  • If the input is not valid the program should display 'Item is not in our store. Please retry.'
Based on your criteria I suggest using container.Map. The following example is one way of achieving the above:
%%Creating Map
% populate each variable
% Order determines which price is mapped to the specific product
productNames = {'car', 'basketball', 'pen'};
prices = [5000 20 1];
MyMap = containers.Map(productNames, prices);
% add a value to Map
MyMap('tv') = 300;
%%Using Map with User
readInput = true;
while(readInput)
userResponse = input('Please Input a product name: ', 's');
if strcmp(userResponse, 'Bye')
readInput = false;
else
if isKey(MyMap, userResponse)
disp(['Price for ', userResponse, ': $', num2str(MyMap(userResponse))]);
else
disp('Item is not in our store. Please retry');
end
end
end
Please refer to the container.Map documentation for information on how to operate on maps and the methods that come along with this data type
  2 Comments
Taylor Gates
Taylor Gates on 18 Jul 2018
Thanks! Would it be possible to use a matrix for the product/pricing and use switch with input to complete this? That's what I'm trying to work on now.
Will Fritz
Will Fritz on 18 Jul 2018
This is what a matrix would look like. MATLAB calls this particular one a 4x2 string array.
%%Creating Map
% populate each variable
% Order determines which price is mapped to the specific product
productNames = ["car", "basketball", "pen"];
prices = ["5000" "20" "1"];
MyMatrix = [productNames' prices'];
% add a value to Map
MyMatrix(4,1) = "tv";
MyMatrix(4,2) = "400";
What do you have in mind for the switch? The only constant thing it can match with is "Bye", everything else depends on the content of the matrix.

Sign in to comment.

More Answers (0)

Categories

Find more on Financial Toolbox in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!