create savePath if doesn't exist
This commit is contained in:
parent
6833f23d5a
commit
2e6aaae280
172
src/BillCalc.jl
172
src/BillCalc.jl
@ -5,61 +5,163 @@ module BillCalc
|
|||||||
|
|
||||||
function splitCost(bill::Float64, peopleAmount::Int64)
|
function splitCost(bill::Float64, peopleAmount::Int64)
|
||||||
total = bill / peopleAmount
|
total = bill / peopleAmount
|
||||||
return print("$(@sprintf("%.2f", total))\n")
|
return print('$', "$(@sprintf("%.2f", total))\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
print("Enter Total Cost of Water Bill: ")
|
# Make sure the user hasn't already created a save file
|
||||||
waterBill = chomp(readline())
|
function checkSave(savePath)
|
||||||
|
|
||||||
waterBill = lstrip(waterBill, ['.', '@', '#', '$', '%', '^', '&', '*', '(', ')',
|
|
||||||
'-','+', '-', '+', '{', '}', '[', ']', '|',
|
|
||||||
':', ';', '<', '>', ',', '?', '/', '!', '~', '`'])
|
|
||||||
|
|
||||||
if occursin(r"[0-9]","$waterBill") != true
|
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
|
||||||
|
|
||||||
|
# Collect user information from billParse() and write to file
|
||||||
|
function writeReport(month, name, dueDate, amount, splitBy, splitAmount, savePath)
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
#Get bill specific input from user
|
||||||
|
function billParse(month, savePath)
|
||||||
|
|
||||||
|
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.")
|
println("Please enter a number.")
|
||||||
exit(1)
|
exit(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
waterBill = tryparse(Float64, waterBill)
|
|
||||||
|
|
||||||
print("Enter Total Cost of Gas Bill: ")
|
amount = tryparse(Float64, amount)
|
||||||
gasBill = readline()
|
|
||||||
|
|
||||||
if occursin(r"[0-9]","$gasBill") != true
|
print("Split bill by how many people?: ")
|
||||||
println("Please enter a number.")
|
splitBill = chomp(readline())
|
||||||
exit(1)
|
|
||||||
|
if splitBill == "q"
|
||||||
|
exit(0)
|
||||||
end
|
end
|
||||||
|
|
||||||
gasBill = tryparse(Float64, gasBill)
|
splitBill = tryparse(Int64, splitBill)
|
||||||
|
|
||||||
print("Enter Total Cost of Internet Bill: ")
|
if occursin(r"[0-9]","$splitBill") != true
|
||||||
internetBill = readline()
|
|
||||||
|
|
||||||
if occursin(r"[0-9]","$internetBill") != true
|
|
||||||
println("Please enter a number.")
|
println("Please enter a number.")
|
||||||
exit(1)
|
return 1
|
||||||
end
|
end
|
||||||
|
|
||||||
internetBill = tryparse(Float64, internetBill)
|
if splitBill < 1
|
||||||
|
println("You can't divide by 0 bitch!")
|
||||||
print("Enter Total Cost of Electric Bill: ")
|
return 1
|
||||||
electricBill = readline()
|
|
||||||
|
|
||||||
if occursin(r"[0-9]","$electricBill") != true
|
|
||||||
println("Please enter a number.")
|
|
||||||
exit(1)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
electricBill = tryparse(Float64, electricBill)
|
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
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
function main()
|
||||||
|
baseDir = homedir()
|
||||||
|
saveDir = baseDir
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
end # module
|
||||||
|
Loading…
x
Reference in New Issue
Block a user