Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.ComponentModel.DataAnnotations;
  6.  
  7. namespace Web_S10189876.Models
  8. {
  9. public class Fine
  10. {
  11. [Display(Name = "Due Date")]
  12. [DataType(DataType.Date)]
  13. [DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}")]
  14. public DateTime DueDate { get; set; }
  15. [Display(Name = "Number of Books Overdue")]
  16. [Range(1, 10, ErrorMessage =
  17. "Invalid value! Please enter a value from 1 to 10")]
  18. public int NumBooksOverdue { get; set; }
  19. [Display(Name = "Number of Days Overdue")]
  20. public int NumDaysOverdue { get; set; }
  21. [Display(Name = "Fine Rate (SGD)")]
  22. [DisplayFormat(DataFormatString = "{0:#,##0.00}",
  23. ApplyFormatInEditMode = true)]
  24. public double FineRate { get; set; }
  25. [Display(Name = "Fine (SGD)")]
  26. [DisplayFormat(DataFormatString = "{0:#,##0.00}")]
  27. public double FineAmt { get; set; }
  28. }
  29.  
  30. }
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. using System;
  45. using System.Collections.Generic;
  46. using System.Linq;
  47. using System.Threading.Tasks;
  48. using Microsoft.AspNetCore.Mvc;
  49. using Microsoft.AspNetCore.Mvc.Rendering;
  50. using Web_S10189876.Models;
  51.  
  52.  
  53. namespace Web_S10189876.Controllers
  54. {
  55. public class FineController : Controller
  56. {
  57. public ActionResult Calculate()
  58. {
  59. //Prepare the ViewData to be use in Calculate.cshtml view
  60. ViewData["ShowResult"] = false;
  61. Fine fine = new Fine();
  62. fine.DueDate = DateTime.Today;
  63. fine.FineRate = 0.50;
  64. return View(fine);
  65. }
  66. [HttpPost]
  67. public ActionResult Calculate(Fine fine)
  68. {
  69. // The fine object contains user inputs from view
  70. if (!ModelState.IsValid) // validation fails
  71. {
  72. return View(fine); // returns the view with errors
  73. }
  74. // Calculate the cumulative fine and its breakdown
  75. double fineTotal = 0.0;
  76. string fineBreakdown = "";
  77. for (int count = 1; count <= fine.NumBooksOverdue; count++)
  78. {
  79. double fineForEachBook = count * fine.FineRate *
  80. fine.NumDaysOverdue;
  81. fineTotal += fineForEachBook;
  82. fineBreakdown += "Overdue cost for Book " + count + " = $" +
  83. fineForEachBook.ToString("#,##0.00") +
  84. "<br />";
  85. }
  86. fine.FineAmt = fineTotal;
  87. // Prepare the ViewData to be used in Calculate.cshtml view
  88. ViewData["ShowResult"] = true;
  89. ViewData["FineBreakdown"] = fineBreakdown;
  90. // Route to Calculate.cshtml view to display result
  91. return View(fine);
  92. }
  93. }
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. @model Web_S10189876.Models.Fine
  107.  
  108. @{
  109. ViewData["Title"] = "Calculate";
  110. Layout = "~/Views/Shared/_Layout.cshtml";
  111. }
  112.  
  113. <h4 class="PageTitle">Fine Calculator</h4>
  114. <hr />
  115. <form asp-controller="Fine" asp-action="Calculate" method="post">
  116. <fieldset class="FormContent">
  117. <legend>Input Data:</legend>
  118. <div asp-validation-summary="ModelOnly"
  119. class="text-danger"></div>
  120. <div class="form-group row">
  121. <label asp-for="DueDate"
  122. class="col-sm-4 col-form-label"></label>
  123. <div class="col-sm-8 col-md-4">
  124. <input asp-for="DueDate" class="form-control"
  125. max="@DateTime.Today.ToString("yyyy-MM-dd")" />
  126. </div>
  127. </div>
  128. <div class="form-group row">
  129. <label asp-for="NumDaysOverdue"
  130. class="col-sm-4 col-form-label"></label>
  131. <div class="col-sm-8">
  132. <input asp-for="NumDaysOverdue" readonly
  133. class="form-control-plaintext" />
  134. </div>
  135. </div>
  136. <div class="form-group row">
  137. <label asp-for="FineRate"
  138. class="col-sm-4 col-form-label"></label>
  139. <div class="col-sm-8">
  140. <input asp-for="FineRate" readonly
  141. class="form-control-plaintext" />
  142. <span style="font-style:italic;">
  143. (rate per day for the 1st book,
  144. double for the 2nd, triple for the 3rd ...)
  145. </span>
  146. </div>
  147. </div>
  148. <div class="form-group row">
  149. <label asp-for="NumBooksOverdue"
  150. class="col-sm-4 col-form-label"></label>
  151. <div class="col-sm-8 col-md-4">
  152. <input asp-for="NumBooksOverdue" class="form-control"
  153. required />
  154. <span asp-validation-for="NumBooksOverdue"
  155. class="text-danger"></span>
  156. </div>
  157. </div>
  158. <div class="form-group row">
  159. <div class="col-sm-8 offset-sm-4">
  160. <input type="submit" value="Compute"
  161. class="btn btn-primary" />
  162. </div>
  163. </div>
  164. </fieldset>
  165. </form>
  166. @if (Convert.ToBoolean(ViewData["ShowResult"]))
  167. {
  168. <fieldset class="FormContent">
  169. <legend>Result:</legend>
  170. <div class="form-group row">
  171. <label asp-for="FineAmt"
  172. class="col-sm-4 col-form-label"></label>
  173. <div class="col-sm-8 form-control-plaintext">
  174. @Html.Display("FineAmt")
  175. </div>
  176. </div>
  177. <div class="form-group row">
  178. <label class="col-sm-4 col-form-label">
  179. Breakdown of the Fine
  180. </label>
  181. <div class="col-sm-8 form-control-plaintext">
  182. @Html.Raw(ViewData["FineBreakdown"])
  183. </div>
  184. </div>
  185. </fieldset>
  186. }
  187. <script>
  188. // Calculate and display the days overdue when the due date is set
  189. document.getElementById("DueDate").onchange = function () {
  190. // The number of milliseconds in one day
  191. var oneday = 24 * 60 * 60 * 1000;
  192. var today = new Date();
  193. var dueDate = new Date(
  194. document.getElementById("DueDate").value);
  195. // Convert both dates to milliseconds
  196. var today_ms = today.getTime();
  197. var dueDate_ms = dueDate.getTime();
  198. // Calculate the difference in milliseconds
  199. var difference_ms = today_ms - dueDate_ms;
  200. // Convert back to days
  201. var numDayOverdue = Math.floor(difference_ms / oneday);
  202. // Assign the value to input field
  203. document.getElementById("NumDaysOverdue").value
  204. = numDayOverdue;
  205. };
  206. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement