From 2e6aaae2801f49e86f8b177542c739588565c229 Mon Sep 17 00:00:00 2001 From: mollusk Date: Mon, 14 Jan 2019 14:45:00 -0700 Subject: [PATCH] create savePath if doesn't exist --- src/BillCalc.jl | 174 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 138 insertions(+), 36 deletions(-) diff --git a/src/BillCalc.jl b/src/BillCalc.jl index 434a3eb..29e4fa4 100644 --- a/src/BillCalc.jl +++ b/src/BillCalc.jl @@ -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 isfile("$savePath") + println(savePath, " already exists") + print("a: Append, o: Overwrite: ") + changeFile = chomp(readline()) - if occursin(r"[0-9]","$waterBill") != true - println("Please enter a number.") - exit(1) + 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 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 - if occursin(r"[0-9]","$gasBill") != true - println("Please enter a number.") - exit(1) 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() + done = false - if occursin(r"[0-9]","$electricBill") != true - println("Please enter a number.") - exit(1) + 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