-
Notifications
You must be signed in to change notification settings - Fork 0
/
budgetCalculator.r
127 lines (112 loc) · 4.31 KB
/
budgetCalculator.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
library(R6)
#Eventually load environment variables into calculator to avoid repetion.
#Load CSV, excel, or ods file for
# AND/OR put data in sqlite file for later access.
source("taxCalculator.r")
BudgetCalculator <- R6Class("BudgetCalculator",
inherit = CalculateTaxes,
public = list(
setHouseholdCount = function(householdNumber = NULL) {
if (is.null(householdNumber)) {
print("How many people in the household pay bills?")
householdNumber <- as.numeric(readline())
while (householdNumber < 1) {
print("Please enter a household number greater than zero.")
householdNumber <- as.numeric(readline())
}
}
private$householdCount <- householdNumber
},
setNames = function(names = NULL) {
if (is.null(names)) {
iterator <- private$householdCount
while (iterator != 0) {
iterator <- iterator - 1
print("Enter each household member's names one by one")
name <- readline()
while (!nzchar(name)) {
print("The name you entered was blank, please try again")
name <- readline()
}
private$peopleInfo[[name]] <- name
}
} else {
for (name in names) {
private$peopleInfo[[name]] <- name
}
}
},
setIncome = function(incomeInfo = NULL) {
if (is.null(incomeInfo)) {
for (name in private$peopleInfo) {
print(paste("How much does", name, "make per hour?"))
hourlyWage = as.numeric(readline())
print(paste("How many hours did", name, "work per day?"))
hoursWorked = as.numeric(readline())
print(paste("How many days did", name, "work?"))
daysWorked <- as.numeric(readline())
calculatedIncome <- hourlyWage * hoursWorked * daysWorked
private$peopleInfo[[name]] <- list("weeklyIncome" = calculatedIncome, "annualIncome" = calculatedIncome * 52)
}
} else {
for (name in names(incomeInfo)) {
private$peopleInfo[[name]] <- list("weeklyIncome" = incomeInfo[[name]], "annualIncome" = calculatedIncome * 52)
}
}
},
#Currently only based on california.
calculateTaxes = function() {
for (name in names(private$peopleInfo)) {
annualIncome <- private$peopleInfo[[name]][["annualIncome"]]
super$setAnnualIncome(annualIncome)
print(paste("What is", name, "'s filing status?"))
print("Single, Head of Household, or Married?")
filingStatus <- tolower(readline())
while (filingStatus != "single" && filingStatus != "head of household" && filingStatus != "hoh" && filingStatus != "married") {
print("Please enter an appropriate value")
}
if (filingStatus == "single") {
super$totalSingleCaliforniaFedTaxes()
} else if (filingStatus == "head of household" || filingStatus == "hoh") {
super$totalHeadOfHouseholdCaliforniaFedTaxes()
} else if (filingStatus == "married") {
print("Are you filing separately or together?")
choice <- tolower(readline())
}
print(paste("take home pay: ", super$getBiweeklyTakeHomePay()))
}
},
setBills = function() {
for (name in names(private$peopleInfo)) {
if (is.null(private$peopleInfo[[name]][["bills"]])) {
private$peopleInfo[[name]][["bills"]] <- list()
}
print(paste("How many bills does", name, "pay for?"))
billCount <- as.numeric(readline())
while (billCount > 0) {
print(paste("What is the name for this bill?"))
billName <- tolower(readline())
print(paste("What is the cost for this bill?"))
billCost <- as.numeric(readline())
private$peopleInfo[[name]][["bills"]][[billName]] <- billCost
billCount <- billCount - 1
}
# loop until billcount is zero
}
},
getPeopleInfo = function() {
print(private$peopleInfo)
}
),
private = list(
householdCount = NA,
peopleInfo = list()
)
)
calc <- BudgetCalculator$new()
calc$setHouseholdCount()
calc$setNames()
calc$setIncome()
calc$setBills()
calc$calculateTaxes()
calc$getPeopleInfo()