I just spent three hours on this code, and I am so proud. As much as I hate my computer science class, I love moments when a program works. It's under the read more just because most people would think tumblr glitched if they randomly saw 116 lines of code. Yay!
function [updatedStruct payout] = payMachine(os, findName, findCost)
%Takes structures with people's wallet's contents and shows if they have
%enough money to pay and what money they need to use.
%Usage: [updated structure with info on what coins were used by who,
%what coins were used or a string saying they didn't have enough] =
%payMachine(original structure of wallet contents, name of person, cost
%of what they're buying)
lengths = length(os)
names = {os.name}
quarters = {os.quarters}
dimes = {os.dimes}
nickels = {os.nickels}
pennies = {os.pennies}
step = 0
starter = {};
starter1 = {};
starter2 = {};
starter3 = {};
starter4 = {};
while step < lengths
a = names{1}
starter = horzcat(2,starter,{a})
names(1) = []
step = step + 1
end
lengths = length(quarters)
step = 0
while step < lengths
a = quarters{1}
starter1 = horzcat(2,starter1,{a})
quarters(1) = []
step = step + 1
end
lengths = length(dimes)
step = 0
while step < lengths
a = dimes{1}
starter2 = horzcat(2,starter2,{a})
dimes(1) = []
step = step + 1
end
lengths = length(nickels)
step = 0
while step < lengths
a = nickels{1}
starter3 = horzcat(2,starter3,{a})
nickels(1) = []
step = step + 1
end
lengths = length(pennies)
step = 0
while step < lengths
a = pennies{1}
starter4 = horzcat(2,starter4,{a})
pennies(1) = []
step = step + 1
end
starter(1:lengths) = [];
starter1(1:lengths) = [];
starter2(1:lengths) = [];
starter3(1:lengths) = [];
starter4(1:lengths) = [];
names = {os.name};
quarters = {os.quarters};
dimes = {os.dimes};
nickels = {os.nickels};
pennies = {os.pennies};
counter = 1;
question = strcmp(names{counter},findName)
while question == 0
question = strcmp(names{counter},findName)
counter = counter + 1
end
gold = counter - 1
total = [quarters{gold} dimes{gold} nickels{gold} pennies{gold}]
findCost = findCost.*100
quartersTotal = total(1) .* 25
if findCost > quartersTotal
dimesTotal = quartersTotal + (total(2).*10)
end
if findCost > dimesTotal
nickelsTotal = dimesTotal + (total(3) .* 5)
end
if findCost > nickelsTotal
penniesTotal = nickelsTotal + total(4)
end
if findCost > penniesTotal
payout = 'You don"t have enough money'
end
if dimesTotal > findCost
subtract = dimesTotal - findCost
dimesTotal = quartersTotal - subtract
end
if nickelsTotal > findCost
subtract = nickelsTotal - findCost
nickelsTotal = nickelsTotal - subtract
end
if penniesTotal > findCost
subtract = penniesTotal - findCost
penniesTotal = penniesTotal - subtract
end
newQuarters = quartersTotal./25
newDimes = (dimesTotal - quartersTotal)./10
newNickels = (nickelsTotal - dimesTotal)./5
newPennies = penniesTotal - nickelsTotal
if penniesTotal >= findCost
payout = struct('quarters', newQuarters, 'dimes', newDimes, 'nickels', newNickels, 'pennies', newPennies)
end
quarters{gold} = newQuarters
dimes{gold} = newDimes
nickels{gold} = newNickels
pennies{gold} = newPennies
updatedStruct = struct('name',names, 'quarters', quarters, 'dimes', dimes, 'nickels', nickels, 'pennies', pennies)
payout = payout
end














