Reputation: 25
I'm a complete rookie with programming (especially C#) and here is my problem. I need to create a class with variables that can be accessed/referenced/used across in different classes and I have tried I think everything (Interfaces, Abstract classes, lists) and still can't make it to work.
Here is the last code snippet with the latest idea how it might look like:
abstract class FuelsList
{
string sFuelName;
float fFuelPrice;
void LPG()
{
sFuelName = "LPG";
fFuelPrice = 0.65f;
}
void Diesel()
{
sFuelName = "Diesel";
fFuelPrice = 0.95f;
}
void Petrol()
{
sFuelName = "Petrol";
fFuelPrice = 1.15f;
}
}
Then using lists like these I would like to reference it in other classes (if I remember well) like this:
Console.WriteLine("This pump dispenses {0} and the price is {1} per litre.",
FuelsList.Petrol.sFuelName, FuelsList.Petrol.sFuelPrice);
Not sure if I'm trying to reinvent a wheel or do like 600% of work when there it's much easier. The problem is that I need lots of classes, interfaces, inheritance and private fields accessible via accessors as it is polymorphism and fields accessing exercise. Also, I will need to create something similar, but with CarTypes that will have stuff like FuelUsed, TankCapacity etc., but once I get to understand the above example it will be easy-peasy.
I tried looking around the other discussions and questions, and also other sites, but couldn't really find any solution to this. Thank you all in advance.
Upvotes: 2
Views: 246
Reputation: 5539
This is one way I would envision it:
Since LPG, Petrol, and Diesel are having the same attributes but nothing default, I would define a basic rule (Interface) for them to follow. If I had some default behavior for each of them, I would have used abstract class.
public interface IFuel
{
string Name { get; }
float Price { get; }
}
Implement the rule for the family
public class LPG : IFuel
{
public string Name => "LPG";
public float Price => 0.65f;
}
public class Diesel : IFuel
{
public string Name => "Diesel";
public float Price => 0.95f;
}
public class Petrol : IFuel
{
public string Name => "Petrol";
public float Price => 1.15f;
}
Define the consumer. Here I have the option to inject the dependent object in an easier way using the Interface. The consumer object deals with how to consume the fuel object that it is dependent on
public class FuelConsumer
{
private readonly IFuel fuel;
public FuelConsumer(IFuel fuelData)
{
fuel = fuelData;
}
public string GetFuelDetails()
{
return $"This pump dispenses {fuel.Name} and the price is {fuel.Price} per litre.";
}
}
Usage: I could pass any type of fuel to the consumer and we have made each object responsible for only taking care of it's job, no tight coupling, more readable and maintainable.
Next checkpoint would be whether my design is really SOLID
FuelConsumer consumer = null;
IFuel fuel = null;
// Let's use LPG
fuel = new LPG();
consumer = new FuelConsumer(fuel);
Console.WriteLine(consumer.GetFuelDetails());
// Let's use diesel
fuel = new Diesel();
consumer = new FuelConsumer(fuel);
Console.WriteLine(consumer.GetFuelDetails());
// Let's use petrol
fuel = new Petrol();
consumer = new FuelConsumer(fuel);
Console.WriteLine(consumer.GetFuelDetails());
Upvotes: 2
Reputation: 1472
Please don't just copy/paste, you won't learn anything from that. Understand this code and, like you said, the rest of the exercise will be easy peazy.
public class Fuel
{
private string sFuelname = default(string);
private float Price = default(float);
public string FuelName
{
get { return sFuelName; } set { sFuelName = value; }
}
public float FuelPrice
{
get { return fFuelPrice; } set { fFuelPrice= value; }
}
public static class FuelsList
{
public Fuel LPG
{
get
{
var temp = new Fuel
{
FuelName = "LPG",
FuelPrice = 0.65f
}
return temp;
}
}
public Fuel Diesel
{
get
{
var temp = new Fuel
{
FuelName = "Diesel",
FuelPrice = 0.95f
}
return temp;
}
}
public Fuel Petrol
{
get
{
var temp = new Fuel
{
FuelName = "Petrol",
FuelPrice = 1.15f
}
return temp;
}
}
}
Usage:
Console.WriteLine("This pump dispenses {0} and the price is {1} per litre.", FuelsList.Petrol.FuelName, FuelsList.Petrol.FuelPrice);
Upvotes: 0
Reputation: 35290
This might be what you're looking for. Here's the base class Fuel
:
public abstract class Fuel
{
public string Name { get; private set; }
public decimal Price { get; private set; }
public override string ToString()
{
return string.Format(@"Name: {0} Price: {1}", Name, Price.ToString("C"));
}
protected Fuel(string name, decimal price)
{
this.Name = name;
this.Price = price;
}
}
And each fuel type is a class that derives from it:
public class Petrol : Fuel
{
public Petrol(decimal price) : base("Petrol", price) { }
}
public class Diesel : Fuel
{
public Diesel(decimal price) : base("Diesel", price) { }
}
public class Lpg : Fuel
{
public Lpg(decimal price) : base("LPG", price) { }
}
Then you can use them for example like this:
var petrol = new Petrol(1.15M);
var diesel = new Diesel(0.95M);
var lpg = new Lpg(0.65M);
System.Diagnostics.Debug.Print(@"My fuels: " +
Environment.NewLine + string.Join(
Environment.NewLine, new[] { petrol.ToString(), diesel.ToString(), lpg.ToString() }));
Upvotes: 0
Reputation: 25370
create two classes - one to store the Fuel
object, and your object that holds the different types. Then, just use properties instead of methods (which is what you were going for, you had the syntax wrong).
public static class FuelList
{
static FuelList()
{
LPG = new Fuel{ Name = "LPG", Price = .65F };
//...
}
public static Fuel LPG {get;}
public static Fuel Diesel {get;}
public static Fuel Petrol {get;}
}
public sealed class Fuel
{
public string Name {get;set;}
public float Price {get;set;}
}
then you can use it like you intended:
Console.WriteLine("This pump dispenses {0} and the price is {1} per litre.",
FuelsList.Petrol.Name, FuelsList.Petrol.Price);
Upvotes: 0