public static TripState Get(TripContext context) { Debug.Assert(context != null); Debug.Assert(context.Trip != null); var tripStateStatus = context.Trip.TripStateStatus; switch (tripStateStatus) { case TripState.Status.Create: return(new TripStateCreate(context)); case TripState.Status.AddPackages: return(new TripStateAddPackages(context)); case TripState.Status.ChoosePaymentType: return(new TripStateChoosePaymentType(context)); case TripState.Status.AddThankYou: return(new TripStateAddThankYou(context)); case TripState.Status.Complete: return(new TripStateComplete(context)); default: throw new NotSupportedException($"{tripStateStatus} is an invalid state"); } }
public override TripStateLoop.Status Execute() { Console.WriteLine(Environment.NewLine + "*** ADD THANK YOU ***"); Console.WriteLine(); Console.WriteLine("- COMMAND: [later] to return later or write thank you note (5 chars min)"); var getThankYou = true; string thankYou = null; while (getThankYou) { thankYou = (Console.ReadLine() ?? "").Trim(); //come back later? if (ReturnLater(thankYou)) { return(TripStateLoop.Status.Stop); } //stop if we can change state getThankYou = !IsThankYouValid(thankYou); } TripContext.Trip.ThankYou = thankYou; TripContext.ChangeState(new TripStateComplete(TripContext)); return(TripStateLoop.Status.Continue); }
public override TripStateLoop.Status Execute() { decimal amount = 0; Console.WriteLine(Environment.NewLine + "*** ACCEPT CASH PAYMENT ***"); Console.WriteLine(); Console.WriteLine("- COMMAND: [later] to return later or amount"); Console.WriteLine(); Console.WriteLine($"The total amount for the trip is: ${TripContext.Trip.totalPrice}"); Console.WriteLine("- Enter amount or [later] to return later"); Console.WriteLine(); // loops to handle user input var enterAmount = true; while (enterAmount) { string newAmount = (Console.ReadLine() ?? "").Trim(); //int payerNumber = Int32.Parse(Console.ReadLine()); if (Decimal.TryParse(newAmount, out amount)) { if (amount > TripContext.Trip.totalPrice) { Console.WriteLine("You are overpaying, we don't need your pity, please re-enter amount"); continue; } if (amount < TripContext.Trip.totalPrice) { Console.WriteLine("You are underpaying, please, we are struggling, re-enter amount"); continue; } } else if (ReturnLater(newAmount)) { return(TripStateLoop.Status.Stop); } else { Console.WriteLine("Please enter a valid number"); } break; } TripContext.Trip.Payment = new PaymentCash(amount); Console.WriteLine($"{TripContext.Trip.Payer} paid {TripContext.Trip.totalPrice} by cash"); //change state when finished TripContext.ChangeState(new TripStateAddThankYou(TripContext)); return(TripStateLoop.Status.Continue); }
protected TripState(TripContext context, Status tripStateStatus) { TripContext = context; if (TripContext.Trip != null) { TripContext.Trip.TripStateStatus = tripStateStatus; } }
public TripStateCreate(TripContext context) : base(context, TripState.Status.Create) { TripContext.Trip = new Trip() { BookedOn = DateTime.Now, OrderId = DateTime.Now.Ticks, TripStateStatus = TripState.Status.Create }; }
public TripStateAddPackages(TripContext context) : base(context, TripState.Status.AddPackages) { }
public TripStateAddThankYou(TripContext context) : base(context, TripState.Status.AddThankYou) { }
//Hardcoded existing Trips public override TripStateLoop.Status Execute() { List <TripContext> existingTrips = new List <TripContext>(); var tripStateLoop = new TripStateLoop(); //TripContext.Trip.TripAgent.LoadTrips(); //public TripStateLoop.Status LoadTrips() { Trip incompleteTrip = new Trip(); incompleteTrip.TripStateStatus = TripState.Status.AddPackages; incompleteTrip.tripID = 129046; incompleteTrip.BookedOn = DateTime.Now; incompleteTrip.OrderId = DateTime.Now.Ticks; incompleteTrip.TripAgent = TripContext.Trip.TripAgent; incompleteTrip.selectedTravellers.Add(new Person("Bojack H.", "478-691-9002")); incompleteTrip.selectedTravellers.Add(new Person("Mr. Peanutbutter", "478-691-8181")); TripContext context1 = new TripContext(incompleteTrip); Trip completeTrip = new Trip(); completeTrip.TripStateStatus = TripState.Status.Complete; completeTrip.tripID = 19067; completeTrip.TripAgent = TripContext.Trip.TripAgent; completeTrip.selectedTravellers.Add(new Person("Bojack H.", "478-691-9002")); completeTrip.selectedTravellers.Add(new Person("Mr. Peanutbutter", "478-691-8181")); completeTrip.BookedOn = DateTime.Now; completeTrip.OrderId = DateTime.Now.Ticks; completeTrip.selectedPacks.Add(new Package("LAX Airport", "Atlanta Airport", new DateTime(2019, 2, 25, 12, 0, 0), new DateTime(2019, 1, 20, 15, 0, 0), Package.TransportType.PrivateJet)); completeTrip.Payer = completeTrip.selectedTravellers[0]; completeTrip.totalPrice = completeTrip.selectedPacks[0].price; completeTrip.Payment = new PaymentCash(completeTrip.totalPrice); completeTrip.ThankYou = "Thanks Bojack, you're the greatest"; TripContext context2 = new TripContext(completeTrip); existingTrips.Add(context1); existingTrips.Add(context2); Console.WriteLine("Choose the number of the trip you wish to continue"); for (int existingTripIndex = 0; existingTripIndex < existingTrips.Count; existingTripIndex++) { Console.WriteLine($"{existingTripIndex+1}. TripID:{existingTrips[existingTripIndex].Trip.tripID}, Status: {existingTrips[existingTripIndex].Trip.TripStateStatus}"); } int tripSelector = 0; //loop to handle user input var selectTrip = true; while (selectTrip) { string newTrip = (Console.ReadLine() ?? "").Trim(); if (Int32.TryParse(newTrip, out tripSelector)) { if (tripSelector > existingTrips.Count || tripSelector < 1) { Console.WriteLine("Please enter a valid number"); continue; } //existingTrips[tripSelector - 1].Execute(); var existingTrip = tripStateLoop.Execute(existingTrips[tripSelector - 1].Trip); while (true) { ShowItinerary(existingTrip); Console.WriteLine( Environment.NewLine + "Simulate trip reload to correct state? [yes]"); if ((Console.ReadLine() ?? "").ToLower().Trim() == "yes") { tripStateLoop.Execute(existingTrip); } else { break; } } return(TripStateLoop.Status.Continue); } Console.WriteLine("Please enter a valid number"); } return(TripStateLoop.Status.Continue); } }
public TripStateHandleExisting(TripContext context) : base(context, TripState.Status.HandleExisting) { }
public override TripStateLoop.Status Execute() { int cardNumber = 0; string expDate; decimal amount = 0; Console.WriteLine(Environment.NewLine + "*** ACCEPT CARD PAYMENT ***"); Console.WriteLine("Enter Card Number or [later] to return later"); var selectCardNumber = true; while (selectCardNumber) { string newCardNumber = (Console.ReadLine() ?? "").Trim(); //int payerNumber = Int32.Parse(Console.ReadLine()); if (Int32.TryParse(newCardNumber, out int number)) { cardNumber = number; break; } else if (ReturnLater(newCardNumber)) { return(TripStateLoop.Status.Stop); } else { Console.WriteLine("Please enter a valid number"); } } Console.WriteLine("Enter card expiration date (mm/yy) or [later] to return later"); var inputExpDate = true; while (inputExpDate) { string newExpDate = (Console.ReadLine() ?? "").Trim(); //int payerNumber = Int32.Parse(Console.ReadLine()); if (ReturnLater(newExpDate)) { return(TripStateLoop.Status.Stop); } else { expDate = newExpDate; } break; } Console.WriteLine(); Console.WriteLine($"The total amount for the trip is: ${TripContext.Trip.totalPrice}"); Console.WriteLine("- Enter amount or [later] to return later"); Console.WriteLine(); var enterAmount = true; while (enterAmount) { string newAmount = (Console.ReadLine() ?? "").Trim(); //int payerNumber = Int32.Parse(Console.ReadLine()); if (Decimal.TryParse(newAmount, out amount)) { if (amount > TripContext.Trip.totalPrice) { Console.WriteLine("You are overpaying, we don't need your pity, please re-enter amount"); continue; } if (amount < TripContext.Trip.totalPrice) { Console.WriteLine("You are underpaying, please, we are struggling, re-enter amount"); continue; } } else if (ReturnLater(newAmount)) { return(TripStateLoop.Status.Stop); } else { Console.WriteLine("Please enter a valid number"); } break; } TripContext.Trip.Payment = new PaymentCard(cardNumber, amount, "02/21)"); Console.WriteLine($"{TripContext.Trip.Payer} paid {TripContext.Trip.totalPrice} by card"); TripContext.ChangeState(new TripStateAddThankYou(TripContext)); return(TripStateLoop.Status.Continue); }
public override TripStateLoop.Status Execute() { for (int x = 0; x < TripContext.Trip.selectedPacks.Count; x++) { TripContext.Trip.totalPrice += Math.Abs(TripContext.Trip.selectedPacks[x].price); } Console.WriteLine($"THE TOTAL PRICE OF THE TRIP IS: ${TripContext.Trip.totalPrice}"); Console.WriteLine(); Console.WriteLine("COMMAND: ENTER THE NUMBER OF THE TRAVELLER WHO WILL BE PAYING OR [later] TO RETURN LATER:"); Console.WriteLine(); for (int x = 0; x < TripContext.Trip.selectedTravellers.Count; x++) { Console.WriteLine($"{x + 1}: " + TripContext.Trip.selectedTravellers[x].ToString()); } var selectPayer = true; while (selectPayer) { string newPayer = (Console.ReadLine() ?? "").Trim(); //int payerNumber = Int32.Parse(Console.ReadLine()); if (Int32.TryParse(newPayer, out int payerNumber) && (payerNumber > 0 && payerNumber < TripContext.Trip.selectedTravellers.Count + 1)) { Console.WriteLine($"Payer = { TripContext.Trip.selectedTravellers[payerNumber - 1].ToString() }"); TripContext.Trip.Payer = TripContext.Trip.selectedTravellers[payerNumber - 1]; break; } else if (ReturnLater(newPayer)) { return(TripStateLoop.Status.Stop); } else { Console.WriteLine("Please enter a valid number"); } } Console.WriteLine(Environment.NewLine + "*** CHOOSE PAYMENT TYPE ***"); Console.WriteLine(); Console.WriteLine("- COMMAND: [later] to return later, [cash] or [check] or [card]"); while (true) { var paymentType = (Console.ReadLine() ?? "").Trim(); if (ReturnLater(paymentType)) { return(TripStateLoop.Status.Stop); //exit loop and method } //empty entry does nothing if (string.IsNullOrWhiteSpace(paymentType)) { continue; } if (paymentType == "cash") { TripContext.ChangeState(new TripStatePayCash(TripContext)); return(TripStateLoop.Status.Continue); } if (paymentType == "check") { TripContext.ChangeState(new TripStatePayCheck(TripContext)); return(TripStateLoop.Status.Continue); } if (paymentType == "card") { TripContext.ChangeState(new TripStatePayCard(TripContext)); return(TripStateLoop.Status.Continue); } Console.WriteLine("- ERROR: [later], [cash], [check], or [card]"); } }
public TripStateChoosePaymentType(TripContext context) : base(context, TripState.Status.ChoosePaymentType) { }
public override TripStateLoop.Status Execute() { Console.WriteLine(Environment.NewLine + "*** ADD PACKAGES ***"); Console.WriteLine(); for (int x = 0; x < premadePacks.Count; x++) { Console.WriteLine($"{x + 1}: " + premadePacks[x].ToString()); Console.WriteLine(); } ShowCurrentDestinations(); Console.WriteLine( "- COMMAND: [later] to return later, [done] to finish destinations, or enter number of package"); var selectPackages = true; while (selectPackages) { string newPackage = (Console.ReadLine() ?? "").Trim(); if (ReturnLater(newPackage)) { return(TripStateLoop.Status.Stop); } int packSelect; if (Int32.TryParse(newPackage, out packSelect)) { if (packSelect > premadePacks.Count || packSelect < 1) { Console.WriteLine("Please enter a valid number"); continue; } } //come back later? //if (ReturnLater(newPackage)) return TripStateLoop.Status.Stop; else { Console.WriteLine("Please enter a valid number"); } //check unique and continue entering if (ContinueEnteringDestinations(newPackage)) { if (IsDestinationValid(newPackage, packSelect - 1)) { TripContext.Trip.selectedPacks.Add(premadePacks[packSelect - 1]); Console.WriteLine($"- Added Package [{premadePacks[packSelect-1].ToString()}]"); } } else { //stop if we can change state selectPackages = !IsDestinationListValid(); } } TripContext.ChangeState(new TripStateChoosePaymentType(TripContext)); return(TripStateLoop.Status.Continue); }
public TripStatePayCard(TripContext context) : base(context, TripState.Status.PayCard) { }
public override TripStateLoop.Status Execute() { int id = 0; int agentNumber; Console.WriteLine(); Console.WriteLine("Please select the agent by entering the number corresponding to the agent"); for (int x = 0; x < travelAgents.Count; x++) { Console.WriteLine($"{x + 1}: " + travelAgents[x].ToString()); } //loops to handle user input var enterAgentNumber = true; while (enterAgentNumber) { string newAgentNumber = (Console.ReadLine() ?? "").Trim(); if (Int32.TryParse(newAgentNumber, out agentNumber)) { if (agentNumber > travelAgents.Count || agentNumber < 1) { Console.WriteLine("Please enter a valid number"); continue; } TripContext.Trip.TripAgent = travelAgents[agentNumber - 1]; Console.WriteLine($"Welcome {travelAgents[agentNumber -1].name}".ToUpper()); break; } Console.WriteLine("Please enter a valid number"); } Console.WriteLine(); Console.WriteLine("To view existing trips, enter [yes], to create a new trip hit the enter key"); Console.WriteLine(); var chooseExisting = true; while (chooseExisting) { string decision = (Console.ReadLine() ?? "").Trim(); if (GoToExisting(decision)) { TripContext.ChangeState(new TripStateHandleExisting(TripContext)); return(TripStateLoop.Status.Continue); } break; } Console.WriteLine(); Console.WriteLine("*** NEW TRIP CREATED ***"); Console.WriteLine(" Enter trip ID for the new trip"); var enterTripID = true; while (enterTripID) { string newAmount = (Console.ReadLine() ?? "").Trim(); //int payerNumber = Int32.Parse(Console.ReadLine()); if (Int32.TryParse(newAmount, out id)) { TripContext.Trip.tripID = id; break; } Console.WriteLine("Please enter a valid number"); } Console.WriteLine("Please add travellers to the trip by entering the number corresponding to the person"); Console.WriteLine(); for (int x = 0; x < travellers.Count; x++) { Console.WriteLine($"{x + 1}: " + travellers[x].ToString()); } Console.WriteLine(); var selectTravellers = true; while (selectTravellers) { string newTraveller = (Console.ReadLine() ?? "").Trim(); //int packSelect = Int32.Parse(newPackage)-1; if (Int32.TryParse(newTraveller, out int travelSelect)) { if (travelSelect > travellers.Count || travelSelect < 1) { Console.WriteLine("Please enter a valid number"); continue; } } if (ReturnLater(newTraveller)) { return(TripStateLoop.Status.Stop); } if (ContinueEnteringDestinations(newTraveller)) { if (IsDestinationValid(newTraveller, travelSelect - 1)) { TripContext.Trip.selectedTravellers.Add(travellers[travelSelect - 1]); Console.WriteLine($"- Added Traveller [{travellers[travelSelect-1].ToString()}]"); } } else { //stop if we can change state selectTravellers = !IsDestinationListValid(); } } TripContext.ChangeState(new TripStateAddPackages(TripContext)); return(TripStateLoop.Status.Continue); }
public TripStateComplete(TripContext context) : base(context, TripState.Status.Complete) { }
public TripStatePayCheck(TripContext context) : base(context, TripState.Status.PayCheck) { }