function test()
count = 1;
AmountDue = 0;
reply = MainMenu();
AmountDue = CalculateAmount(reply, AmountDue);
reply = MoreDrink();
while reply == 'Y'
reply = MainMenu();
if reply == '3'
break;
end
count = count + 1;
if count > 3
fprintf('You choose maximum order\n');
count = count - 1;
break;
end
AmountDue = CalculateAmount(reply, AmountDue);
reply = MoreDrink();
end
fprintf('Number of Drinks = %d\n', count);
fprintf('Amount Due = RM%.2f\n', AmountDue');
end
function reply = MainMenu()
clc;
fprintf('\t1:\tHot Drink\tRM1.00\n');
fprintf('\t2:\tCold Drink\tRM1.00\n');
fprintf('\t3:\tQuit\n');
reply = input('Please choose 1,2,3 [1]:', 's');
if isempty(reply)
reply = '1';
end
if reply > '3' || reply < '1'
reply = '1';
end
end
function AmountDue = CalculateAmount(reply, AmountDue)
switch(reply)
case '1'
AmountDue = AmountDue + 1;
fprintf('You choose Hot Drink\n');
sugarreply = SugarMenu();
if sugarreply == 'Y'
AmountDue = AmountDue + 0.5;
end
milkreply = MilkMenu();
if milkreply == 'Y'
AmountDue = AmountDue + 0.5;
end
case '2'
AmountDue = AmountDue + 1;
fprintf('You choose Cold Drink\n');
sugarreply = SugarMenu();
if sugarreply == 'Y'
AmountDue = AmountDue + 0.5;
end
milkreply = MilkMenu();
if milkreply == 'Y'
AmountDue = AmountDue + 0.5;
end
end
end
function reply = SugarMenu()
sugarreply = input('Do you want to add sugar? Y/N [Y]: ', 's');
sugarreply = upper(sugarreply);
if sugarreply == 'Y'
reply = 'Y';
elseif sugarreply == 'N'
reply = 'N';
else
reply = 'Y';
end
end
function reply = MilkMenu()
milkreply = input('Do you want to add milk? Y/N [Y]: ', 's');
milkreply = upper(milkreply);
if milkreply == 'Y'
reply = 'Y';
elseif milkreply == 'N'
reply = 'N';
else
reply = 'Y';
end
end
function reply = MoreDrink()
morereply = input('More Drink to order Y/N [N]: ', 's');
morereply = upper(morereply);
if morereply == 'Y'
reply = 'Y';
elseif morereply == 'N'
reply = 'N';
else
reply = 'N';
end
end