r/learncsharp • u/symplaren • May 28 '19
C# classes/constructors/interfaces
Hello, I have project to do on C# the project looks like 3 classes that need to inheritance each other also 1 interface and some other methods if someone think he can help me out i'll appreciate it.
===================================================================== so i had to make program for controlling parking lot.it should get,save and shows the information.
i will try to explain it as simple as i can. First i have to create 3 classes : Cars , LightTrucks , HeavyTrucks they both should contain: type , brand , model; in constructor
actually the input should be like this: parkinglot parkingname1 2 2 2 parkinglot parkingname2 2 1 1 Car Kia Seed Lighttruck Citroen Jumper Car VW Polo Lighttruck Ford Tranzit Heavytruck MAN TGM18 Car Audi А6 Heavytruck Volvo FM12 Print something1 End
and the Output should be: parked cars in parkinglot something1: Brand Kia, Model Seed Brand VW, Model Polo Brand Citroen, Model Jumper Brand Ford, Model Tranzit Brand MAN, Model TGM18 Brand Volvo, model FM12 Parkinglot something1 has the following locations: Car 2, busy 2 Lighttrucks 2, busy 2 Heavytrucks 2, busy 2 Parkinglot something2 has the following locations: Car 2, busy 1 Lighttruck 1, busy 1 Heavytruck 1, busy 1
///////////////////////////////////// so what should it do create parking lot with command parkinglot name 2 2 2 (first 2 = car ; second Lighttruck ; third Heavytruck)
after i create 1 or more parkinglots i should be able to enter cars,lighttrucks or heavytrucks.
like: car Audi A6 Lighttruck Citroen Jumper Heavytruck Volvo FM12
and then Enter "End" to stop the program or Enter Print parkinglot name like "Print something1" and the program should print "parked cars in parkinglot something1: Brand Audi, Model A6 ; Brand Citroen, Model Jumper ; HeavyTruck Brand Volvo, Model FM12 ; Parkinglot something1 has the following locations: cars 2 , busy 1 ; lighttrucks 2 , busy 1 ; heavytrucks 2 ,busy 1"
1
u/CrimsonWolfSage May 28 '19 edited May 28 '19
Slowly reading code between tasks. I'll update this with feedback as I go.
General Notes:
Naming Conventions & Meaningful Names
DRY: Don't Repeat Yourself ( If you typed it 2~3 times, probably a better way )
Structure:
-> Remove Unused Imports/Using Libraries
-> Layout Rabbit Hole: C Dev Guidelines
class Program
--> demo is unclear, input is clearer
Line 17: var input = Console.ReadLine()
--> Create Variables before the loop
Line 19: if( condition ) { break; }
--> Put condition in the loop While( condition )
Line 24: same as 17
--> Create Variables before the loop
Line 25-39: Parsing Block Don't Repeat Yourself
Line 27: ICar = new car("Type","Brand","Model");
--> Instantiate class Car, not interface ICar
--> The car class knows it is a car, not sure why you pass the type into it and save it.
Line 28: This line repeats for every type, it could be done once for every type.
interface ICar
3 shared properties to get allows every vehicle type to share this behavior, but that's not a good way to use it.
interfaces provide a "contract" that every object using the interface agrees on.
ICar shoud contain Behaviors every car, and truck will use. In this case, Park() and Exit() might be a common use case for every vehicle to agree on, as well as Location() or Spot() to list where it is located in your Parking Lot.
The common properties, or values is a good candidate for Inheritance, where every car, truck can be a Vehicle with certain base details and behaviors.
class Car : ICar
Line 75: public Cars(string type,string brand,string model)
--> The car class knows it's a car
--> Type isn't used anywhere
Line 90: return $"Марка {this.Brand}, Модел {this.Model}";
-->only uses Brand & Model, no reason for Type in class
Copy Pasta for all ICar vehicles...
--> Suggest Inheritance: Base Class for shared data and methods that rarely/never changes