diff --git a/bankclasses.rb b/bankclasses.rb new file mode 100644 index 0000000..87a38fc --- /dev/null +++ b/bankclasses.rb @@ -0,0 +1,103 @@ +class Bank + def initialize(bank_name) + # Expects a string + @bank_name = bank_name + @bank_database = Hash.new + end + + def set_bank_customer_funds(bank_customer, funds) + # Expects a BankCustomer object, and an integer or float + @bank_database[bank_customer] = funds + end + + def get_bank_customer_funds(bank_customer) + # Expects a BankCustomer object + @bank_database[bank_customer] + end + + def withdraw(customer, amount) + # Expects a BankCustomer object, and an integer or float + + return false if amount > @bank_database[customer.customer_identity] + + @bank_database[customer.customer_identity] = @bank_database[customer.customer_identity] - amount + true + end + +end + +class CreditCard + def initialize(customer_identity) + # Expects a BankCustomer object + @card_owner = customer_identity + end + + def get_card_owner + @card_owner + end + +end + +class BankCustomer + def initialize(customer_identity, initial_funds, bank_name) + # Expects a string, integer or float, and a Bank object + @customer_identity = customer_identity + @bank_name = bank_name + register_new_customer(initial_funds) + end + + def customer_identity + @customer_identity + end + + def bank_name + @bank_name + end + + def card + @card + end + + def register_new_customer(funds) + bank_name.set_bank_customer_funds(@customer_identity, funds) + end + + def register_new_credit_card + @card = CreditCard.new(self) + end + + def deposit(amount) + bank_name.set_bank_customer_funds(@customer_identity, get_bank_customer_funds(@customer_identity) + amount) + puts "You deposited $#{amount} into your account, you now have $#{bank_customer[@customer_identity]}" + end +end + +class Store + @products = { 'Cheesecake' => 12, 'Hair Dryer' => 52, 'Purple Object' => 600, 'Mask' => 25, 'Horn' => 125 } + + def self.display_products + @products.each do | k, v | + puts "#{k}: $#{v}" + end + nil + end + + def self.purchase_item(credit_card, item) + # Expects a CreditCard object, and a valid item from the hash keys of @products + if @products[item].nil? + puts "No such item." + return nil + end + + if credit_card.get_card_owner.bank_name.withdraw(credit_card.get_card_owner, @products[item]) + puts "Transaction successful, have fun with your new #{item}!" + else + puts "Card denied!" + end + end +end + +# Tests + +bank = Bank.new('bank') +customer = BankCustomer.new('customer', 5000, bank) diff --git a/http_requests.txt b/http_requests.txt index 1764a22..92c78d5 100644 --- a/http_requests.txt +++ b/http_requests.txt @@ -21,12 +21,14 @@ answer the following... 1. GET /movies Fill this in with what the page will look like in the browser: - +Title: A New Hope, Year: 1977 +Title: The Empire Strikes Back, Year: 1980 +Title: Return of the Jedi, Year: 1983 2. GET /movies/1 Fill this in with what the page will look like in the browser: - +Title: A New Hope, Year: 1977 3. POST /movies with params {:movie => {:title => "The Phantom Menace", :year => 1998}} @@ -35,13 +37,13 @@ Fill in what the database should look like after a post with parameters ================================================ | id | Title | Year | ================================================ -| | | | +| 1 | A New Hope | 1977 | ------------------------------------------------ -| | | | +| 2 | The Empire Strikes Back | 1980 | ------------------------------------------------ -| | | | +| 3 | Return of the Jedi | 1983 | ------------------------------------------------ -| | | | +| 4 | The Phantom Menace | 1998 | ------------------------------------------------ 4. PUT /movies/4 {:movie => {:title => "The Phantom Menace", :year => 1999}} @@ -50,18 +52,21 @@ Fill in what the database should look like after the PUT with the parameters ================================================ | id | Title | Year | ================================================ -| | | | +| 1 | A New Hope | 1977 | ------------------------------------------------ -| | | | +| 2 | The Empire Strikes Back | 1980 | ------------------------------------------------ -| | | | +| 3 | Return of the Jedi | 1983 | ------------------------------------------------ -| | | | +| 4 | The Phantom Menace | 1999 | ------------------------------------------------ 5. GET /movies Fill in what the page should look like in the browser - +Title: A New Hope, Year: 1977 +Title: The Empire Strikes Back, Year: 1980 +Title: Return of the Jedi, Year: 1983 +Title: The Phantom Menace, Year: 1999 6. DELETE /movies/4 Fill in what the database should look like after the above request @@ -69,9 +74,9 @@ Fill in what the database should look like after the above request ================================================ | id | Title | Year | ================================================ -| | | | +| 1 | A New Hope | 1977 | ------------------------------------------------ -| | | | +| 2 | The Empire Strikes Back | 1980 | ------------------------------------------------ -| | | | +| 3 | Return of the Jedi | 1983 | ------------------------------------------------ diff --git a/interview_responses.txt b/interview_responses.txt index 3eecdbd..33562b4 100644 --- a/interview_responses.txt +++ b/interview_responses.txt @@ -1,9 +1,67 @@ You can write your responses here! -1. +1. +If you can only pull fruit out randomly, then it is the number of buckets * the number of times you iterate over them until one of the buckets produces a different fruit. Then you would have found the bucket with two types of fruit, and aleady know the contents of the other two. If you can select to attempt to take out a certain type of fruit, for example, I want to go to bucket A and try to pull out an apple, and either fail or succeed, then you can determine the contents of the buckets in 5 draws ( or less, depending on luck ). Check the three buckets for apples, when you fail to find an apple in one bucket, search the two other buckets for oranges. The bucket that has an orange in it is the apple + orange bucket, the first bucket is the orange bucket, and the remaining bucket is the apple bucket. -2. +2. +reversing_string = ('a'..'z').to_a +reversed_string = '' +reversing_string.each do | k | + reversed_string = k + reversed_string +end -3. +3. + +With Recursion: + +summing_array = [1, 2, [3, 4, [5]], [6, 7], 8, 9, [10]] + +$passed_sum = 0 +def recursive_sum_funct(sum_array) + for element in sum_array do + if element.kind_of?(Array) + recursive_sum_funct(element) + else + $passed_sum += element + end + end +end + +puts recursive_sum_funct(summing_array) +puts $passed_sum + +Question here -> With the puts statement for the function, I get back each number seperately that I wish to add. Ideally, instead of using a global variable, I would like to use the numbers that each recursive function is returning, and just add those. Is there a way to add the results outside of the function? I.E., if I wanted to say, do +"final_sum = recursive_sum_funct(summing_array)" and somehow have that add together all the recursive results. Is that possible? Or is it just the exact same as using a global variable since what I'm trying to add is each recursive function return, which is the number anyways? + +Or, more simply with helper methods: +summing_array = [1, 2, [3, 4, [5]], [6, 7], 8, 9, [10]] +summing_array.flatten.inject{|sum, x| sum + x} + +Without Recursion: + +sum = 0 +summable_array = [1, 2, [3, 4, [5]], [6, 7], 8, 9, [10]] + +formatted_array = summable_array.join(" ").split(" ").each do | element | + sum += element.to_i +end + +puts sum + + +4. + +Just going to construct this as arrays, considering that you don't yet know what's in the jars, do this. Take one pill from each jar, to put on the scale, and record the order. Treating each jar as an array with each pill being it's own array containing an integer representing the weight. I.E. +mystery_jar4 = [[10], [10], [10], etc...] + +scale = 0 +scale += mystery_jar1.pop, mystery_jar2.pop, mystery_jar3.pop, mystery_jar4.pop, etc... + +Take the measurement + +scale.index(9) + +The index shows which jar the contaminated pill came from. + +Outside of a programming construct, not sure how the scale measurement relationship makes sense. Is it only fair to measure the sum of any collection of materials on the scale? -4. \ No newline at end of file