Challenge

Project 3

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

// Enumeration for car types

typedef enum { GAS, DIESEL, ELECTRIC } Type;

// Base Vehicle structure

typedef struct {

char make[50];

char model[50];

int year;

int odometer;

Type type;

} Vehicle;

// Function prototypes for base Vehicle

void getCarInformation(Vehicle *vehicle);

void checkTireRotation(Vehicle *vehicle);

Type getType(Vehicle *vehicle);

// GasCar structure

typedef struct {

Vehicle base;

} GasCar;

// DieselCar structure

typedef struct {

Vehicle base;

} DieselCar;

// ElectricCar structure

typedef struct {

Vehicle base;

} ElectricCar;

// Inventory structure

typedef struct {

Vehicle **vehicles;

int count;

} Inventory;

// Function prototypes for derived types

void checkOil(Vehicle *vehicle);

void checkChargerPower(ElectricCar *electricCar);

// Function prototypes for inventory management

void addVehicle(Inventory *inventory, Vehicle *vehicle);

void listVehicles(Inventory *inventory);

void updateVehicle(Inventory *inventory, int index, const char *make, const char *model, int year, int odometer);

void removeVehicle(Inventory *inventory, int index);

int main() {

Inventory inventory = {NULL, 0};

int choice;

do {

printf(" 0. Quit.\n");

printf(" 1. List inventory.\n");

printf(" 2. Add vehicle.\n");

printf(" 3. Update vehicle\n");

printf(" 4. Remove vehicle\n");

printf(" 5. Tire Rotation\n");

printf(" 6. Oil Change\n");

printf(" 7. Recharge\n");

printf("Enter choice: ");

scanf("%d", &choice);

switch (choice) {

case 1: {

listVehicles(&inventory);

break;

}

case 2: {

char make[50], model[50];

int year, odometer, type;

printf("Enter make: ");

scanf("%s", make);

printf("Enter model: ");

scanf("%s", model);

printf("Enter year: ");

scanf("%d", &year);

printf("Enter odometer: ");

scanf("%d", &odometer);

printf("Enter type (0 = GAS, 1 = DIESEL, 2 = ELECTRIC): ");

scanf("%d", &type);

Vehicle *vehicle = (Vehicle *)malloc(sizeof(Vehicle));

strcpy(vehicle->make, make);

strcpy(vehicle->model, model);

vehicle->year = year;

vehicle->odometer = odometer;

vehicle->type = type;

addVehicle(&inventory, vehicle);

break;

}

case 3: {

int index;

char make[50], model[50];

int year, odometer;

printf("Enter index of vehicle to update: ");

scanf("%d", &index);

printf("Enter new make: ");

scanf("%s", make);

printf("Enter new model: ");

scanf("%s", model);

printf("Enter new year: ");

scanf("%d", &year);

printf("Enter new odometer: ");

scanf("%d", &odometer);

updateVehicle(&inventory, index - 1, make, model, year, odometer);

break;

}

case 4: {

int index;

printf("Enter index of vehicle to remove: ");

scanf("%d", &index);

removeVehicle(&inventory, index - 1);

break;

}

case 5: {

int index;

printf("Enter index of vehicle to check tire rotation: ");

scanf("%d", &index);

if (index > 0 && index <= inventory.count) {

checkTireRotation(inventory.vehicles[index - 1]);

} else {

printf("Invalid index.\n");

}

break;

}

case 6: {

int index;

printf("Enter index of vehicle to check oil change: ");

scanf("%d", &index);

if (index > 0 && index <= inventory.count) {

Vehicle *vehicle = inventory.vehicles[index - 1];

if (vehicle->type == GAS || vehicle->type == DIESEL) {

checkOil(vehicle);

} else {

printf("This vehicle does not require an oil change.\n");

}

} else {

printf("Invalid index.\n");

}

break;

}

case 7: {

int index;

printf("Enter index of vehicle to check recharge: ");

scanf("%d", &index);

if (index > 0 && index <= inventory.count) {

Vehicle *vehicle = inventory.vehicles[index - 1];

if (vehicle->type == ELECTRIC) {

checkChargerPower((ElectricCar *)vehicle);

} else {

printf("This vehicle does not require a recharge.\n");

}

} else {

printf("Invalid index.\n");

}

break;

}

default:

if (choice != 0) {

printf("Invalid choice. Try again.\n");

}

break;

}

} while (choice != 0);

// Free allocated memory

for (int i = 0; i < inventory.count; ++i) {

free(inventory.vehicles[i]);

}

free(inventory.vehicles);

return 0;

}

// Function implementations

void getCarInformation(Vehicle *vehicle) {

printf("Make: %s\nModel: %s\nYear: %d\nOdometer: %d km\n",

vehicle->make, vehicle->model, vehicle->year, vehicle->odometer);

}

void checkTireRotation(Vehicle *vehicle) {

printf("Checking tire rotation...\n");

if (vehicle->odometer % 50000 == 0) {

printf("Tire rotation needed.\n");

} else {

printf("Tires are good.\n");

}

}

Type getType(Vehicle *vehicle) {

return vehicle->type;

}

void checkOil(Vehicle *vehicle) {

printf("Checking oil...\n");

if (vehicle->odometer % 10000 == 0) {

printf("Oil change needed.\n");

} else {

printf("Oil is good.\n");

}

}

void checkChargerPower(ElectricCar *electricCar) {

printf("Checking charger power...\n");

if (electricCar->base.odometer % 400 == 0) {

printf("Recharge needed.\n");

} else {

printf("Battery is good.\n");

}

}

void addVehicle(Inventory *inventory, Vehicle *vehicle) {

inventory->vehicles = realloc(inventory->vehicles, (inventory->count + 1) * sizeof(Vehicle *));

inventory->vehicles[inventory->count] = vehicle;

inventory->count++;

}

void listVehicles(Inventory *inventory) {

for (int i = 0; i < inventory->count; ++i) {

printf("%d. ", i + 1);

getCarInformation(inventory->vehicles[i]);

printf("\n");

}

}

void updateVehicle(Inventory *inventory, int index, const char *make, const char *model, int year, int odometer) {

if (index >= 0 && index < inventory->count) {

Vehicle *vehicle = inventory->vehicles[index];

strcpy(vehicle->make, make);

strcpy(vehicle->model, model);

vehicle->year = year;

vehicle->odometer = odometer;

} else {

printf("Invalid index.\n");

}

}

void removeVehicle(Inventory *inventory, int index) {

if (index >= 0 && index < inventory->count) {

free(inventory->vehicles[index]);

for (int i = index; i < inventory->count - 1; ++i) {

inventory->vehicles[i] = inventory->vehicles[i + 1];

}

inventory->count--;

inventory->vehicles = realloc(inventory->vehicles, inventory->count * sizeof(Vehicle *));

} else {

printf("Invalid index.\n");

}

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.