// Metod för att avsluta parking static void CheckOutSpaceship(People character) { using (var db = new SpaceParkContext()) { Console.WriteLine("Which spaceship do you want to check out?"); var characterShips = from b in db.Parking where b.Name == character.Name && b.Payment == 0 select b; var newList = characterShips.ToList(); for (int i = 0; i < newList.Count; i++) { Console.WriteLine($"{i}) {newList[i].Spaceship}"); } var choice = Convert.ToInt32(Console.ReadLine()); var shipToCheckOut = from a in db.Parking where a.Name == character.Name && a.Spaceship == newList[choice].Spaceship && newList[choice].Payment == 0 select a; var shipToCheckOutList = shipToCheckOut.ToList(); shipToCheckOutList[0].ParkingEnd = DateTime.Now; var duration = (shipToCheckOutList[0].ParkingStart - shipToCheckOutList[0].ParkingEnd).Duration().TotalSeconds; shipToCheckOutList[0].Payment = Math.Round((decimal)(duration * 10)); db.SaveChanges(); Console.WriteLine($"You parked for {duration} seconds and were billed {shipToCheckOutList[0].Payment} republic credits"); } }
public static async void StoreParking(dynamic person, dynamic starship, int time, int price) { SpaceParkContext db = new SpaceParkContext(); db.Database.EnsureCreated(); //primary key blir till siffran från swapi url var starshipId = Int32.Parse(starship["url"].Remove(0, 31).Trim('/')); var personId = Int32.Parse(person["url"].Remove(0, 28).Trim('/')); Task persontask = StorePersonInDatabase(person, personId); Task starshiptask = StoreStarShipInDatabase(starship, starshipId); var park = new ParkEvent() { StarShipId = starshipId, PersonId = personId, TimeParked = time.ToString(), Price = price }; db.ParkEvent.Add(park); await Task.WhenAll(persontask, starshiptask); db.SaveChanges(); }
public static async Task PayParking(Person p) { using (var context = new SpaceParkContext()) { Console.WriteLine(); // If the person has not payed, change the value of hasPaid to true in the people table. if (!(HasPersonPaid(p).Result)) { context.People .Where(x => x.Name == p.Name) .FirstOrDefault() .HasPaid = true; Console.WriteLine("Parking has been paid & you are now ready to check out!"); Thread.Sleep(2500); } else { Console.WriteLine("You have already paid!"); Thread.Sleep(2500); } context.SaveChanges(); } }
private static async Task NullSpaceShipIDInPeopleTable(Person p, SpaceParkContext context) { // Find the person in the people table and sets the spaceshipID to null. context.People.Where(x => x.Name == p.Name) .FirstOrDefault().SpaceShipID = null; context.SaveChanges(); }
static void Main(string[] args) { using var context = new SpaceParkContext(); SpacePort spacePort = SpacePort.CreateSpacePort(context); ParkingLot.CheckParkingSpaces(context, spacePort); Menu.GenerateProgram(context); }
public static async Task <Person> GetPersonFromDatabase(string name) { using (var context = new SpaceParkContext()) { // return the person object from the people table with a matching name. return(context.People .Where(x => x.Name == name) .FirstOrDefault()); } }
public static async Task ClearParkedShip(SpaceShip spaceShip) { using (var context = new SpaceParkContext()) { // Finds the ship in the person table and set it to null. context.ParkingLot.Where(x => x.SpaceShipID == spaceShip.SpaceShipID) .FirstOrDefault() .SpaceShip = null; context.SaveChanges(); } }
public async static Task <bool> IsPersonInDatabase(string name) { // Returns true if a person with tha maching name is stored in the people table. using (var context = new SpaceParkContext()) { var person = context.People.Where(x => x.Name == name).FirstOrDefault(); if (person != null && person.Name == name) { return(true); } return(false); } }
private static async Task StorePersonInDatabase(dynamic person, int personId) { //lägg till om den inte finns await Task.Run(() => { SpaceParkContext db = new SpaceParkContext(); var personQuery = db.Person.Find(personId); if (personQuery == null) { var Person = new Person() { Name = person["name"], PersonId = personId }; db.Person.Add(Person); db.SaveChanges(); } }); }
private static async Task StoreStarShipInDatabase(dynamic starship, int starshipId) { //lägg till om den inte finns await Task.Run(() => { SpaceParkContext db = new SpaceParkContext(); var starShipQuery = db.StarShip.Find(starshipId); if (starShipQuery == null) { StarShip starShip = new StarShip() { StarShipName = starship["name"], StarShipId = starshipId }; db.StarShip.Add(starShip); db.SaveChanges(); } }); }
public static async Task WriteParkingSpaceToDataBase() { // Makes sure there are 10 rows to the parkinglot table. using (var context = new SpaceParkContext()) { for (int i = context.ParkingLot.Count(); i < 10; i++) { var parkingSpace = new ParkingLot { Length = 50, SpaceShip = null }; context.ParkingLot.Add(parkingSpace); } context.SaveChanges(); } }
public static async Task <ParkingLot> FindAvailableParkingSpace() { using (var context = new SpaceParkContext()) { // Finds the first available (where SpaceShipID == null) parkingspot, and then returns that spot. var parkingSpace = context.ParkingLot.FirstOrDefault(x => x.SpaceShipID == null); if (parkingSpace == null) { // Should throw an exception here but does not work with catch. Console.WriteLine(); Console.WriteLine("There aren't any parking spaces available."); Thread.Sleep(2500); return(null); } return(parkingSpace); } }
public static async Task <bool> HasPersonPaid(Person p) { using (var context = new SpaceParkContext()) { // Finds the person in the people table and checks if the value of hasPaid is true or false, // then returns that value. var hasPaid = context .People .Where(x => x.Name == p.Name) .FirstOrDefault().HasPaid; if (hasPaid) { return(true); } return(false); } }
// Metod för att parkera rymdskepp static void ParkSpaceShip(People character, List <Spaceship> characterShips) { Console.WriteLine(); Console.WriteLine($"Choose which spaceship you want to park: "); // Skriver ut rymdskepp som hör till karaktären for (int i = 0; i < characterShips.Count; i++) { Console.WriteLine($"{i}) {characterShips[i].Name}"); } // Tar in vilket rymdskepp användaren vill parkera Console.WriteLine(); Console.Write("Input: "); var shipInput = Console.ReadLine(); var chosenShip = -1; for (int i = 0; i < characterShips.Count; i++) { if (shipInput == i.ToString()) { chosenShip = i; } } if (chosenShip == -1) { throw new Exception("Invalid ship input"); } using (var db = new SpaceParkContext()) { Console.WriteLine("Input being saved to database"); var parking = new Parking { Name = character.Name, Spaceship = characterShips[chosenShip].Name, ParkingStart = DateTime.Now }; db.Parking.Add(parking); db.SaveChanges(); } }
public static async Task ParkShip(Person p) { ParkingLot currentSpace; using (var context = new SpaceParkContext()) { try { currentSpace = FindAvailableParkingSpace().Result; // If the ship is smaller than the parkingspace park in the space => park it. if (double.Parse(p.CurrentShip.Length) <= currentSpace.Length) { context.ParkingLot.Where(x => x.ParkingLotID == currentSpace.ParkingLotID) .FirstOrDefault() .SpaceShip = p.CurrentShip; } else { Console.WriteLine("Sorry your ship is to big!"); Thread.Sleep(2500); } } catch (Exception) { Console.WriteLine("No avaialable parking spaces!"); Thread.Sleep(2500); } context.SpaceShips.Add(p.CurrentShip); context.People.Add(p); // Adds the person and the ship to the appropriate table then saves the changes. context.SaveChanges(); Console.WriteLine("Your ship has been parked."); Thread.Sleep(2500); } }
public static async Task CheckOut(Person p) { if (p.HasPaid) { using (var context = new SpaceParkContext()) { // Sets the parkingspaces' shipID back to null. context.ParkingLot.Where(x => x.SpaceShipID == p.SpaceShipID) .FirstOrDefault() .SpaceShipID = null; // Nulls a persons current shipID await NullSpaceShipIDInPeopleTable(p, context); //Removes the curernt person from the person table context.Remove(context.People .Where(x => x.Name == p.Name) .FirstOrDefault()); // Borde inte denna och den ovan se exakt lika ut? var temp = context.SpaceShips.Where(x => x.SpaceShipID == p.SpaceShipID) .FirstOrDefault(); context.Remove(temp); context.SaveChanges(); } Console.WriteLine(); Console.WriteLine("You have been checked out!"); Thread.Sleep(2500); } else { Console.WriteLine(); Console.WriteLine("Sorry you have to pay first!"); Thread.Sleep(2500); } }