Search code examples
arraysmatlabrandomreceipt

MATLAB receipt print random values issue


I have a project where I must make the following;

You have a small business and you sell 6 different products. Choose your products and their prices within the range of 20p to £25.00 (these could be completely fictitious). Your shop has 4 employees, one of whom will be at the till at the time of purchase. Your task is to write MATLAB code to prepare a receipt for a fictitious transaction as explained below. There is a customer at the till. They want to purchase 3 random products with specific quantities for each. For example, the customer wants 2 cappuccinos, 1 croissant and 6 raspberry muffins. (1) Select randomly 3 products from your list. For each product choose a random quantity between 1 and 9. (2) Calculate the total cost. (3) Choose randomly the staff member to complete the transaction. (4) Suppose that the price includes 20% VAT. Calculate the amount of VAT included in the price. (6) Prepare the receipt as text in the MATLAB command window. Use the current date and time (check datestr(now,0)). Your code should output the receipt in the format shown in the picture. There should be 60 symbols across. Choose our own shop name.

receipt example.

My code so far is the following:

clear all
clc
close all

items = {'apples   ','carrots  ','tomatoes','lemons   ','potatoes','kiwis   '};%    products
price = {3.10, 1.70, 4.00, 1.65, 9.32, 5.28};% item prices. I set spaces for each entry     in order to maintain the border format.
employee = {'James','Karina','George','Stacey'};%the employees array
disp(sprintf('+-----------------------------------------------+'));

disp(sprintf('|\t%s \t\t\tAlex''s Shop |\n|\t\t\t\t\t\t\t\t\t\t\t\t|',     datestr(now,0)));

totalPrice = 0;
for i = 1:3
    randItems = items {ceil(rand*6)};
    randprice = price {ceil(rand*6)};
    randQuantity = ceil(rand*9);% random quantity from 1 to 9 pieces
    randEmployee = employee{ceil(rand*4)};
    itemTotal = randprice * randQuantity;%total price of individual item
    totalPrice = totalPrice + itemTotal;

    disp(sprintf('|\t%s\t (%d) x %.2f = £ %.2f \t\t\t|', randItems, randQuantity, randprice, itemTotal))

end

disp(sprintf('|\t\t\t\t-----------------------------\t|'));

disp(sprintf('|\t\t\t\t\t\t\t\t\t\t\t\t|\n|\t Total to pay   \t £     %.2f\t\t\t\t|',totalPrice));

disp(sprintf('|\t VAT \t\t\t\t £ %.2f\t\t\t\t| \n|\t\t\t\t\t\t\t\t\t\t\t\t|',     totalPrice*0.2));

disp(sprintf('|\tThank you! You have been served by %s\t|\t', randEmployee));

disp(sprintf('+-----------------------------------------------+'));

My issue of course is the following. Upon choosing a random item from the items list, I then choose a random price to assign as well. I don't want this though. I would like to find a way to assign a preset price to each item to be printed automatically when generating a random item to be added to the basket. I hope this explanation is sufficient for you, if you have any questions feel free to ask. Thank you in advance.


Solution

  • When you write

    randItems = items {ceil(rand*6)};
    randprice = price {ceil(rand*6)};
    

    you calculate a random index into the array items, and then you calculate a random index into the array price. If you instead assign the index you calculate via ceil(rand*6) to a separate variable, called e.g. index, you can re-use it to pick, say, item #3 from both items and price. Thus, the ith item will always show up with the ith price.