%ME 345 Cash Register example

clc;
clear;

format bank             %cleaner format for numbers here

penny_count=0;          %track # of coins/bills to return
nickle_count=0;
dime_count=0;
quarter_count=0;
dollar_count=0;
five_count=0;
ten_count=0;
twenty_count=0;

item=0;                 %item they choose from the menu
quantity=0;             %number of items they order (i.e. 3 fries, etc)

total=0;                %running total of the bill

value=0;
paid=0;
change=0;

while item ~= 5 
    disp('Item #      Item           Price');    
    disp('1         Hamburger       $7.75');
    disp('2         Cheese Burger    9.00');
    disp('3         French Fries     2.00');
    disp('4         Soda             1.75');
    disp('5         That is all');
    disp(' ');          %gives a blank line for cleanliness
    item=input('Enter item number please.');
    if item ==1
        value=1.75;
    elseif item == 2
        value=2;
    elseif item == 3
        value=1;
    elseif item == 4
        value=0.75;
    elseif item == 5
        disp('One moment please')
    else
        disp('invalid entry')
    end

    if item > 0 & item < 5
        quantity=input('How many would you like?');
        total=total+(quantity*value);
    end
end

%This loop determines whether the purchase is taxed - if so, will calculate
%the tax and add to the total

disp(' ');          
pay_tax=input('Is this purchase taxable at 7%? Press 1 for yes, 2 for no ');
if pay_tax==1
    total=1.07*total;
end

%Want to 'round down' the total to the lowest penny
total=floor(total*100)/100;

% This is a check to make sure they are paying more (or equal to) the bill
while paid < total
    disp('Total');
    disp(total);
    paid=input('Enter amount you will be paying with:  ');
    if paid < total
        disp('Insufficient amount paid')
    end
end

change=paid-total;
disp('Change:');
disp(change);

%Change is the change that they need to pay. For every denomination,
%subtract off the amount of change returned and updated the number of said
%demonination

%WARNING: the "Penny Problem" has to do with roundoff error in Matlab. Even
%when you think a variable = 0, it may be something like 3.2 x 10^-16. This
%is a problem if you have something like "if change > 0", because 3.2 x
%10^-16 is in fact greater than zero. So once you have your solution, be
%sure to check that you are getting the right change!

while change > 0.0099               %this is to account for Penny Problem!
    if change >= 20
        change=change-20;
        twenty_count=twenty_count + 1;
    elseif change >= 10
        change=change-10;
        ten_count=ten_count + 1;
    elseif change >= 5
        change=change-5;
        five_count=five_count + 1;
    elseif change >= 1
        change=change-1;
        dollar_count=dollar_count + 1;
    elseif change >= 0.25
        change=change-0.25;
        quarter_count=quarter_count + 1;
    elseif change >= 0.10
        change=change-0.10;
        dime_count=dime_count + 1;
    elseif change >= 0.05
        change=change-0.05;
        nickle_count=nickle_count + 1;
    else 
        %note: beware the 'Penny Problem' due to roundoff error!!
        change=change-0.01;
        penny_count=penny_count + 1;            
    end
end

% Some more advanced formatting of the output. Not necessary for purposes
% of the ME 345 class. First create the appropriate strings, then display.
% There are other ways to do this which may be easier / more
% straightforward.

format short
string_twenties=['Twenties: ' num2str(twenty_count)];
string_tens=['Tens: ' num2str(ten_count)];
string_fives=['Five: ' num2str(five_count)];
string_ones=['Ones: ' num2str(dollar_count)];
string_quarters=['Quarters: ' num2str(quarter_count)];
string_dimes=['Dimes: ' num2str(dime_count)];
string_nickles=['Nickles: ' num2str(nickle_count)];
string_pennies=['Pennies: ' num2str(penny_count)];

disp(string_twenties);
disp(string_tens);
disp(string_fives);
disp(string_ones);
disp(string_quarters);
disp(string_dimes);
disp(string_nickles);
disp(string_pennies);

disp('Thank You Come Again!');
disp(' ');
disp(' ');


