public static void Withdraw(decimal amount, ref decimal balance) { if (amount >= 0) { if (Validation.ValidateWithdrawal(amount, balance)) { balance -= amount; OutputHandling.Message("Withdrawn Amount: " + amount); CheckBalance(balance); } else { OutputHandling.Error("Insufficient funds!"); CheckBalance(balance); OutputHandling.Message("You tried to withdraw: " + amount); } } else { OutputHandling.Error(lesserThanEqualZeroError); ATMInput.Withdraw(ref balance); } }
public static void DeletePublisher(int publisherId) { string q_deletePublisher = "DeletePublisher"; SqlParameter p_publisherId = new SqlParameter { Value = publisherId, SqlDbType = SqlDbType.Int, ParameterName = "publisherId", }; SqlCommand c_deletePublisher = new SqlCommand(q_deletePublisher, dbBooksConn); c_deletePublisher.CommandType = CommandType.StoredProcedure; c_deletePublisher.Parameters.Add(p_publisherId); try { dbBooksConn.Open(); c_deletePublisher.ExecuteNonQuery(); OutputHandling.Message($"The publisher with id {publisherId} and all of it's associated books have been removed", ConsoleColor.Green); } catch { OutputHandling.Error("Invalid Publisher Id"); } finally { dbBooksConn.Close(); } }
static void Main(string[] args) { InvalidOperationException invalidOperationException = new InvalidOperationException(message: "Invalid number"); try { int x = Convert.ToInt32(Console.ReadLine()); if (x < 0) { throw invalidOperationException; } Console.WriteLine("{0:F3}", Math.Sqrt(x)); } catch { OutputHandling.Error("Invalid number"); } finally { OutputHandling.Message("Good bye!"); Console.ReadKey(); } }
private static void UpdatePublisher(SqlParameter bookId) { int newPbId = InputHandling.ReadValue("New Publisher Id: "); SqlParameter newPublisherId = new SqlParameter { Value = newPbId, SqlDbType = SqlDbType.Int, ParameterName = "newPublisherId" }; string updatePublisherIdString = "Update BOOK SET PublisherId = @newPublisherId WHERE BookId = @bookId"; SqlCommand updatePublisherCommand = new SqlCommand(updatePublisherIdString, dbBooksConn); updatePublisherCommand.Parameters.Add(bookId); updatePublisherCommand.Parameters.Add(newPublisherId); try { dbBooksConn.Open(); updatePublisherCommand.ExecuteNonQuery(); OutputHandling.Message($"Book price field for book id {bookId} Updated Successfully", ConsoleColor.Green); } catch { OutputHandling.Error("Invalid Book Id"); } finally { dbBooksConn.Close(); } }
public static void Download(string url) { try { Uri uri = new Uri(url); string fileName = File.SetName(uri); WebClient webClient = new WebClient(); try { File.TryOverWrite(ref fileName, uri); Download(uri, fileName, webClient); } catch (WebException) { OutputHandling.Error("EMPTY INNER PATH NAME NOT LEGAL"); OutputHandling.Error($"UNABLE TO DOWNLOAD {fileName}, PLEASE MAKE SURE THAT THE FILE PATH IS CORRECT"); } } catch (UriFormatException) { OutputHandling.Error("INVALID URI"); OutputHandling.Error("INVALID URL FORMAT SPECIFIED"); } }
private static void UpdateTitle(SqlParameter bookId) { string newBkTitle = InputHandling.ReadString("New book Title: "); SqlParameter newBookTitle = new SqlParameter { Value = newBkTitle, SqlDbType = SqlDbType.VarChar, ParameterName = "newTitle" }; string updateTitleString = "Update BOOK SET Title = @newTitle WHERE BookId = @bookId"; SqlCommand updateTitleCommand = new SqlCommand(updateTitleString, dbBooksConn); updateTitleCommand.Parameters.Add(bookId); updateTitleCommand.Parameters.Add(newBookTitle); try { dbBooksConn.Open(); updateTitleCommand.ExecuteNonQuery(); } catch { OutputHandling.Error("Invalid Book Id"); } finally { dbBooksConn.Close(); } }
public static void InsertBook(string bTitle, int pId, int bYear, int bPrice) { // Query to execute command string insertString = "INSERT INTO Book (Title, PublisherId, Year, Price) " + "VALUES (@bookTitle, @publisherId, @year, @price);" + "SELECT CAST(scope_identity() AS INT)"; // Create Command SqlCommand insertBook = new SqlCommand { Connection = dbBooksConn, CommandText = insertString }; // Define parameters SqlParameter bookTitle = new SqlParameter { Value = bTitle, SqlDbType = SqlDbType.VarChar, ParameterName = "bookTitle" }; SqlParameter bookPublisherId = new SqlParameter { Value = pId, SqlDbType = SqlDbType.Int, ParameterName = "publisherId" }; SqlParameter bookYear = new SqlParameter { Value = bYear, SqlDbType = SqlDbType.Int, ParameterName = "year" }; SqlParameter bookPrice = new SqlParameter { Value = bPrice, SqlDbType = SqlDbType.Int, ParameterName = "price" }; // Add Parameters to command insertBook.Parameters.Add(bookTitle); insertBook.Parameters.Add(bookPublisherId); insertBook.Parameters.Add(bookYear); insertBook.Parameters.Add(bookPrice); try { // Open Connection dbBooksConn.Open(); // Execute the command var bkId = insertBook.ExecuteScalar(); OutputHandling.Message($"The book with id {bkId} has been successfully added", ConsoleColor.Green); } catch { // Throw Error OutputHandling.Error("There is already a book with the same Id in the database"); } finally { dbBooksConn.Close(); } }
public static void Deposit(decimal amount, ref decimal balance) { if (amount > 0) { balance += amount; OutputHandling.Message("Deposited sum: " + amount); CheckBalance(balance); } else { OutputHandling.Error(lesserThanEqualZeroError); ATMInput.Deposit(ref balance); } }
public static void InsertPublisher(int pId, string pName) { // Query to execute command string insertString = "SET IDENTITY_INSERT dbo.Publisher ON;" + "INSERT INTO Publisher (PublisherId, Name) " + "VALUES (@publisherId, @publisherName);" + "SELECT CAST(scope_identity() AS INT)"; // Create Command SqlCommand insertPublisher = new SqlCommand { Connection = dbBooksConn, CommandText = insertString }; // Define parameters SqlParameter publisherId = new SqlParameter { Value = pId, SqlDbType = SqlDbType.Int, ParameterName = "publisherId" }; SqlParameter publisherName = new SqlParameter { Value = pName, SqlDbType = SqlDbType.VarChar, ParameterName = "publisherName" }; // Add Parameters to command insertPublisher.Parameters.Add(publisherId); insertPublisher.Parameters.Add(publisherName); try { // Open Connection dbBooksConn.Open(); // Execute the command var pubId = insertPublisher.ExecuteScalar(); OutputHandling.Message($"Publisher with Id {pubId} has been successfully added", ConsoleColor.Green); } catch { // Throw Error OutputHandling.Error("There is already a publisher with the same Id in the database"); } finally { dbBooksConn.Dispose(); } }
public static void Download(string url) { WebClient webClient = new WebClient(); try { Uri uri = new Uri(url); string fileName = FileOps.SetName(uri); Uri currentPath = new Uri(Directory.GetCurrentDirectory() + '/' + fileName); if (uri.AbsolutePath == currentPath.AbsolutePath) { OutputHandling.Error("UNABLE TO DOWNLOAD A FILE TO THE SAME LOCATION THAT THE ORIGINAL FILE RESIDES IN"); return; } try { FileOps.TryOverWrite(ref fileName, uri); Download(uri, fileName, webClient); } catch (WebException) { OutputHandling.Error($"UNABLE TO DOWNLOAD {fileName}, PLEASE MAKE SURE THAT THE FILE PATH IS CORRECT"); } finally { webClient.Dispose(); } } catch (UriFormatException) { OutputHandling.Error("INVALID URL FORMAT SPECIFIED"); } finally { webClient.Dispose(); } }
public static void ReadFileContents(string filePath) { StringBuilder fileContents = new StringBuilder(); fileContents.Append("*** BEGINNING OF FILE***\n\n"); try { fileContents.Append(File.ReadAllText(filePath)); fileContents.Append("\n\n*** END OF FILE***"); PrintFileContents(fileContents.ToString()); } catch (ArgumentException) { OutputHandling.Error("PATH NAMES CANNOT CONTAIN SPECIAL CHARACTERS OR EMPTY SPACES"); } catch (DirectoryNotFoundException) { OutputHandling.Error("NO SUCH DIRECTORY!"); } catch (FileNotFoundException) { OutputHandling.Error("FILE NOT FOUND!"); } catch (UnauthorizedAccessException) { OutputHandling.Error("YOU ARE NOT ALLOWED TO ACCESS THIS PATH"); } catch (Exception) { OutputHandling.Error("UNKNOWN EXCEPTION"); } finally { fileContents.Clear(); ExitProgram(); } }
public static void UpdateBook(int bId, ConsoleKeyInfo cki) { SqlParameter bookId = new SqlParameter { Value = bId, SqlDbType = SqlDbType.Int, ParameterName = "bookId" }; if (cki.Key.Equals(ConsoleKey.D1) || cki.Key.Equals(ConsoleKey.NumPad1)) { UpdateTitle(bookId); IOHandler.UpdateMenu(bId); } else if (cki.Key.Equals(ConsoleKey.D2) || cki.Key.Equals(ConsoleKey.NumPad2)) { UpdatePublisher(bookId); IOHandler.UpdateMenu(bId); } else if (cki.Key.Equals(ConsoleKey.D3) || cki.Key.Equals(ConsoleKey.NumPad3)) { UpdateYear(bookId); IOHandler.UpdateMenu(bId); } else if (cki.Key.Equals(ConsoleKey.D4) || cki.Key.Equals(ConsoleKey.NumPad4)) { UpdatePrice(bookId); IOHandler.UpdateMenu(bId); } else if (cki.Key.Equals(ConsoleKey.D5) || cki.Key.Equals(ConsoleKey.NumPad5)) { return; } else { OutputHandling.Error("Invalid menu option"); IOHandler.UpdateMenu(bId); } }
public static void NumberOfRows() { string selectString = "SELECT COUNT(*) FROM Publisher"; SqlCommand fetchNumberOfRows = new SqlCommand(selectString, sqlConnection); try { sqlConnection.Open(); var numberOfRows = fetchNumberOfRows.ExecuteScalar(); OutputHandling.Message($"Number of rows = {numberOfRows}"); } catch { OutputHandling.Error("Cannot connect to database"); } finally { sqlConnection.Close(); } }
public static void DeleteBook(SqlParameter bookId) { string deleteString = "DELETE FROM Book WHERE BookId = @bookId"; SqlCommand deleteCommand = new SqlCommand(deleteString, dbBooksConn); deleteCommand.Parameters.Add(bookId); try { dbBooksConn.Open(); deleteCommand.ExecuteNonQuery(); OutputHandling.Message($"The book with the id {bookId.Value} has been removed from the database", ConsoleColor.Green); } catch { OutputHandling.Error("Could not perform operation!"); } finally { dbBooksConn.Close(); } }