Beispiel #1
0
        protected override int GetHashCodeCore()
        {
            var a = TrainShift.GetHashCode();
            var b = Seat.GetHashCode();

            return(a ^ b);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            var stationCount = 4;
            var seatCount = 3;
            var trainShift = new TrainShift("0001", "Z19").Inatialize(stationCount, seatCount);

            var ticket = trainShift.BookTicket(new Segment(1, 2));
            PrintTicket(ticket);

            ticket = trainShift.BookTicket(new Segment(2, 3));
            PrintTicket(ticket);

            ticket = trainShift.BookTicket(new Segment(3, 4));
            PrintTicket(ticket);

            ticket = trainShift.BookTicket(new Segment(1, 4));
            PrintTicket(ticket);

            ticket = trainShift.BookTicket(new Segment(1, 4));
            PrintTicket(ticket);

            ticket = trainShift.BookTicket(new Segment(1, 4));
            PrintTicket(ticket);

            System.Console.ReadLine();
        }
Beispiel #3
0
 public (bool, string, TrainOrder) BookTrainTicket(TrainShift TrainShift, TrainStation StartTrainStation, TrainStation EndTrainStation, IList <SeatTypeConfig> SeatTypes, CustomerInfo CustomerInfo, UserContract UserContract)
 {
     return(BookTrainTicket(TrainShift, StartTrainStation, EndTrainStation, SeatTypes, CustomerInfo, new List <UserContract>()
     {
         UserContract
     }));
 }
        public FreezeSeatInfo(TrainShift TrainShift, DestinationSeat DestinationSeat) : base()
        {
            this.DestinationSeat = DestinationSeat;
            this.TrainShift      = TrainShift;

            TrainShift.FreezeSeatInfos.Add(this);
        }
Beispiel #5
0
 public ExtraSeatInfo(TrainShift TrainShift, Seat Seat)
 {
     this.Seat       = Seat;
     this.TrainShift = TrainShift;
     AddTime         = DateTimeOffset.Now;
     UpdateTime      = DateTimeOffset.Now;
     AddUserId       = UserHelper.User.Id;
     UpdateUserId    = UserHelper.User.Id;
 }
Beispiel #6
0
        public SaleSeatInfo(TrainShift TrainShift, DestinationSeat DestinationSeat)
        {
            this.DestinationSeat = DestinationSeat;
            this.TrainShift      = TrainShift;
            AddTime      = DateTimeOffset.Now;
            UpdateTime   = DateTimeOffset.Now;
            AddUserId    = UserHelper.User.Id;
            UpdateUserId = UserHelper.User.Id;

            TrainShift.SaleSeatInfos.Add(this);
        }
Beispiel #7
0
 private Seat isMatchSeat(TrainShift TrainShift, TrainStation StartTrainStation, TrainStation EndTrainStation, IList <SeatTypeConfig> SeatTypes)
 {
     foreach (var item in TrainShift.SaleSeatInfos.Select(o => o.DestinationSeat))
     {
         int count = item.TrainStationWays.Count(o => (StartTrainStation <o.EndTrainStation && EndTrainStation> o.EndTrainStation) ||
                                                 (EndTrainStation > o.StartTrainStation && EndTrainStation < o.EndTrainStation) ||
                                                 (StartTrainStation >= o.StartTrainStation && EndTrainStation <= o.EndTrainStation) ||
                                                 (StartTrainStation <= o.StartTrainStation && EndTrainStation >= o.EndTrainStation)
                                                 );
         if (count == 0 && SeatTypes.Contains(item.Seat.SeatType))
         {
             return(item.Seat);
         }
     }
     return(null);
 }
Beispiel #8
0
 public ExtraSeatInfo(TrainShift TrainShift, Seat Seat) : base()
 {
     this.Seat       = Seat;
     this.TrainShift = TrainShift;
 }
Beispiel #9
0
        public (bool, string, TrainOrder) BookTrainTicket(TrainShift TrainShift, TrainStation StartTrainStation, TrainStation EndTrainStation, IList <SeatTypeConfig> SeatTypes, CustomerInfo CustomerInfo, IList <UserContract> UserContracts)
        {
            if (StartTrainStation == null)
            {
                throw new ArgumentNullException("StartTrainStation");
            }
            if (!StartTrainStation.IsSale)
            {
                throw new ArgumentNullException($"{StartTrainStation} is Not Sale");
            }
            if (EndTrainStation == null)
            {
                throw new ArgumentNullException("EndTrainStation");
            }
            if (SeatTypes == null)
            {
                throw new ArgumentNullException("SeatTypes");
            }
            if (UserContracts == null || UserContracts.Count == 0)
            {
                throw new ArgumentNullException("UserContract");
            }
            if (UserContracts.Count(o => o.UserType != ContractUserType.Children) == 0)
            {
                throw new Exception("儿童不能单独买票");
            }
            if (StartTrainStation.Order > EndTrainStation.Order)
            {
                throw new ArgumentNullException("StartTrainStation same EndTrainStation Order");
            }
            if (StartTrainStation.Order > EndTrainStation.Order)
            {
                var temp = StartTrainStation;
                StartTrainStation = EndTrainStation;
                EndTrainStation   = temp;
            }

            var trainStationWay = new TrainStationWay(StartTrainStation, EndTrainStation);

            IList <Seat> seats = isMatchSeats(TrainShift, StartTrainStation, EndTrainStation, SeatTypes);

            //匹配优先在同一车厢
            if (seats == null || seats.Count == 0)
            {
                seats = TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType)).GroupBy(o => o.TrainCarriage).Where(o => o.Count() >= UserContracts.Count).FirstOrDefault().Take(UserContracts.Count).ToList();

                if (seats == null)
                {
                    seats = TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType)).Take(UserContracts.Count).ToList();
                    if (seats == null || seats.Count < UserContracts.Count)
                    {
                        throw new Exception("余票不足,建议单独购买");
                    }
                }
            }
            else if (seats.Count < UserContracts.Count)
            {
                foreach (var item in seats)
                {
                    seats = seats.Union(TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType) && o.TrainCarriage == item.TrainCarriage).Take(UserContracts.Count - seats.Count).ToList()).ToList();
                    if (seats.Count == UserContracts.Count)
                    {
                        break;
                    }
                }

                if (seats.Count < UserContracts.Count)
                {
                    seats = seats.Union(TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType)).Take(UserContracts.Count - seats.Count).ToList()).ToList();
                    if (seats.Count < UserContracts.Count)
                    {
                        throw new Exception("余票不足,建议单独购买");
                    }
                }
            }
            else
            {
                seats = seats.GroupBy(o => o.TrainCarriage).Where(o => o.Count() >= UserContracts.Count).FirstOrDefault().Take(UserContracts.Count).ToList();
                if (seats == null || seats.Count == 0)
                {
                    seats = seats.Take(UserContracts.Count).ToList();
                }
            }
            if (seats == null || UserContracts.Count > seats.Count)
            {
                throw new Exception("余票不足");
            }

            IList <TrainOrderItem> trainOrderItems = new List <TrainOrderItem>();

            for (int i = 0; i < seats.Count; i++)
            {
                var seat         = seats[i];
                var userContract = UserContracts[i];
                var b            = TrainShift.ExtraSeatInfos.Remove(new ExtraSeatInfo(TrainShift, seat));


                new FreezeSeatInfo(TrainShift, new DestinationSeat(seat, new List <TrainStationWay>()
                {
                    trainStationWay
                }));
                new FreezeSeatInfo(TrainShift, new DestinationSeat(seat, new List <TrainStationWay>()
                {
                    trainStationWay
                }));

                var            price          = TrainShift.TrainNumber.GetTrainStationWayPrice(trainStationWay, seat.SeatType);
                TrainOrderItem trainOrderItem = new TrainOrderItem(seat, userContract, price);
                trainOrderItems.Add(trainOrderItem);
            }


            TrainOrder trainOrder = new TrainOrder(trainStationWay.StartTrainStation, trainStationWay.EndTrainStation, CustomerInfo, trainOrderItems);

            return(true, "预定成功", trainOrder);
        }
Beispiel #10
0
 public (bool, string, TrainOrder) BookTrainTicket(TrainShift TrainShift, TrainStation StartTrainStation, TrainStation EndTrainStation, SeatTypeConfig SeatType, CustomerInfo CustomerInfo, IList <UserContract> UserContracts)
 {
     return(BookTrainTicket(TrainShift, StartTrainStation, EndTrainStation, new List <SeatTypeConfig> {
         SeatType
     }, CustomerInfo, UserContracts));
 }