Guest User

Untitled

a guest
Aug 21st, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. Show only Message from ApplicationException
  2. static void getBookInfo(Book book)
  3. {
  4. bool isNumeric;
  5. float number;
  6. string numberInput;
  7.  
  8. Console.Write("Enter Book Title: ");
  9. book.Title = Console.ReadLine();
  10. Console.Write("Enter Author's First Name: ");
  11. book.AuthorFirstName = Console.ReadLine();
  12. Console.Write("Enter Author's Last Name: ");
  13. book.AuthorLastName = Console.ReadLine();
  14. Console.Write("Enter Book Price: $");
  15. numberInput = Console.ReadLine();
  16.  
  17. isNumeric = float.TryParse(numberInput, out number);
  18.  
  19. if (isNumeric)
  20. book.Price = number;
  21. else
  22. {
  23. throw new ApplicationException
  24. (
  25. "This is not a number!n" +
  26. "Please try again."
  27. );
  28. }
  29. }
  30.  
  31. using System;
  32.  
  33. namespace Lab_6
  34. {
  35. class Program
  36. {
  37. static void Main(string[] args)
  38. {
  39. Address address = new Address();
  40. address.StreetNumber = "800";
  41. address.StreetName = "East 96th Street";
  42. address.City = "Indianapolis";
  43. address.State = "IN";
  44. address.ZipCode = "46240";
  45.  
  46. Book book = new Book();
  47.  
  48. try
  49. {
  50. getBookInfo(book);
  51. book.PublisherAddress = address;
  52. book.PublisherName = "Sams Publishing";
  53.  
  54. Console.WriteLine("----Book----");
  55. book.display();
  56. }
  57. catch (NegativeInputException ex)
  58. {
  59. Console.WriteLine(ex.Message);
  60. return;
  61. }
  62. catch (ApplicationException ex)
  63. {
  64. Console.WriteLine(ex.Message); // I had to change so I have only this,
  65. // instead of whole printout.
  66. return;
  67. }
  68. }
  69.  
  70. static void getBookInfo(Book book)
  71. {
  72. bool isNumeric;
  73. float number;
  74. string numberInput;
  75.  
  76. Console.Write("Enter Book Title: ");
  77. book.Title = Console.ReadLine();
  78. Console.Write("Enter Author's First Name: ")
  79. book.AuthorFirstName = Console.ReadLine();
  80. Console.Write("Enter Author's Last Name: ");
  81. book.AuthorLastName = Console.ReadLine();
  82. Console.Write("Enter Book Price: $");
  83. numberInput = Console.ReadLine();
  84.  
  85. isNumeric = float.TryParse(numberInput, out number);
  86.  
  87. if (isNumeric)
  88. book.Price = number;
  89. else
  90. {
  91. throw new ApplicationException
  92. (
  93. "This is not a number!n" +
  94. "Please try again."
  95. )
  96. }
  97. }
  98. }
  99. }
  100.  
  101. if (isNumeric)
  102. {
  103. book.Price = number;
  104. }
  105. else
  106. {
  107. MessageBox.Show("This is not a number!n" + "Please try again.");
  108. }
  109.  
  110. try
  111. {
  112. getBookInfo(...)
  113. }
  114. catch (ApplicationException exception)
  115. {
  116. MessageBox.Show(exception.Message);
  117. }
  118.  
  119. try
  120. {
  121. Book myBookParameter = .....;
  122. getBookInfo(myBookParameter);
  123. }
  124. catch(ApplicationException x)
  125. {
  126. MessageBox.Show(x.Message);
  127. }
Add Comment
Please, Sign In to add comment