Example #1
0
        static void PrintTrain(Locomotive train)
        {
            Write("\n");
            RailCar railCar = train.getFirstRailCar();

            Write("L");
            while (railCar != null)
            {
                Write($"-{railCar.toString()}");
                railCar = railCar.getNext();
            }
        }
Example #2
0
 public RailCar(string name)
 {
     if (name.Length < 2)
     {
         throw (new MyError("The name of a Rail Car has to be at least 2 characters long."));
     }
     else
     {
         this.name = name;
         this.next = null;
     }
 }
Example #3
0
        static Locomotive RemoveUnprofitable(Locomotive L)
        {
            RailCar A = L.getFirstRailCar();
            RailCar B = A.getNext();

            if (B == null)
            {
                if (!A.isProfitable())
                {
                    L.setFirstRailCar(null);
                }
            }

            else
            {
                while (A != null && B != null)
                {
                    if (!A.isProfitable())
                    {
                        L.setFirstRailCar(B);
                        A = B;
                        B = B.getNext();
                    }
                    else if (!B.isProfitable())
                    {
                        B = B.getNext();
                        A.setNext(B);
                    }
                    else
                    {
                        A = B;
                        B = B.getNext();
                    }
                }
                if (A != null)
                {
                    if (!A.isProfitable())
                    {
                        L.setFirstRailCar(null);
                    }
                }
            }

            return(L);
        }
Example #4
0
        static Locomotive ReverseOrder(Locomotive L)
        {
            Locomotive reverse = new Locomotive();

            reverse.setFirstRailCar(L.getFirstRailCar().clone());
            RailCar first = reverse.getFirstRailCar();

            first.setNext(null);

            RailCar car    = L.getFirstRailCar();
            RailCar newCar = L.getFirstRailCar();

            while (car.getNext() != null)
            {
                newCar = car.getNext().clone();
                newCar.setNext(first);
                reverse.setFirstRailCar(newCar);
                first = reverse.getFirstRailCar();

                car = car.getNext();
            }

            return(reverse);
        }
Example #5
0
        public void attachRailCar(RailCar newCar)
        {
            if (first == null)
            {
                first = newCar;
            }
            else
            {
                RailCar append = first;
                bool    insert = false;

                if (string.Compare(newCar.toString(), first.toString()) == -1)
                {
                    newCar.setNext(first);
                    first = newCar;
                }
                else
                {
                    while (append != null && !insert)
                    {
                        if (append.getNext() == null)
                        {
                            insert = true;
                            append.setNext(newCar);
                        }
                        else if (string.Compare(newCar.toString(), append.getNext().toString()) == -1)
                        {
                            insert = true;
                            newCar.setNext(append.getNext());
                            append.setNext(newCar);
                        }
                        append = append.getNext();
                    }
                }
            }
        }
Example #6
0
 public void setNext(RailCar nextCar)
 {
     this.next = nextCar;
 }
Example #7
0
 public void setFirstRailCar(RailCar R) => first = R;