// Purchase a ship public Ship newShip(bool keepLooping) { string model = ""; do { try { Console.WriteLine($"\nWallet: {this.GetCredits()} credits"); string[] modelNames = { "Collosus", "SpitFire" }; Console.Write($"\nSelect the type of ship you want to purchase, {this.name}:\n" + $"\n1. {modelNames[0]}\t1000 credits" + $"\n2. {modelNames[1]}\t\t2000 credits\n\nC: Cancel\n\n>>> "); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (Enumerable.Range(1, 4).Contains(selection.GetSelection())) { model = modelNames[selection.GetSelection() - 1]; Ship newShip = new Ship(model); if (this.GetCredits() >= newShip.GetPrice()) { keepLooping = false; } else { Console.Clear(); throw new Exception("\nYou don't have enough credits to purchase the selected model."); } } else { Console.Clear(); throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } while (keepLooping); return(new Ship(model)); }
// Buy goods public void BuyGoods(string buyMenu, Items[] tradingGoods) { bool keepLooping = true; do { try { if (GetCargo().Count() != GetShip().GetCargoCapacity()) { Console.Write(buyMenu); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (Enumerable.Range(1, 10).Contains(selection.GetSelection())) { Console.Write("\nHow many units would you like to buy?\n\nC: Cancel\n\n>>> "); MenuSelection quantity = new MenuSelection(Console.ReadLine().Trim()); // Checks if the user can carry anymore cargo. if (quantity.GetSelection() == 0) { break; } else if (GetCargo().Count() + quantity.GetSelection() <= GetShip().GetCargoCapacity()) { if (GetCredits() >= quantity.GetSelection() * tradingGoods[selection.GetSelection() - 1].GetPrice()) { // Adds an item to the player's cargo for (int i = 0; i < quantity.GetSelection(); i++) { AddCargo(tradingGoods[selection.GetSelection() - 1]); } keepLooping = false; } else { throw new Exception("\nYou don't have enough credit to purchase this good."); } } else { throw new Exception("\nThere isn't enough room in the ship's cargo bay for the selected quantity. Select fewer items."); } } else { throw new Exception("\nInvalid Entry"); } } else { keepLooping = false; throw new Exception("\nThe ship's cargo bay is full. Sell some goods before you attempt to purchase more."); } } catch (Exception ex) { Console.Clear(); Console.WriteLine(ex.Message); } } while (keepLooping); }
// Sell goods public void SellGoods(string sellMenu, Items[] tradingGoods) { bool keepLooping = true; do { try { Console.Write(sellMenu); MenuSelection selection = new MenuSelection(Console.ReadLine().Trim()); if (selection.GetSelection() == 0) { break; } else if (Enumerable.Range(1, 10).Contains(selection.GetSelection())) { // Counts how many of selected items does the user have in his cargo int count = 0; foreach (Items items in GetCargo()) { if (items.GetName() == tradingGoods[selection.GetSelection() - 1].GetName()) { count++; } } if (count > 0) { Console.Write("\nHow many units would you like to sell?\n\nC: Cancel\n\n>>> "); MenuSelection quantity = new MenuSelection(Console.ReadLine().Trim()); if (quantity.GetSelection() == 0) { break; } if (quantity.GetSelection() <= count) { // removes the sold item from the user's cargo for (int i = 0; i < quantity.GetSelection(); i++) { RemoveCargo(tradingGoods[selection.GetSelection() - 1]); } keepLooping = false; } else { int number = 0; foreach (Items items in GetCargo()) { if (items.GetName() == tradingGoods[selection.GetSelection() - 1].GetName()) { number++; } } throw new Exception($"\nYou have only {number} {tradingGoods[selection.GetSelection() - 1].GetName()} in your cargo bay."); } } else { keepLooping = false; throw new Exception("\nYou don't have any of the selected goods in the cargo bay."); } } else { throw new Exception("\nInvalid Entry"); } } catch (Exception ex) { Console.Clear(); Console.WriteLine(ex.Message); } } while (keepLooping); }