Advertisement
Guest User

lol

a guest
Jan 17th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.13 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package policymanager;
  7.  
  8. import java.util.Scanner;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileWriter;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14. import java.io.LineNumberReader;
  15. import java.io.FileReader;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Calendar;
  18.  
  19. /**
  20. *
  21. * @author s6085604
  22. */
  23. public class PolicyManager {
  24.  
  25. //holds the main scanner allowing input from the client
  26. public static Scanner keyboard = new Scanner(System.in);
  27.  
  28. public static void main(String[] args) {
  29. //calls the main menu
  30. displayMenu();
  31. }//end of main
  32.  
  33. static void displayMenu()
  34. { //Shows the main menu which the client has to choose from
  35. String mainMenu;
  36. System.out.println("Please choose an option from below");
  37. System.out.println(" ");
  38. System.out.println("1. Enter new policy");
  39. System.out.println("2. Display summary of policies");
  40. System.out.println("3. Display summary of policies for selected month");
  41. System.out.println("4. Find and display policy");
  42. System.out.println("0. Exit");
  43. //takes the input from the client
  44. mainMenu = keyboard.nextLine();
  45.  
  46. //validates the clients choice ensuring it's valid
  47. while (!(mainMenu.matches("[0-4]{1}")))
  48. {
  49. System.out.println("Invalid option, please try again: ");
  50. mainMenu = keyboard.nextLine();
  51. }
  52. switch(mainMenu)
  53. {
  54. case "1":
  55. newPolicy();
  56. break;
  57. case "2":
  58. fileSummary();
  59. break;
  60. case "3":
  61. monthSummary();
  62. break;
  63. case "4":
  64. searchFile();
  65. break;
  66. case "0":
  67. System.out.println("Exiting...");
  68. break;
  69.  
  70. }//end of switch
  71. }//emd of displau menu
  72.  
  73. static void newPolicy()
  74. {
  75. //holds the methods that call the getters and setters in the systems, allowing the attributes to be set for them
  76. Policy newPolicy = new Policy();
  77. newPolicy.setName(getName());
  78. newPolicy.setRefNum(getNumber());
  79. newPolicy.setGadgetNumber(getGadget());
  80. newPolicy.setGadgetValue(getGadgetValue());
  81. newPolicy.setExcess(getExcess());
  82. newPolicy.setTerm(getTerms());
  83. newPolicy.setDate();
  84. newPolicy.setPremium();
  85.  
  86.  
  87. String gadgetNumber = " ";
  88.  
  89. if (newPolicy.getGadget() == 1)
  90. {
  91. gadgetNumber = "One";
  92. }
  93. else if (newPolicy.getGadget() == 2)
  94. {
  95. gadgetNumber = "Two";
  96. }
  97. else if (newPolicy.getGadget() == 3)
  98. {
  99. gadgetNumber = "Three";
  100. }
  101. else if (newPolicy.getGadget() == 4)
  102. {
  103. gadgetNumber = "Four";
  104. }
  105. else if (newPolicy.getGadget() == 5)
  106. {
  107. gadgetNumber = "Five";
  108. }
  109. else
  110. {
  111. gadgetNumber = Integer.toString(newPolicy.getGadget());
  112. }
  113.  
  114. //code to show the output box
  115. System.out.println(" ");
  116. System.out.println(" +=============================================+");
  117. System.out.println(" | |");
  118. System.out.printf (" |" + "%10s %-20s %14s %n", "Client: " ,newPolicy.getName(), " " + "|");
  119. System.out.println(" | |");
  120. System.out.printf (" |" + "%10s %-20s %5s %-25s %n", "Date", newPolicy.getDate(), "Ref", newPolicy.getNumber() + " |");
  121. System.out.printf (" |" + "%10s %-19s %6s %-5s %2s %n", "Terms: ", newPolicy.getTerms(), "Items:", newPolicy.getGadget(), "|");
  122. System.out.printf (" |" + "%10s £%-20.2f %13s %n", "newPolicy.getExcess(): ", newPolicy.getExcess(), " " + "|" );
  123. System.out.println(" | |");
  124. //code to display any needed error messages in the clients inputs
  125. if (newPolicy.getGadget() > 5)
  126. {
  127. System.out.println(" | Your payment has been rejected due to the |");
  128. System.out.println(" | number of gadgets exceeding the limit |");
  129. }
  130. else if (newPolicy.getGadgetValue() > 1000)
  131. {
  132. System.out.println(" | Your payment has been rejected due to the |");
  133. System.out.println(" | value of gadgets exceeding the limit |");
  134. }
  135. else
  136. {
  137. System.out.printf(" |" + "%9s %-16s %-10s %8s %n", "Annual ","", "Limit per" , " |");
  138. System.out.printf(" |" + "%10s £%-15.2f %1s %2s %8s %n", "Premium: " , newPolicy.getPremium() ,"", "Gadget:" , newPolicy.getGadgetValue() + " |");
  139. }
  140. System.out.println(" | |");
  141. System.out.println(" +=============================================+");
  142. }//end of new policy
  143.  
  144. static String getName()
  145. //gets the name from the client
  146. {
  147. System.out.println("Please enter your name (e.g. John Smith) : "); //20 character maximum
  148. String name = keyboard.nextLine();
  149. //validates and ensures the name doesn't exceed 20 characters
  150. while (name.length() <1 || name.length() >20)
  151. {
  152. System.out.println("Name must be between 1 and 20 chars long");
  153. System.out.println("Name inputted is too long. Please re-enter staff name (e.g. John Smith): ");
  154. name = keyboard.nextLine();
  155. }
  156. return name;
  157. }//end of getName string
  158.  
  159. static String getNumber()
  160. //gets the customer reference number from the client
  161. {
  162. System.out.println("Please enter your customer reference number (e.g. AB123C) : "); //validate
  163. String customerNum = keyboard.nextLine().toUpperCase();
  164. //checks that the customer reference number is of the correct length and formatted in the correct way
  165. boolean valid = false;
  166. while (!valid)
  167. {
  168. valid = true;
  169. if (customerNum.length() != 6)
  170. {
  171. System.out.println("Customer reference number should be 6 charaters long (e.g. AB123C)");
  172. valid = false;
  173. }
  174. else if (!Character.isLetter(customerNum.charAt(0)) || !Character.isLetter(customerNum.charAt(1)) ||
  175. !Character.isDigit(customerNum.charAt(2)) || !Character.isDigit(customerNum.charAt(3)) ||
  176. !Character.isDigit(customerNum.charAt(4)) || !Character.isLetter(customerNum.charAt(5)))
  177. {
  178. System.out.println("Customer reference number should be 2 letters followed by 3 digits, followed by a single letter (e.g. AB123C)" );
  179. valid = false;
  180. }
  181. if (!valid)
  182. {
  183. System.out.print("Please enter customer reference number (e.g. AB123C) : ");
  184. customerNum = keyboard.nextLine();
  185. }
  186. }
  187. return customerNum;
  188. }//end of getNumber string
  189.  
  190. static int getGadget()
  191. //gets the total number of gadgets that the client is buying
  192. {
  193. String quantity;
  194.  
  195. System.out.println("Please enter your total number of gadgets : ");
  196.  
  197. quantity = keyboard.next();
  198.  
  199. while(!(quantity.matches(".*[0-9].*")))
  200. {
  201. System.out.println("You have inputted an invalled data type");
  202. System.out.println("Please try again");
  203. }
  204. //validates and ensures that only positive amounts of gadgets can be bought and that the client can't miss the feild
  205. while (Integer.parseInt(quantity) <1)
  206. {
  207. System.out.println("Please enter your total number of gadgets : ");
  208. quantity = keyboard.next();
  209. }
  210.  
  211. return Integer.parseInt(quantity);
  212. }//end of getGadget() string
  213.  
  214. static double getGadgetValue()
  215. //gets the total of the most expensive gadget the client has bought
  216. {
  217. System.out.println("Please enter the value of your most expensive gadget (e.g. £960) : ");
  218. double gadgetValue;
  219. int gadgetLimit;
  220. gadgetValue = keyboard.nextDouble();
  221.  
  222. //ensures the value isn't below 0
  223. while (gadgetValue <= 0)
  224. {
  225. System.out.println("A negative value has been entered.");
  226. System.out.print("Please enter a positive value : ");
  227. gadgetValue= keyboard.nextDouble();
  228.  
  229. }
  230. //ensures the cost doesn't exceed £1000.00
  231. while (gadgetValue > 1000.00)
  232. {
  233. System.out.println("Please enter a value below £1000");
  234. gadgetValue = keyboard.nextDouble();
  235. }
  236. return gadgetValue;
  237. }
  238.  
  239. static double getExcess()
  240. //gets the amount of excess the client would like to pay
  241. {
  242. System.out.println("Excess must be between £30.00 and £70.00");
  243. System.out.println("Please enter how much excess you would like to pay (e.g. £30.00) : ");
  244. double excess = keyboard.nextDouble();
  245. //validation to ensure that the excess doesn't go below or above the boundaries
  246. while (excess <30.00 && excess > 70.00)
  247. {
  248. System.out.println("Please enter a value above £30.00 and below or equal to £70.00");
  249. excess = keyboard.nextDouble();
  250. }
  251.  
  252. return excess;
  253. }//end of excess string
  254.  
  255. static String getTerms()
  256. // gets the payment terms from the client
  257. {
  258. char payment;
  259. String term;
  260.  
  261.  
  262. System.out.print("Would you like to pay monthly (M) or annually (A) : ");
  263. payment = Character.toUpperCase(keyboard.next().charAt(0));
  264.  
  265. //ensures the correct characters have been entered
  266. while (payment != 'A' && payment != 'M')
  267. {
  268. System.out.print("Invalid entry, please try again (A or M) : ");
  269. payment = Character.toUpperCase(keyboard.next().charAt(0));
  270.  
  271. }
  272.  
  273. //asigns the correct term to the input the client gave
  274. if (payment == 'A')
  275. {
  276. term = "Annual";
  277. }
  278. else
  279. {
  280. term = "Monthly";
  281. }
  282. return term;
  283.  
  284. }//end of get terms string
  285. static void writeToFile(String date, String reference, int gadgetNum, double gadgetCost, double excessCost, double premium, String paymentType, String name)
  286. {
  287. PrintWriter output = null;
  288.  
  289. File policy = new File("policy.txt");
  290.  
  291. // checks to see if the file the client requested exists
  292.  
  293. if ( ! policy.exists())
  294. {
  295. try {
  296. output = new PrintWriter(policy);
  297.  
  298. } catch (FileNotFoundException e)
  299.  
  300. {
  301. System.out.println("The file can not be created. Exiting...");
  302. System.exit(0); // this closes the system
  303. } catch (IOException ex)
  304. {
  305. System.out.println("The file can not be created. Exiting...");
  306. System.exit(0);
  307. }
  308. if (gadgetNum > 5 || gadgetCost > 1000)
  309. {
  310. premium = -1;
  311. paymentType = "r";
  312. }
  313. else
  314. {
  315. premium = premium * 100;
  316. }
  317. output.print(date + "/t");
  318. output.print(reference + "/t");
  319. output.print(gadgetNum + "/t");
  320. output.print(gadgetCost + "/t");
  321. output.print(excessCost + "/t");
  322. output.print(premium + "/t");
  323. output.print(paymentType + "/t");
  324. output.print(name + "/t");
  325. output.close();
  326. }//end of if statement
  327. }//end of write to file string
  328.  
  329. }//end of class policy manager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement