Advertisement
Guest User

blugh

a guest
Jan 16th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.79 KB | None | 0 0
  1. //A program to calculate a simple premium rate for a gadget protection company.
  2. package policy.mannager;
  3.  
  4. import java.util.Scanner;
  5. import java.io.File
  6. import java.io.FileNotFoundException
  7. import java.io.FileWriter
  8. import java.io.IOException
  9. import java.io.PrintWriter
  10. import java.io.LineNumberReader
  11. import java.io.FileReader
  12. import java.text.SimpleDateFormat;
  13. import java.util.Calendar;
  14.  
  15. /**
  16. *
  17. * @author s6085604
  18. */
  19.  
  20. public class PolicyMannager {
  21. //holds the main scanner allowing input from the client
  22. public static Scanner keyboard = new Scanner(System.in);
  23.  
  24. public static void main(String[] args)
  25. {
  26. //calls the main menu
  27. displayMenu();
  28.  
  29. }
  30. static void displayMenu()
  31. { //Shows the main menu which the client has to choose from
  32. String mainMenu;
  33. System.out.println("Please choose an option from below");
  34. System.out.println(" ");
  35. System.out.println("1. Enter new policy");
  36. System.out.println("2. Display summary of policies");
  37. System.out.println("3. Display summary of policies for selected month");
  38. System.out.println("4. Find and display policy");
  39. System.out.println("0. Exit");
  40.  
  41. //takes the input from the client
  42. mainMenu = keyboard.nextLine();
  43.  
  44. //validates the clients choice ensuring it's valid
  45. while (!(mainMenu.matches("[0-4]{1}")))
  46. {
  47. System.out.println("Invalid option, please try again: ");
  48. mainMenu = keyboard.nextLine();
  49. }
  50. switch(mainMenu)
  51. {
  52. case "1":
  53. newPolicy();
  54. break;
  55. case "2":
  56. fileSummary();
  57. break;
  58. case "3":
  59. monthSummary();
  60. break;
  61. case "4":
  62. searchFile();
  63. break;
  64. case "0":
  65. System.out.println("Exiting...");
  66. break;
  67.  
  68. }//end of switch
  69. }// end of main menu
  70.  
  71. static void newPolicy()
  72. {
  73. //holds the methods that call the getters and setters in the systems, allowing the attributes to be set for them
  74. policy newPolicy = new policy();
  75. newPolicy.setName(getName());
  76. newPolicy.setRefNum(getNumber());
  77. newPolicy.setGadgetNumber(getGadget());
  78. newPolicy.setGadgetValue(getExpensive());
  79. newPolicy.setExcess(getExcess());
  80. newPolicy.setTerm(getTerms());
  81. newPolicy.setDate();
  82. newPolicy.setPremium();
  83.  
  84. //calls the method that allows the system to write to a file
  85. writeToFile(newPolicy.getDate(), newPolicy.getNumber(), newPolicy.getGadget(),newPolicy.getExpensive(), newPolicy.getExcess(), newPolicy.getTerms());
  86.  
  87. //Calls the method that displays the policy method
  88. displayPolicy(newPolicy.getDate(), newPolicy.getNumber(), newPolicy.getGadget(),newPolicy.getExpensive(), newPolicy.getExcess(), newPolicy.getTerms());
  89.  
  90. //returns the client back to the menu
  91. backToMenu();
  92. }//end of new policy string
  93. public static String getName()
  94. //gets the name from the client
  95. {
  96. System.out.println("Please enter your name (e.g. John Smith) : "); //20 character maximum
  97. String name = keyboard.nextLine();
  98. //validates and ensures the name doesn't exceed 20 characters
  99. while (name.length() <1 || name.length() >20)
  100. {
  101. System.out.println("Name must be between 1 and 20 chars long");
  102. System.out.println("Name inputted is too long. Please re-enter staff name (e.g. John Smith): ");
  103. name = keyboard.nextLine();
  104. }
  105. return name;
  106. }//end of getName string
  107.  
  108. public static String getDate()
  109. //gets the date
  110. {
  111. Calendar cal = Calendar.getInstance();
  112. SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
  113. return sdf.format(cal.getTime());
  114. }
  115.  
  116.  
  117.  
  118. public static String getNumber()
  119. //gets the customer reference number from the client
  120. {
  121. System.out.println("Please enter your customer reference number (e.g. AB123C) : "); //validate
  122. String customerNum = keyboard.nextLine().toUpperCase();
  123. //checks that the customer reference number is of the correct length and formatted in the correct way
  124. boolean valid = false;
  125. while (!valid)
  126. {
  127. valid = true;
  128. if (customerNum.length() != 6)
  129. {
  130. System.out.println("Customer reference number should be 6 charaters long (e.g. AB123C)");
  131. valid = false;
  132. }
  133. else if (!Character.isLetter(customerNum.charAt(0)) || !Character.isLetter(customerNum.charAt(1)) ||
  134. !Character.isDigit(customerNum.charAt(2)) || !Character.isDigit(customerNum.charAt(3)) ||
  135. !Character.isDigit(customerNum.charAt(4)) || !Character.isLetter(customerNum.charAt(5)))
  136. {
  137. System.out.println("Customer reference number should be 2 letters followed by 3 digits, followed by a single letter (e.g. AB123C)" );
  138. valid = false;
  139. }
  140. if (!valid)
  141. {
  142. System.out.print("Please enter customer reference number (e.g. AB123C) : ");
  143. customerNum = keyboard.nextLine();
  144. }
  145. }
  146. return customerNum;
  147. }//end of getNumber string
  148.  
  149. public static int getGadget()
  150. //gets the total number of gadgets that the client is buying
  151. {
  152. String quantity;
  153.  
  154. System.out.println("Please enter your total number of gadgets : ");
  155.  
  156. quantity = keyboard.next();
  157.  
  158. while(!(quantity.matches(".*[0-9].*")))
  159. {
  160. System.out.println("You have inputted an invalled data type");
  161. System.out.println("Please try again");
  162. }
  163. //validates and ensures that only positive amounts of gadgets can be bought and that the client can't miss the feild
  164. while (Integer.parseInt(quantity) <1)
  165. {
  166. System.out.println("Please enter your total number of gadgets : ");
  167. quantity = keyboard.next();
  168. }
  169.  
  170. return Integer.parseInt(quantity);
  171. }//end of getGadget() string
  172.  
  173. public static double getExpensive()
  174. //gets the total of the most expensive gadget the client has bought
  175. {
  176. System.out.println("Please enter the value of your most expensive gadget (e.g. £960) : ");
  177. double newPolicy.getExpensive();
  178. int gadgetLimit;
  179. newPolicy.getExpensive() = keyboard.nextDouble();
  180.  
  181. //ensures the value isn't below 0
  182. while (getExpensive() <= 0)
  183. {
  184. System.out.println("A negative value has been entered.");
  185. System.out.print("Please enter a positive value : ");
  186. getExpensive() = keyboard.nextDouble();
  187.  
  188. }
  189. //ensures the cost doesn't exceed £1000.00
  190. while (getExpensive() > 1000.00)
  191. {
  192. System.out.println("Please enter a value below £1000");
  193. getExpensive() = keyboard.nextDouble();
  194. }
  195.  
  196. //code to asign the correct limit to the value entered
  197. if (getExpensive() <= 1000 && getExpensive() > 800)
  198. {
  199. gadgetLimit = 1000;
  200. }
  201. else if (getExpensive() <= 800 && getExpensive() > 550)
  202. {
  203. gadgetLimit = 800;
  204. }
  205. else if ( getExpensive() <=550 && getExpensive() >0)
  206. {
  207. gadgetLimit = 550;
  208. }
  209. else
  210. {
  211. gadgetLimit = (int)getExpensive();
  212. }
  213. return getExpensive();
  214. }//end of getExpensive string
  215.  
  216. public static double getExcess()
  217. //gets the amount of newPolicy.getExcess() the client would like to pay
  218. {
  219. System.out.println("newPolicy.getExcess() must be between £30.00 and £70.00");
  220. System.out.println("Please enter how much newPolicy.getExcess() you would like to pay (e.g. £30.00) : ");
  221. double excess = keyboard.nextDouble();
  222. //validation to ensure that the newPolicy.getExcess() doesn't go below or above the boundaries
  223. while (newPolicy.getExcess() <30.00 && newPolicy.getExcess() >70.00)
  224. {
  225. System.out.println("Please enter a value above £30.00 and below or equal to £70.00");
  226. newPolicy.getExcess() = keyboard.nextDouble();
  227. }
  228.  
  229. return newPolicy.getExcess();
  230. }//end of getnewPolicy.getExcess() string
  231.  
  232. static String getTerms()
  233. // gets the payment terms from the client
  234. {
  235. char payment;
  236. String term;
  237.  
  238.  
  239. System.out.print("Would you like to pay monthly (M) or annually (A) : ");
  240. payment = Character.toUpperCase(keyboard.next().charAt(0));
  241.  
  242. //ensures the correct characters have been entered
  243. while (payment != 'A' && payment != 'M')
  244. {
  245. System.out.print("Invalid entry, please try again (A or M) : ");
  246. payment = Character.toUpperCase(keyboard.next().charAt(0));
  247.  
  248. }
  249.  
  250. //asigns the correct term to the input the client gave
  251. if (payment == 'A')
  252. {
  253. term = "Annual";
  254. }
  255. else
  256. {
  257. term = "Monthly";
  258. }
  259. return term;
  260.  
  261. }
  262.  
  263.  
  264.  
  265.  
  266. }
  267.  
  268. static void readFromFile(String file, int option)
  269. {
  270. //variables that are used to read the file into the computer
  271. int total = 0;
  272. int monthTotal = 0;
  273. int jan = 0;
  274. int feb = 0;
  275. int mar = 0;
  276. int apr = 0;
  277. int may = 0;
  278. int jun = 0;
  279. int jul = 0;
  280. int aug = 0;
  281. int sep = 0;
  282. int oct = 0;
  283. int nov = 0;
  284. int dec = 0;
  285. String month = " ";
  286. String search = " ";
  287.  
  288. //sees which option the client is coming from
  289.  
  290. if (option == 2)
  291. {
  292. System.out.println("Please enter the abreviated foorm of the month you want e.g. Jul");
  293. month = keyboard.next().toLowerCase();
  294. while (!(month.equals ("jan") || month.equals ("feb") || month.equals ("mar") || month.equals ("apr") || month.equals ("may") || month.equals ("jun") || month.equals ("jul") || month.equals ("aug") || month.equals ("sep") || month.equals ("oct") || month.equals ("nov") || month.equals ("dec")));
  295. {
  296. System.out.println("Invalid entry please try again");
  297. month = keyboard.next().toLowerCase();
  298. }
  299.  
  300. }// end of if
  301. else if (option == 3)
  302. {
  303. System.out.println("Please enter search");
  304. search = keyboard.next().toLowerCase();
  305. }//end of else if
  306.  
  307. try
  308. {
  309. File currentFile = new File (file);
  310. if (currentFile.exists())
  311. {
  312. while (lnr.readLine() !=null)
  313. {
  314. total++;
  315. }
  316.  
  317. }
  318. else
  319. {
  320. System.out.println("File does not exist");
  321. }
  322. }
  323.  
  324. } catch (IOExeption e) {}
  325.  
  326. Scanner input = null;
  327.  
  328.  
  329.  
  330. //String to assign the word to the number of gadgets being bought
  331. String number;
  332. if (newPolicy.getGadget() == 1)
  333. {
  334. number = "One";
  335. }
  336. else if (newPolicy.getGadget() == 2)
  337. {
  338. number = "Two";
  339. }
  340. else if (newPolicy.getGadget() == 3)
  341. {
  342. number = "Three";
  343. }
  344. else if (newPolicy.getGadget() == 4)
  345. {
  346. number = "Four";
  347. }
  348. else if (newPolicy.getGadget() == 5)
  349. {
  350. number = "Five";
  351. }
  352. else
  353. {
  354. number = Integer.toString(newPolicy.getGadget());
  355. }
  356.  
  357.  
  358.  
  359. //code to show the output box
  360. System.out.println(" ");
  361. System.out.println(" +=============================================+");
  362. System.out.println(" | |");
  363. System.out.printf (" |" + "%10s %-20s %14s %n", "Client: " ,newPolicy.getName(), " " + "|");
  364. System.out.println(" | |");
  365. System.out.printf (" |" + "%10s %-20s %5s %-25s %n", "Date", newPolicy.getDate(), "Ref", newPolicy.getNumber() + " |");
  366. System.out.printf (" |" + "%10s %-19s %6s %-5s %2s %n", "Terms: ", newPolicy.getTerms(), "Items:", number, "|");
  367. System.out.printf (" |" + "%10s £%-20.2f %13s %n", "newPolicy.getExcess(): ", newPolicy.getExcess(), " " + "|" );
  368. System.out.println(" | |");
  369. //code to display any needed error messages in the clients inputs
  370. if (newPolicy.getGadget() > 5)
  371. {
  372. System.out.println(" | Your payment has been rejected due to the |");
  373. System.out.println(" | number of gadgets exceeding the limit |");
  374. }
  375. else if (getExpensive() > 1000)
  376. {
  377. System.out.println(" | Your payment has been rejected due to the |");
  378. System.out.println(" | value of gadgets exceeding the limit |");
  379. }
  380. else
  381. {
  382. System.out.printf(" |" + "%9s %-16s %-10s %8s %n", "Annual ","", "Limit per" , " |");
  383. System.out.printf(" |" + "%10s £%-15.2f %1s %2s %8s %n", "Premium: " , newPolicy.getPremium() ,"", "Gadget:" , getExpensive() + " |");
  384. }
  385. System.out.println(" | |");
  386. System.out.println(" +=============================================+");
  387.  
  388. }//end of create policy string
  389. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement