Skip to content

Instantly share code, notes, and snippets.

@mhmadip
Created May 12, 2025 17:38
Show Gist options
  • Save mhmadip/7afd99c7cf09c5a0fb6808971353c350 to your computer and use it in GitHub Desktop.
Save mhmadip/7afd99c7cf09c5a0fb6808971353c350 to your computer and use it in GitHub Desktop.
OOP project sample 2024-25
import 'dart:io';
// Base Vehicle class (demonstrates abstraction and inheritance)
abstract class Vehicle {
// Properties (demonstrates encapsulation with private fields)
String _make;
String _model;
int _year;
// Constructor
Vehicle(this._make, this._model, this._year);
// Getters
String get make => _make;
String get model => _model;
int get year => _year;
// Abstract method (for polymorphism)
String getVehicleType();
// Common method
String getDetails() {
return '$_year $_make $_model (${getVehicleType()})';
}
}
// Car class extending Vehicle (demonstrates inheritance)
class Car extends Vehicle {
int _doors;
Car(String make, String model, int year, this._doors)
: super(make, model, year);
@override
String getVehicleType() {
return 'Car';
}
@override
String getDetails() {
return '${super.getDetails()}, $_doors doors';
}
}
// Motorcycle class extending Vehicle (demonstrates polymorphism)
class Motorcycle extends Vehicle {
Motorcycle(String make, String model, int year)
: super(make, model, year);
@override
String getVehicleType() {
return 'Motorcycle';
}
}
// Main program
void main() {
// Create a list of vehicles (demonstrates polymorphism)
List<Vehicle> vehicles = [
Car("Toyota", "Corolla", 2020, 4),
Car("Honda", "Civic", 2019, 2),
Motorcycle("Yamaha", "R1", 2021)
];
bool running = true;
while (running) {
printMenu();
print('Enter your choice: ');
String? choice = stdin.readLineSync();
switch(choice) {
case '1':
addVehicle(vehicles);
break;
case '2':
removeVehicle(vehicles);
break;
case '3':
searchVehicle(vehicles);
break;
case '4':
showAllVehicles(vehicles);
break;
case 'q':
running = false;
print('Goodbye!');
break;
default:
print('Invalid choice. Please try again.');
}
}
}
// Menu display function
void printMenu() {
print('\n===== Vehicle Management System =====');
print('1. Add a vehicle');
print('2. Remove a vehicle');
print('3. Search for a vehicle');
print('4. Show all vehicles');
print('q. Quit');
print('====================================');
}
// Add vehicle function
void addVehicle(List<Vehicle> vehicles) {
print('\nAdd a new vehicle:');
print('What type of vehicle? (1 for Car, 2 for Motorcycle): ');
String? typeChoice = stdin.readLineSync();
print('Enter make: ');
String? make = stdin.readLineSync();
print('Enter model: ');
String? model = stdin.readLineSync();
print('Enter year: ');
String? yearStr = stdin.readLineSync();
// Basic validation
if (make == null || model == null || yearStr == null) {
print('Invalid input. Please try again.');
return;
}
int? year;
try {
year = int.parse(yearStr);
} catch (e) {
print('Invalid year. Please enter a number.');
return;
}
if (typeChoice == '1') {
print('Enter number of doors: ');
String? doorsStr = stdin.readLineSync();
int? doors;
try {
doors = int.parse(doorsStr ?? '0');
} catch (e) {
print('Invalid number of doors. Using 4 as default.');
doors = 4;
}
vehicles.add(Car(make, model, year, doors));
print('Car added successfully!');
}
else if (typeChoice == '2') {
vehicles.add(Motorcycle(make, model, year));
print('Motorcycle added successfully!');
}
else {
print('Invalid vehicle type.');
}
}
// Remove vehicle function
void removeVehicle(List<Vehicle> vehicles) {
if (vehicles.isEmpty) {
print('No vehicles to remove.');
return;
}
print('\nRemove a vehicle:');
showAllVehicles(vehicles);
print('Enter the number of the vehicle to remove: ');
String? indexStr = stdin.readLineSync();
try {
int index = int.parse(indexStr ?? '0') - 1;
if (index >= 0 && index < vehicles.length) {
Vehicle removed = vehicles.removeAt(index);
print('Removed: ${removed.getDetails()}');
} else {
print('Invalid vehicle number.');
}
} catch (e) {
print('Please enter a valid number.');
}
}
// Search vehicle function
void searchVehicle(List<Vehicle> vehicles) {
print('\nSearch for a vehicle:');
print('Enter make to search for: ');
String? searchMake = stdin.readLineSync();
if (searchMake == null || searchMake.isEmpty) {
print('Invalid search term.');
return;
}
bool found = false;
for (var vehicle in vehicles) {
if (vehicle.make.toLowerCase() == searchMake.toLowerCase()) {
print('Found: ${vehicle.getDetails()}');
found = true;
}
}
if (!found) {
print('No vehicles found with make: $searchMake');
}
}
// Show all vehicles function
void showAllVehicles(List<Vehicle> vehicles) {
if (vehicles.isEmpty) {
print('No vehicles to display.');
return;
}
print('\nAll Vehicles:');
for (int i = 0; i < vehicles.length; i++) {
print('${i+1}. ${vehicles[i].getDetails()}');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment