-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvehicle_management_system.py
228 lines (192 loc) · 8.28 KB
/
vehicle_management_system.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import tkinter as tk
from tkinter import messagebox
import json
import os
# JSON File for Data Storage
DATA_FILE = "vehicles.json"
def initialize_json():
if not os.path.exists(DATA_FILE):
with open(DATA_FILE, 'w') as f:
json.dump([], f)
def load_vehicles():
with open(DATA_FILE, 'r') as f:
return json.load(f)
def save_vehicles(vehicles):
with open(DATA_FILE, 'w') as f:
json.dump(vehicles, f, indent=4)
# Vehicle Class (Base Class)
class Vehicle:
def __init__(self, vehicle_id, brand, model, year):
self.vehicle_id = vehicle_id
self.brand = brand
self.model = model
self.year = year
def display_info(self):
return f"ID: {self.vehicle_id}, Brand: {self.brand}, Model: {self.model}, Year: {self.year}"
# Car Class (Derived Class)
class Car(Vehicle):
def __init__(self, vehicle_id, brand, model, year, num_doors):
super().__init__(vehicle_id, brand, model, year)
self.num_doors = num_doors
def display_info(self):
return super().display_info() + f", Doors: {self.num_doors}"
# Truck Class (Derived Class)
class Truck(Vehicle):
def __init__(self, vehicle_id, brand, model, year, payload_capacity):
super().__init__(vehicle_id, brand, model, year)
self.payload_capacity = payload_capacity
def display_info(self):
return super().display_info() + f", Payload Capacity: {self.payload_capacity} tons"
# Bike Class (Derived Class)
class Bike(Vehicle):
def __init__(self, vehicle_id, brand, model, year, engine_capacity):
super().__init__(vehicle_id, brand, model, year)
self.engine_capacity = engine_capacity
def display_info(self):
return super().display_info() + f", Engine Capacity: {self.engine_capacity} cc"
# Vehicle Management System Class
class VehicleManagementSystem:
def add_vehicle(self, vehicle):
vehicles = load_vehicles()
if isinstance(vehicle, Car):
vehicle_type = "Car"
additional_info = vehicle.num_doors
elif isinstance(vehicle, Truck):
vehicle_type = "Truck"
additional_info = vehicle.payload_capacity
elif isinstance(vehicle, Bike):
vehicle_type = "Bike"
additional_info = vehicle.engine_capacity
else:
raise ValueError("Unknown vehicle type")
vehicles.append({
"vehicle_id": vehicle.vehicle_id,
"brand": vehicle.brand,
"model": vehicle.model,
"year": vehicle.year,
"type": vehicle_type,
"additional_info": additional_info
})
save_vehicles(vehicles)
def remove_vehicle(self, vehicle_id):
vehicles = load_vehicles()
updated_vehicles = [v for v in vehicles if v["vehicle_id"] != vehicle_id]
if len(updated_vehicles) == len(vehicles):
return False
save_vehicles(updated_vehicles)
return True
def get_all_vehicles(self):
vehicles = load_vehicles()
result = []
for v in vehicles:
if v["type"] == "Car":
result.append(Car(v["vehicle_id"], v["brand"], v["model"], v["year"], int(v["additional_info"])))
elif v["type"] == "Truck":
result.append(Truck(v["vehicle_id"], v["brand"], v["model"], v["year"], float(v["additional_info"])))
elif v["type"] == "Bike":
result.append(Bike(v["vehicle_id"], v["brand"], v["model"], v["year"], int(v["additional_info"])))
return result
# GUI Class
class VehicleManagementApp:
def __init__(self, root):
self.vms = VehicleManagementSystem()
self.root = root
self.root.title("Vehicle Management System")
# Labels and Entry fields
tk.Label(root, text="Vehicle ID:").grid(row=0, column=0)
self.entry_id = tk.Entry(root)
self.entry_id.grid(row=0, column=1)
tk.Label(root, text="Brand:").grid(row=1, column=0)
self.entry_brand = tk.Entry(root)
self.entry_brand.grid(row=1, column=1)
tk.Label(root, text="Model:").grid(row=2, column=0)
self.entry_model = tk.Entry(root)
self.entry_model.grid(row=2, column=1)
tk.Label(root, text="Year:").grid(row=3, column=0)
self.entry_year = tk.Entry(root)
self.entry_year.grid(row=3, column=1)
tk.Label(root, text="Type (Car/Truck/Bike):").grid(row=4, column=0)
self.entry_type = tk.Entry(root)
self.entry_type.grid(row=4, column=1)
tk.Label(root, text="Additional Info (Doors/Payload/Engine Capacity):").grid(row=5, column=0)
self.entry_info = tk.Entry(root)
self.entry_info.grid(row=5, column=1)
# Buttons
tk.Button(root, text="Add Vehicle", command=self.add_vehicle).grid(row=6, column=0)
tk.Button(root, text="Remove Vehicle", command=self.remove_vehicle).grid(row=6, column=1)
tk.Button(root, text="Show All Vehicles", command=self.show_all_vehicles).grid(row=7, column=0, columnspan=2)
self.text_area = tk.Text(root, height=10, width=50)
self.text_area.grid(row=8, column=0, columnspan=2)
def add_vehicle(self):
vehicle_id = self.entry_id.get()
brand = self.entry_brand.get()
model = self.entry_model.get()
year = self.entry_year.get()
vehicle_type = self.entry_type.get().lower()
additional_info = self.entry_info.get()
if not (vehicle_id and brand and model and year and vehicle_type and additional_info):
messagebox.showerror("Error", "All fields must be filled out")
return
try:
year = int(year)
except ValueError:
messagebox.showerror("Error", "Year must be a number")
return
if vehicle_type == "car":
try:
num_doors = int(additional_info)
vehicle = Car(vehicle_id, brand, model, year, num_doors)
except ValueError:
messagebox.showerror("Error", "Number of doors must be a number")
return
elif vehicle_type == "truck":
try:
payload_capacity = float(additional_info)
vehicle = Truck(vehicle_id, brand, model, year, payload_capacity)
except ValueError:
messagebox.showerror("Error", "Payload capacity must be a number")
return
elif vehicle_type == "bike":
try:
engine_capacity = int(additional_info)
vehicle = Bike(vehicle_id, brand, model, year, engine_capacity)
except ValueError:
messagebox.showerror("Error", "Engine capacity must be a number")
return
else:
messagebox.showerror("Error", "Invalid vehicle type. Use 'Car', 'Truck', or 'Bike'")
return
self.vms.add_vehicle(vehicle)
messagebox.showinfo("Success", "Vehicle added successfully")
self.clear_fields()
def remove_vehicle(self):
vehicle_id = self.entry_id.get()
if not vehicle_id:
messagebox.showerror("Error", "Vehicle ID is required")
return
if self.vms.remove_vehicle(vehicle_id):
messagebox.showinfo("Success", "Vehicle removed successfully")
else:
messagebox.showerror("Error", "Vehicle ID not found")
self.clear_fields()
def show_all_vehicles(self):
vehicles = self.vms.get_all_vehicles()
self.text_area.delete(1.0, tk.END)
if vehicles:
for vehicle in vehicles:
self.text_area.insert(tk.END, vehicle.display_info() + "\n")
else:
self.text_area.insert(tk.END, "No vehicles available")
def clear_fields(self):
self.entry_id.delete(0, tk.END)
self.entry_brand.delete(0, tk.END)
self.entry_model.delete(0, tk.END)
self.entry_year.delete(0, tk.END)
self.entry_type.delete(0, tk.END)
self.entry_info.delete(0, tk.END)
# Main Program
if __name__ == "__main__":
initialize_json()
root = tk.Tk()
app = VehicleManagementApp(root)
root.mainloop()