create savePath if doesn't exist

This commit is contained in:
mollusk 2019-01-14 14:45:00 -07:00
parent 6833f23d5a
commit 2e6aaae280

View File

@ -5,61 +5,163 @@ module BillCalc
function splitCost(bill::Float64, peopleAmount::Int64)
total = bill / peopleAmount
return print("$(@sprintf("%.2f", total))\n")
return print('$', "$(@sprintf("%.2f", total))\n")
end
print("Enter Total Cost of Water Bill: ")
waterBill = chomp(readline())
# Make sure the user hasn't already created a save file
function checkSave(savePath)
waterBill = lstrip(waterBill, ['.', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'-','+', '-', '+', '{', '}', '[', ']', '|',
':', ';', '<', '>', ',', '?', '/', '!', '~', '`'])
if occursin(r"[0-9]","$waterBill") != true
println("Please enter a number.")
exit(1)
if isfile("$savePath")
println(savePath, " already exists")
print("a: Append, o: Overwrite: ")
changeFile = chomp(readline())
if changeFile == "o" || changeFile == "O"
rm("$savePath")
touch("$savePath")
return
elseif changeFile == "a" || changeFile == "A"
return
end
else
return
end
end
waterBill = tryparse(Float64, waterBill)
# Collect user information from billParse() and write to file
function writeReport(month, name, dueDate, amount, splitBy, splitAmount, savePath)
print("Enter Total Cost of Gas Bill: ")
gasBill = readline()
if occursin(r"[0-9]","$gasBill") != true
println("Please enter a number.")
exit(1)
if isfile(savePath) != true
touch(savePath)
end
if isfile(savePath) == true
file = open(savePath, "a")
write(file, "\nBill Name: ",name, "\n")
write(file, "Due: $dueDate", "\n")
write(file, "Total: ", amount, "\n")
write(file, "Split by: $splitBy\n")
write(file, "Each pays: ", "$splitAmount", "\n")
close(file)
else
println("Could not open $savePath")
exit(0)
end
end
gasBill = tryparse(Float64, gasBill)
#Get bill specific input from user
function billParse(month, savePath)
print("Enter Total Cost of Internet Bill: ")
internetBill = readline()
println("\n", "(enter q in any prompt to exit)")
print("\n", "Name of bill: ")
name = chomp(readline())
if name == "q"
exit(0)
end
print("Due Date: ")
dueDate = chomp(readline())
if dueDate == "q"
exit(0)
end
print("Amount of bill: ")
amount = chomp(readline())
if amount == "q"
exit(0)
end
if occursin(r"[0-9]","$amount") != true
println("Please enter a number.")
exit(1)
end
amount = tryparse(Float64, amount)
print("Split bill by how many people?: ")
splitBill = chomp(readline())
if splitBill == "q"
exit(0)
end
splitBill = tryparse(Int64, splitBill)
if occursin(r"[0-9]","$splitBill") != true
println("Please enter a number.")
return 1
end
if splitBill < 1
println("You can't divide by 0 bitch!")
return 1
end
println("\n", "Month: $month")
println("Bill: $name")
println("Due: $dueDate")
println("Amount: ", amount)
println("Split by: ", splitBill)
print("Is this information correct?[Y/n]: ")
confirm = chomp(readline())
if confirm == "y" || confirm == "Y"
splitAmount = amount / Float64(splitBill, RoundUp)
splitTrimmed = @sprintf("%.2f", splitAmount)
println("\n", "$name Bill")
println("------------------------------")
println("$name: ", '$', amount)
print("Between ", splitBill, " people: $splitAmount\n")
println("------------------------------", "\n")
writeReport("$month", "$name", "$dueDate", "$amount", "$splitBill", "$splitTrimmed", savePath)
else
return
end
if occursin(r"[0-9]","$internetBill") != true
println("Please enter a number.")
exit(1)
end
internetBill = tryparse(Float64, internetBill)
function main()
baseDir = homedir()
saveDir = baseDir
print("Enter Total Cost of Electric Bill: ")
electricBill = readline()
if occursin(r"[0-9]","$electricBill") != true
println("Please enter a number.")
exit(1)
done = false
print("\n", "Current Month: ")
curMonth = chomp(readline())
saveFile = "$curMonth-bills.txt"
savePath = joinpath(saveDir, saveFile)
checkSave("$savePath")
while done == false
billParse(curMonth, savePath)
done == true
end
end
electricBill = tryparse(Float64, electricBill)
main()
println("-----------------------------------")
println("Cost Per Person")
println("-----------------------------------")
print("Water: ", '$'); splitCost(waterBill, householdSize)
print("Gas: ", '$'); splitCost(gasBill, householdSize)
print("Internet: ", '$'); splitCost(internetBill, householdSize)
print("Electric: ", '$'); splitCost(electricBill, householdSize)
end # module