Question
hey can you please explain this code and what was done exactly like how many loops and how was this made function pilot() clc %storing
hey can you please explain this code and what was done exactly like how many loops and how was this made
function pilot() clc
%storing the items, their prices and quantities items = ["Masks","Sanitizer","Gloves","Mouthwash"]; cost = [10,15,5,12]; quantities = [15,20,5,10];
userQuantites = zeros(size(items)); %vector to store purchased item number choice = showMenu(); while(choice ~= 3) %continuing until not exiting switch choice case 1 userQuantites = showAvailableList(items,cost,quantities,userQuantites); case 2 if(checkoutCart(items,cost,userQuantites)) userQuantites = zeros(size(items)); %reseting user quantity vector based on checkout status end otherwise disp("Wrong choice"); end choice = showMenu(); end
end
%function to show main menu and return user choice function choice = showMenu() disp("Welcome to the Shopping Portal. Please select any option to continue.") disp("1) Show List of Products") disp("2) Show Checkout Cart") disp("3) Exit program.") choice = input('Please input choice : '); end
%function to display the available commodities in table form and return %bought quantites function userQuantites = showAvailableList(items,cost,quantities, userQuantites) disp("Displaying available items in store") %displaying lsit of tiems fprintf("Items\t\t\tPrice\t\tQuantity "); for i=1:length(items) fprintf("%-15s\t%d AED\t\t%d ",items(i),cost(i),quantities(i)); end
[choice,quantity] = buyProducts(items); while(choice ~= 5) %taking input until customer dosent want to go back to main menu if(choice >= 1 && choice <= 4) %using if else to ensure choices are legit if (quantity > quantities(choice)) disp('Sorry, we do not have so much quantity for selected product') else userQuantites(choice) = quantity; %updating customer and shop inventory quantities(choice) = quantities(choice) - quantity; end else disp('Sorry, choice is invalid, please try again') end [choice,quantity] = buyProducts(items); end
end
%function to display product choice and return choice and quantity function [choice,quantity] = buyProducts(items) disp("Please select the Product You wish to buy from the list") %printing list of available items for i = 1:length(items) fprintf("%d)%s ",i,items(i)); end fprintf("5) Back to previous menu "); choice = input('Please enter your choice : '); %taking user quantity input if(choice ~= 5) quantity = input('Please enter required quantity : '); else quantity = 0; end end
%function to checkout customer, and return a flag based on checkout status function flag = checkoutCart(items,cost,userQuantites) disp("Products in your cart") sno = 1; for i=1:length(userQuantites) %printing cart data if(userQuantites(i) ~= 0) fprintf("%d. %-10s\t Quantity=%d\t\tUnit Price=%d AED ",sno,items(i),userQuantites(i),cost(i)); sno = sno + 1; end end choice = input('Do you Want to Checkout(Y/N): ','s'); if(choice == 'N') flag = false; %returning if customer wants to continue shopping return end name = input('Please enter your name : ','s'); address= input('Please enter your address : ','s'); item_cost = userQuantites.*cost;
%outputting bill summary fprintf("For user %s living at address %s ",name,address); disp('Summary of bill'); fprintf("Items \t\tquantity \tprice ") for i=1:length(userQuantites) if(userQuantites(i) ~= 0) fprintf("%-10s\t %d\t\t\t%d AED ",items(i),userQuantites(i),item_cost(i)); end end fprintf("The total amount you need to pay is %d AED ",sum(item_cost)); flag = true; end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started