C
C#•7mo ago
RebornJcush

Why is my list output not using the method to check true/false

public class InterfacePA
{
public static void Main(string[] args)
{
Console.WriteLine("\nName, Week 2 Interface PA\n");


//create instances of car and cargotruck
Car car1 = new Car("Ford", "Festiva", true, "Miami, FL");
Car car2 = new Car("Honda", "Accord", false, "Atlanta, GA");
CargoTruck ct1 = new CargoTruck("Volvo", "VNL 860", false, "150 cubic ft", "Richmond, VA");
CargoTruck ct2 = new CargoTruck("Ford", "Transit", true, "123 cubic ft", "Virginia Beach, VA");

//create list and add instances for list print
List<IVehicle> vehicles = new List<IVehicle> { car1, car2, ct1, ct2 };
Console.WriteLine("Vehicles printed from List");
foreach (IVehicle vehicle in vehicles)
{
PrintVehicle(vehicle, true);
Console.WriteLine();
}


Console.WriteLine("Vehicles printed directly");
PrintVehicle(car1, true);
Console.WriteLine();
PrintVehicle(car2, false);
Console.WriteLine();
PrintVehicle(ct1, false);
Console.WriteLine();
PrintVehicle(ct2, true);
Console.WriteLine();
}
private static void PrintVehicle(IVehicle vehicle, bool running)
{
Console.Write(vehicle);
vehicle.Drive(running, vehicle.GetDestination());
}
}
public class InterfacePA
{
public static void Main(string[] args)
{
Console.WriteLine("\nName, Week 2 Interface PA\n");


//create instances of car and cargotruck
Car car1 = new Car("Ford", "Festiva", true, "Miami, FL");
Car car2 = new Car("Honda", "Accord", false, "Atlanta, GA");
CargoTruck ct1 = new CargoTruck("Volvo", "VNL 860", false, "150 cubic ft", "Richmond, VA");
CargoTruck ct2 = new CargoTruck("Ford", "Transit", true, "123 cubic ft", "Virginia Beach, VA");

//create list and add instances for list print
List<IVehicle> vehicles = new List<IVehicle> { car1, car2, ct1, ct2 };
Console.WriteLine("Vehicles printed from List");
foreach (IVehicle vehicle in vehicles)
{
PrintVehicle(vehicle, true);
Console.WriteLine();
}


Console.WriteLine("Vehicles printed directly");
PrintVehicle(car1, true);
Console.WriteLine();
PrintVehicle(car2, false);
Console.WriteLine();
PrintVehicle(ct1, false);
Console.WriteLine();
PrintVehicle(ct2, true);
Console.WriteLine();
}
private static void PrintVehicle(IVehicle vehicle, bool running)
{
Console.Write(vehicle);
vehicle.Drive(running, vehicle.GetDestination());
}
}
my output is printing like this: Justyn Cushing, Week 2 Interface PA Vehicles printed from List Make: Ford Model: Festiva The car is running and heading to Miami, FL Make: Honda Model: Accord The car is running and heading to Atlanta, GA Make: Volvo Model: VNL 860 The truck is running and hauling 150 cubic ft of cargo to Richmond, VA Make: Ford Model: Transit The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA Vehicles printed directly Make: Ford Model: Festiva The car is running and heading to Miami, FL Make: Honda Model: Accord The car is not running and heading to Atlanta, GA Make: Volvo Model: VNL 860 The truck is not running and hauling 150 cubic ft of cargo to Richmond, VA Make: Ford Model: Transit The truck is running and hauling 123 cubic ft of cargo to Virginia Beach, VA The directly output is correct however, the list will not put anything other than all true or all false. I can upload other classes if needed.
25 Replies
RebornJcush
RebornJcush•7mo ago
my bool for true / false has a check to change it to is or is not so that it doesnt say true running or false running but that method only seems to be working in the direct
mg
mg•7mo ago
I don't see any if statements in what you sent Is there more? Oh wait I think I see the issue
RebornJcush
RebornJcush•7mo ago
trying to add the other classes
mg
mg•7mo ago
Presumably Vehicle.Drive() takes in a boolean representing whether or not it's running and the destination right
RebornJcush
RebornJcush•7mo ago
yes its
public void Drive(bool running, string destination)
{
Console.WriteLine("The car" + (running ? " is" : " is not") + " running and heading to " + destination);
}
public void Drive(bool running, string destination)
{
Console.WriteLine("The car" + (running ? " is" : " is not") + " running and heading to " + destination);
}
mg
mg•7mo ago
Well, the reason it's behaving that way in your foreach loop is that you're passing true every time If the boolean that represents whether or not the vehicle is running comes from the vehicle class itself, it shouldn't be a parameter to the Drive() method
RebornJcush
RebornJcush•7mo ago
so i tried not having true but then it tells me im missing a paramtere for that bool and fails to run.. i added true after trying chat gpt
mg
mg•7mo ago
Could you post the entire classes for car, truck, and IVehicle $paste
MODiX
MODiX•7mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
RebornJcush
RebornJcush•7mo ago
yeah one sec
mg
mg•7mo ago
Don't try chatgpt If googling doesn't get you the answer just come here and ask actual people
RebornJcush
RebornJcush•7mo ago
BlazeBin - sfrjmejrqped
A tool for sharing your source code with the world!
RebornJcush
RebornJcush•7mo ago
yeah chat gpt had me redo the whole thing because it royally messed it up
mg
mg•7mo ago
yeah it'll do that to you Ok there are a lot of things to talk about here but I'll start with what's most relevant to your issue Like I mentioned, because Car and CargoTruck both have fields m_running, that shouldn't be a parameter to Drive() In the Drive() method, you can write Console.WriteLine("The car" + (m_running ? ... Similarly for destination It's what's referred to as encapsulation. The Car class contains everything about the car, so those things shouldn't be passed in via parameters, they should be supplied by the class's members Onto things that don't directly have anything to do with your issue, but are important if you're gonna write good C# code; instead of having fields m_make and methods GetMake() etc. you should have properties https://learn.microsoft.com/en-us/dotnet/csharp/properties
RebornJcush
RebornJcush•7mo ago
so it should just be public void Drive(string destination)?
mg
mg•7mo ago
What you're doing is something you'd see in C++, which doesn't have properties It shouldn't have any parameters
RebornJcush
RebornJcush•7mo ago
yeah i know its sloppy and can be simplified but this is how the teacher wants it done lol
mg
mg•7mo ago
Both running and destination come from the class, not from outside I was worried this was gonna be the answer In that case keep it that way to appease your teacher but know that it is totally wrong to do things that way in C# lol
RebornJcush
RebornJcush•7mo ago
yeah i seem to hear that alot.. the joys of school.. so when i remove the parameters my writeline for running? and destination error because they dont exist in the context
mg
mg•7mo ago
Right, you need to replace them with m_running and m_destination, respectively Those two things being members of the class are how you represent the concept that the car knows whether its running and where it's going; those two things don't need to be supplied to it from the outside
RebornJcush
RebornJcush•7mo ago
oh wow it really was such a simple fix thank you.. idk why our guided assignment has us do it the way i had it and it works then when i reference that for this one it doesnt work that way
mg
mg•7mo ago
This does not strike me as a high quality class But yeah, in C# the whole m_variable, GetVariable(), SetVariable() trio can be replaced by simply Variable { get; set; } Fields do have their place and are prefixed with a single _. They're used when the variable doesn't need to be exposed, so tl;dr when it's private
RebornJcush
RebornJcush•7mo ago
none of them are.. be happy you havent seen some of my other classes .. and yeah get set is a lot simpler
mg
mg•7mo ago
Oh trust me I've been through my own CS hell > puts an HTTP request on the screen "so this is like, pseudocode"
RebornJcush
RebornJcush•7mo ago
lets put it this way.. i graduate in less than a year and i have no idea what im doing other than basic loops and making tiny programs that will never be used in the workforce 😄 thanks again! Ive been stuck on that for like 3 hours lol....