Skip to content

Commit e14081f

Browse files
committed
Merge pull request #38 from noglows/jln/master
Jln/master
2 parents 3142513 + c70daaa commit e14081f

File tree

5 files changed

+196
-14
lines changed

5 files changed

+196
-14
lines changed

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ Create an `Account` class which should have the following functionality:
3535
- Add an `owner` property to each Account to track information about who owns the account.
3636
- The `Account` can be created with an `owner`, OR you can create a method that will add the `owner` after the `Account` has already been created.
3737

38-
<!--
38+
3939
## Wave 2
4040
### CSV Files!
41+
- Update the `Account` class to be able to handle all of these fields from the CSV file used as input.
42+
- For example, manually choose the data from the first line of the CSV file and ensure you can create a new instance of your Account using that data
4143
- Add the following **class** methods to your existing `Account` class
4244
- `self.all` - returns a collection of `Account` instances, representing all of the Accounts described in the CSV. See below for the CSV file specifications
4345
- `self.find(id)` - returns an instance of `Account` where the value of the id field in the CSV matches the passed parameter
44-
- Update the `Account` class to be able to handle all of these fields from the CSV file used as input.
46+
4547

4648
#### CSV Data File
4749
Bank::Account
@@ -57,6 +59,21 @@ Create an `Account` class which should have the following functionality:
5759
- `self.all` - returns a collection of `Owner` instances, representing all of the Owners described in the CSV. See below for the CSV file specifications
5860
- `self.find(id)` - returns an instance of `Owner` where the value of the id field in the CSV matches the passed parameter
5961

62+
Bank::Owner
63+
The data, in order in the CSV, consists of:
64+
**ID** - (Fixnum) a unique identifier for that Owner
65+
**Last Name** - (String) the owner's last name
66+
**First Name** - (String) the owner's first name
67+
**Street Addess** - (String) the owner's street address
68+
**City** - (String) the owner's city
69+
**State** - (String) the owner's state
70+
71+
To create the relationship between the accounts and the owners use the `account_owners` CSV file.
72+
The data for this file, in order in the CSV, consists of:
73+
**Account ID** - (Fixnum) a unique identifier corresponding to an account
74+
**Owner ID** - (Fixnum) a unique identifier corresponding to an owner
75+
76+
<!--
6077
## Wave 3
6178
Create a `SavingsAccount` class which should inherit behavior from the `Account` class. It should include updated logic with the following functionality:
6279
- An updated `initialize` method:

bank.rb

+141-12
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,195 @@
1+
require 'csv'
2+
require 'pry'
3+
14
module Bank
25
class Account
36

4-
attr_reader :balance, :owner
7+
attr_reader :balance, :id
8+
attr_accessor :owner
59

6-
def initialize(id, initial_balance)
7-
#@account_owner = owner.first_name
8-
@id = id
9-
@balance = initial_balance
10-
@owner = nil
10+
def initialize(id, initial_balance, open_date)
11+
@id = id.to_i
12+
@balance = initial_balance.to_i/100.0
13+
@open_date = DateTime.strptime(open_date, "%Y-%m-%d %H:%M:%S %z")
14+
#@owner = nil
1115

1216
# Raises an argument error if the initial balance is less than 0
13-
if initial_balance < 0
17+
if initial_balance.to_i < 0
1418
raise ArgumentError, "The balance cannot be less than 0."
1519
end
1620
end
1721

22+
# Creates accounts from the accounts.csv file
23+
def self.all
24+
@accounts = []
25+
accounts_csv = CSV.read("support/accounts.csv")
26+
accounts_csv.each do |id, balance, date|
27+
id = Bank::Account.new(id,balance,date)
28+
@accounts.push(id)
29+
end
30+
#puts @accounts
31+
return @accounts
32+
end
33+
34+
# Finds the account with the ID that matches the passed parameter and returns the instance
35+
def self.find(id_search)
36+
found = @accounts.find do |account|
37+
account.id == id_search
38+
end
39+
return found
40+
end
41+
42+
# Links owners with accounts
43+
def self.link_owner
44+
self.all
45+
Bank::Owner.all
46+
accounts_with_owners = []
47+
account_owners_csv = CSV.read("support/account_owners.csv")
48+
account_owners_csv.each do |row|
49+
account = self.find(row[0].to_i)
50+
owner_account = Bank::Owner.find(row[1].to_i)
51+
account.owner = owner_account
52+
accounts_with_owners.push(account)
53+
end
54+
return accounts_with_owners
55+
end
56+
57+
# ----------------------------------------- #
58+
# Work below is extra #
59+
60+
# Displays (with formatting) the account details for all the accounts in accounts.csv
61+
# (Made this on accident and didn't want to let it go to waste!)
62+
def self.all_print_nice
63+
if @accounts == nil
64+
puts "There are no accounts."
65+
else
66+
@accounts.each do |account|
67+
puts account
68+
account.current_balance
69+
end
70+
end
71+
return
72+
end
73+
74+
# Finds an account by id passed in as a parameter and displays the account information nicely formatted
75+
# (Made this on accident and didn't want to let it go to waste!)
76+
def self.find_and_display(id_search)
77+
found = @accounts.find do |account|
78+
account.id == id_search
79+
end
80+
return found.current_balance
81+
end
82+
# end of extra work #
83+
# ----------------------------------------#
84+
85+
# Method for withdrawing from account
1886
def withdraw(amount_to_withdraw)
87+
# Checks that the user is not withdrawing more than what is available in the account
1988
if (@balance - amount_to_withdraw)< 0
2089
puts "The requested withdrawal is more than the available funds."
2190
puts "You only have $#{@balance} available for withdrawal."
2291
return @balance
2392
else
93+
# makes the withdrawal and displays info to the user
2494
@balance -= amount_to_withdraw
2595
puts "You have withdrawn $#{amount_to_withdraw}."
2696
puts "Your current balance is $#{@balance}"
2797
return @balance
2898
end
2999
end
30100

101+
# Method for depositing into account
31102
def deposit(amount_to_deposit)
103+
# Makes the deposit and displays info to the user
32104
@balance += amount_to_deposit
33105
puts "You have deposited $#{amount_to_deposit}."
34106
puts "Your current balance is $#{@balance}."
35107
return @balance
36108
end
37109

110+
# Displays current balance in the account
38111
def current_balance
39112
puts "The account with ID #{@id} currently has a balance of $#{@balance}."
113+
puts "This account was set up on #{@open_date}"
40114
end
41115

116+
42117
end
43118

44119
class Owner
45-
attr_reader :first_name, :last_name, :street, :city, :state, :zip_code
120+
attr_reader :first_name, :last_name, :street, :city, :state, :id
46121

47122
def initialize(owner_hash)
123+
@id = owner_hash[:id]
48124
@first_name = owner_hash[:first_name]
49125
@last_name = owner_hash[:last_name]
50-
@street = owner_hash[:street]
126+
@street_address = owner_hash[:street_address]
51127
@city = owner_hash[:city]
52128
@state = owner_hash[:state]
53-
@zip_code = owner_hash[:zip_code]
54129
end
55130

131+
# Method to display details of an owner instance
56132
def print_owner_details
57133
puts "The owner of this account is #{@first_name} #{@last_name}."
58-
puts "Street: #{@street}"
134+
puts "Street: #{@street_address}"
59135
puts "City: #{@city}"
60136
puts "State: #{@state}"
61-
puts "Zip: #{@zip_code}"
62137
end
63138

139+
# Creates accounts from the accounts.csv file
140+
def self.all
141+
owner_hash = Hash.new
142+
@owners = []
143+
owners_csv = CSV.read("support/owners.csv")
144+
owners_csv.each do |id, last_name, first_name, street_address, city, state|
145+
owner_hash[:id] = id.to_i
146+
owner_hash[:last_name] = last_name
147+
owner_hash[:first_name] = first_name
148+
owner_hash[:street_address] = street_address
149+
owner_hash[:city] = city
150+
owner_hash[:state] = state
151+
id = Bank::Owner.new(owner_hash)
152+
@owners.push(id)
153+
end
154+
puts @owners
155+
return @owners
156+
end
157+
158+
# Finds the owner with the ID that matches the passed parameter and returns the instance
159+
def self.find(id_search)
160+
found = @owners.find do |owner|
161+
owner.id == id_search
162+
end
163+
return found
164+
end
165+
166+
167+
# ----------------------------------------- #
168+
# Work below is extra #
169+
170+
# Displays (with formatting) the owner details for all the accounts in owners.csv
171+
def self.all_print_nice
172+
if @owners == nil
173+
puts "There are no accounts."
174+
else
175+
@owners.each do |owner|
176+
owner.print_owner_details
177+
end
178+
end
179+
return
180+
end
181+
182+
# Finds an owner by id passed in as a parameter and displays the owner information nicely formatted
183+
def self.find_and_display(id_search)
184+
found = @owners.find do |owner|
185+
owner.id == id_search
186+
end
187+
return found.print_owner_details
188+
end
189+
190+
# end of extra work #
191+
# ----------------------------------------#
192+
64193
end
65194

66195
end

support/account_owners.csv

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1212,25
2+
1213,24
3+
1214,19
4+
1215,14
5+
1216,18
6+
1217,15
7+
15151,17
8+
15152,16
9+
15153,21
10+
15154,20
11+
15155,22
12+
15156,23

support/accounts.csv

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1212,1235667,1999-03-27 11:30:09 -0800
2+
1213,66367,2010-12-21 12:21:12 -0800
3+
1214,9876890,2007-09-22 11:53:00 -0800
4+
1215,919191,2011-10-31 13:55:55 -0800
5+
1216,100022,2000-07-07 15:07:55 -0800
6+
1217,12323,2003-11-07 11:34:56 -0800
7+
15151,9844567,1993-01-17 13:30:56 -0800
8+
15152,34343434343,1999-02-12 14:03:00 -0800
9+
15153,2134,2013-11-07 09:04:56 -0800
10+
15154,43567,1996-04-17 08:44:56 -0800
11+
15155,999999,1990-06-10 13:13:13 -0800
12+
15156,4356772,1994-11-17 14:04:56 -0800

support/owners.csv

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
14,Morales,Wanda,9003 Gerald Hill,Honolulu,Hawaii
2+
15,Foster,Shirley,79734 Namekagon Court,Tampa,Florida
3+
16,Taylor,James,9 Portage Court,Winston Salem,North Carolina
4+
17,Ross,Marilyn,0 Delaware Circle,Seattle,Washington
5+
18,Gonzalez,Laura,310 Hauk Street,Springfield,Illinois
6+
19,Cooper,Ruby,99 American Road,Atlanta,Georgia
7+
20,Knight,Helen,3373 American Point,Charlotte,North Carolina
8+
21,Bell,Jessica,06 Kenwood Hill,Lansing,Michigan
9+
22,Sanders,Annie,8113 Sutherland Center,Everett,Washington
10+
23,Berry,Shirley,7 Kings Pass,Cleveland,Ohio
11+
24,King,Kevin,3499 Judy Center,Santa Monica,California
12+
25,Clark,Kathleen,72984 Chive Hill,New York City,New York

0 commit comments

Comments
 (0)