Skip to content

Instantly share code, notes, and snippets.

@Miha-Pleskovic
Created August 4, 2016 13:22
Show Gist options
  • Save Miha-Pleskovic/280e133f08031abedfb2bbb46c109303 to your computer and use it in GitHub Desktop.
Save Miha-Pleskovic/280e133f08031abedfb2bbb46c109303 to your computer and use it in GitHub Desktop.
Vehicle Manager Program
# -*- coding: utf-8 -*-
import fileinput
try:
vehicles_file = open("vehicles.txt", "r")
except:
vehicles_file = open("vehicles.txt", "w+")
vehicles_file.close()
print ""
print " VEHICLE MANAGER"
print "-----------------------------------------------"
print " manage your fleet the easy way"
print ""
# MAIN MENU
def main_menu():
print "1 -- NEW VEHICLE 2 -- VEHICLE ROSTER 3 -- EDIT VEHICLE INFO 4 -- EXIT PROGRAM"
print ">>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>"
choice = raw_input("").lower()
print ""
if choice == "1" or choice == "new vehicle":
new_vehicle()
elif choice == "2" or choice == "vehicle roster":
roster()
elif choice == "3" or choice == "edit vehicle info":
edit()
elif choice == "4" or choice == "exit program":
print "CLOSING PROGRAM ..."
quit()
else:
print "Command not recognised. Please try again."
main_menu()
# CLASS MODEL FOR STORING VEHICLE INFO
class Vehicle(object):
def __init__(self, brand, model, mileage, service_date):
self.brand = brand
self.model = model
self.mileage = mileage
self.service_date = service_date
# STORES A NEW VEHICLE IN FILE
def new_vehicle():
print "ADDING A NEW VEHICLE ..."
print "Enter a brand:"
brand = raw_input("").lower()
print "Enter a model:"
model = raw_input("").lower()
print "Enter a mileage (in kilometers):"
mileage = raw_input("")
print "Enter a service date (DD.MM.YYYY):"
service_date = raw_input("")
print ""
if mileage.isdigit() and service_date.isdigit() or service_date.replace(".", "").isdigit():
vehicles_file = open("vehicles.txt", "a")
vehicles_file.write(brand + " - " + model + " - " + mileage + " - " + service_date + "\n")
vehicles_file.close()
print "%s %s -- SAVED IN FILE" % (brand, model)
print ""
new_vehicle_again()
else:
print "ERROR! Discrepancy detected. Please re-enter data correctly."
new_vehicle()
# ASKS IF YOU WANT TO ADD ANOTHER NEW VEHICLE
def new_vehicle_again():
print "Would you like to add another vehicle? (YES / NO)"
answer = raw_input("").lower()
print ""
if answer == "yes":
new_vehicle()
elif answer == "no":
main_menu()
else:
print "Command not recognised. Please enter either YES or NO."
new_vehicle_again()
# DISPLAYS A LIST OF VEHICLES IN FILE
def roster():
vehicles_file = open("vehicles.txt", "r")
for item in vehicles_file:
brand, model, mileage, service_date = item.split(" - ")
print "VEHICLE: " + brand.capitalize() + " " + model.capitalize()
print "MILEAGE: " + mileage
print "SERVICE DATE: " + service_date
print ""
vehicles_file.close()
main_menu()
# CHANGES MILEAGE AND SERVICE DATE IN SPECIFIC VEHICLE
def edit():
vehicles = []
vehicles_file = open("vehicles.txt", "r")
for item in vehicles_file:
brand, model, mileage, service_date = item.split(" - ")
print brand.capitalize() + " " + model.capitalize() + " - " + mileage + " kilometers - " + service_date
new_vehicle = Vehicle(brand=brand, model=model, mileage=mileage, service_date=service_date)
vehicles.append(new_vehicle)
vehicles_file.close()
x_brand = raw_input("Enter a brand: ").lower()
x_model = raw_input("Enter a model: ").lower()
x_mileage = raw_input("Enter a CURRENT MILEAGE: ")
print ""
print "SEARCHING ..."
print ""
new_mileage = raw_input("Enter a NEW MILEAGE: ")
new_service_date = raw_input("Enter a NEW SERVICE DATE: ")
print ""
vehicles_file = open("vehicles.txt", "w+")
for vehicle in vehicles:
if x_brand == vehicle.brand and x_model == vehicle.model and x_mileage == vehicle.mileage:
vehicle.mileage = new_mileage
vehicle.service_date = new_service_date
vehicles_file.write(vehicle.brand + " - " + vehicle.model + " - " + vehicle.mileage + " - " + vehicle.service_date + "\n")
else:
vehicles_file.write(vehicle.brand + " - " + vehicle.model + " - " + vehicle.mileage + " - " + vehicle.service_date)
vehicles_file.close()
for line in fileinput.FileInput("vehicles.txt", inplace=0):
if line.rstrip():
print line
print "CHANGES SAVED"
edit_again()
# ASKS IF YOU WANT TO CHANGE SOMETHING ELSE
def edit_again():
print "Would you like to change mileage and service date again? (YES / NO)"
answer = raw_input("").lower()
print ""
if answer == "yes":
edit()
elif answer == "no":
main_menu()
else:
print "Command not recognised. Please enter either YES or NO."
edit_again()
main_menu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment