Military Elite - 20/100 и RunTime Error
Здравейте колеги,
моля за помощ за следната задача, тъй като вече втори ден се чудя какво не е наред в кода ми. Прегледах поне 4,5 решения на колеги, но така и не успявам да си открия проблема в моето решение. Въпреки, че ми излизат коректни аутпути с дадените инпути, Judge е на друго мнение.
https://github.com/elipopovadev/CSharp-OOP/tree/main/InterfacesAndAbstraction-Exercise/MilitaryElite
https://judge.softuni.bg/Contests/Compete/Index/1502#7
Условие:
Create the following class hierarchy:
- - general class for Soldiers, holding id, first name and last name.
- - lowest base Soldier type, holding the salary(decimal).
- - holds a set of Privates under his command.
- - general class for all specialised Soldiers - holds the corps of the Soldier. The corps can only be one of the following: Airforces or Marines.
- - holds a set of Repairs. A Repair holds a part name and hours worked(int).
- - holds a set of Missions. A mission holds code name and a state (inProgress or Finished). A Mission can be finished through the method CompleteMission().
- - holds the code number of the Spy (int).
- - lowest base Soldier type, holding the salary(decimal).
Extract interfaces for each class. (e.g. ISoldier, IPrivate, ILieutenantGeneral, etc.) The interfaces should hold their public properties and methods (e.g. ISoldier should hold id, first name and last name). Each class should implement its respective interface. Validate the input where necessary (corps, mission state) - input should match exactly one of the required values, otherwise it should be treated as invalid. In case of invalid corps the entire line should be skipped, in case of an invalid mission state only the mission should be skipped.
You will receive from the console an unknown amount of lines containing information about soldiers until the command "End" is received. The information will be in one of the following formats:
- : "Private <id> <firstName> <lastName> <salary>"
- : "LieutenantGeneral <id> <firstName> <lastName> <salary> <private1Id> <private2Id> … <privateNId>" where privateXId will always be an Id of a Private already received through the input.
- : "Engineer <id> <firstName> <lastName> <salary> <corps> <repair1Part> <repair1Hours> … <repairNPart> <repairNHours>" where repairXPart is the name of a repaired part and repairXHours the hours it took to repair it (the two parameters will always come paired).
- : "Commando <id> <firstName> <lastName> <salary> <corps> <mission1CodeName> <mission1state> … <missionNCodeName> <missionNstate>" a missions code name, description and state will always come together.
- : "Spy <id> <firstName> <lastName> <codeNumber>"
Define proper constructors. Avoid code duplication through abstraction. Override ToString() in all classes to print detailed information about the object.
Name: <firstName> <lastName> Id: <id> Salary: <salary>
Name: <firstName> <lastName> Id: <id>
Code Number: <codeNumber>
Name: <firstName> <lastName> Id: <id> Salary: <salary>
Privates:
<private1 ToString()>
<private2 ToString()>
…
<privateN ToString()>
Name: <firstName> <lastName> Id: <id> Salary: <salary>
Corps: <corps>
Repairs:
<repair1 ToString()>
<repair2 ToString()>
…
<repairN ToString()>
Name: <firstName> <lastName> Id: <id> Salary: <salary>
Corps: <corps>
Missions:
<mission1 ToString()>
<mission2 ToString()>
…
<missionN ToString()>
Part Name: <partName> Hours Worked: <hoursWorked>
Code Name: <codeName> State: <state>
NOTE: Salary should be printed rounded to two decimal places after the separator.
Input |
Private 1 Pesho Peshev 22,22 Private 222 Toncho Tonchev 80,08 LieutenantGeneral 3 Joro Jorev 100 222 1 End |
Output |
Name: Pesho Peshev Id: 1 Salary: 22.22 Name: Stamat Stamov Id: 13 Salary: 13.10 Corps: Airforces Missions: Name: Toncho Tonchev Id: 222 Salary: 80.08 Name: Joro Jorev Id: 3 Salary: 100.00 Privates: Name: Toncho Tonchev Id: 222 Salary: 80.08 |
Input:
Engineer 7 Pencho Penchev 12,23 Marines Boat 2 Crane 17
Commando 19 Penka Ivanova 150,15 Airforces HairyFoot finished Freedom inProgress
End
Output:
Name: Pencho Penchev Id: 7 Salary: 12.23
Corps: Marines
Repairs:
Part Name: Boat Hours Worked: 2
Part Name: Crane Hours Worked: 17
Name: Penka Ivanova Id: 19 Salary: 150.15
Corps: Airforces
Missions:
Code Name: Freedom State: inProgress
Hi Axiomatik, thank you so much again. :)
I changed everything in my code like you said:
1) edited try catch;
2) added multiply inheritance in every my interface (my inheritance in every my class is like yours);
3) edited my class Mission and added interface IMission;
In the end I added blank space in my ToString() when I print listPrivates and listRepairs, because I missed them.
And the result is also 20/100 with three correct tests, one incorrect and three RunTime Errors. I'm very disparate.
Maybe I missed something important or might have been something else.
RunTime errors are most likely triggered from your StartUp class (ie Engineer has its for-loop run until inputArray.Length - 1 instead of inputArray.Length), unless some errors in your class builds. You can compare with different StartUp classes from other solutions, to try to fix some errors but also do watch how the SoftUni instructors solve this exercise and then come back to your solution and eliminate all of the problems.
StartUp:
Repair-class (variant 1):
Repair class (variant 2):
@ Axiomatik ,
I got 100/100 after three days working and nearly thirty attemps - https://github.com/elipopovadev/CSharp-OOP/tree/main/InterfacesAndAbstraction-Exercise/MilitaryElite. You really help me to understand how important is this exercise and to found out my mistakes in my last solution. This exercise is closest to the real web project, isn't it?! Can you tell me more exercises like this one, because I'm not very confident to work with Engine, Writer and Reader?
I made some folders:
- Core with Engine and IEngine;
- Enums with enumeration for State and Corps;
- Exceptions with three exceptions - "InvalidCorpsException", "InvalidMissionCompletion Exception", "InvalidStateException";
- Interfaces;
- IO with ConsoleReader, ConsoleWriter, IReader and IWriter;
- Models;
And StartUp:
using MilitaryElite.Core;
using MilitaryElite.IO;
using MilitaryElite.IO.Contracts;
namespace MilitaryElite
{
public class StartUp
{
static void Main(string[] args)
{
IReader reader = new ConsoleReader();
IWriter writer = new ConsoleWriter();
IEngine engine = new Engine(reader, writer);
engine.Run();
}
}
}
Best Regards!
Unfortunately, MilitaryElite is far away from a real web-project and more of a testing ground for class-inheritance and working with abstraction and dependency injection. For now, the best thing to do is to work with the older exams (16 August 2020 is one of the more complex ones and good for training) and the additional workshops included in the C# OOP course (IWriter and IReader are also going to be used and explained in the following exercises, so their usage will become easy after some practise).
Best, ;-)