-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcar.c
83 lines (69 loc) · 2.1 KB
/
car.c
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
#include <stdio.h>
#include "functions.h"
// Define the Car structure
struct Car cars[10] = {
{1, "Toyota Corrolla", 2024, 1},
{2, "Honda City", 2021, 1},
{3, "Kiya Sportage", 2022, 1},
{4, "Suzuki Alto", 2023, 1},
{5, "Boss Mehran", 2006, 1}};
int carCount = 5;
void viewAllCars() {
printf("\n--- All Cars ---\n");
if (carCount == 0) {
printf("No cars available in the system.\n");
return;
}
for (int i = 0; i < carCount; i++) {
printf("Car ID: %d, Model: %s, Year: %d, Available: %s\n",
cars[i].carID, cars[i].model, cars[i].year,
cars[i].available ? "Yes" : "No");
}
}
void addNewCar() {
if (carCount < 10) {
struct Car newCar;
newCar.carID = carCount + 1; // Assign the next car ID
newCar.available = 1; // New car is available by default
printf("Enter Car Model: ");
scanf("%s", newCar.model);
printf("Enter Car Year: ");
scanf("%d", &newCar.year);
// Add the new car to the array
cars[carCount] = newCar;
carCount++; // Increment carCount AFTER adding the car
printf("New car '%s' added successfully!\n", newCar.model);
} else {
printf("Maximum limit of 10 cars reached. Can't add more cars.\n");
}
}
void rentCar()
{
int carID;
printf("Enter the Car ID you want to rent: ");
scanf("%d", &carID);
if (carID >= 1 && carID <= 5 && cars[carID - 1].available)
{
cars[carID - 1].available = 0;
printf("You have successfully rented '%s'.\n", cars[carID - 1].model);
}
else
{
printf("Sorry, the car is either not available or the ID is invalid.\n");
}
}
void returnCar()
{
int carID;
printf("Enter the Car ID you want to return: ");
scanf("%d", &carID);
if (carID >= 1 && carID <= 5 && !cars[carID - 1].available)
{
cars[carID - 1].available = 1;
printf("You have successfully returned '%s'.\n", cars[carID - 1].model);
}
else
{
printf("Sorry, either the car ID is invalid or the car was not rented.\n");
}
}