Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- */
- package policymanager;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- import java.util.Calendar;
- import java.text.SimpleDateFormat;
- import java.util.*;
- /**
- *
- * @author Michael Watts (N3071956) - School of Computing
- */
- public class PolicyManager {
- final private static Scanner UserInput = new Scanner(System.in);
- private static String name;
- private static String ref;
- private static String num;
- private static String payment;
- private static String terms[] = {"Annual", "Monthly"};
- private static String nums[] = {"One", "Two", "Three", "Four", "Five"};
- private static int numOfItemsRejected = 0;
- private static int excess;
- private static int gadgetLimit = 0;
- private static int b;
- private static double val;
- private static double mostExpensive;
- private static double premium = 0;
- private static double total = 0;
- private static double finishedPremium = 0;
- private static double endPremium = 0;
- private static double finishedTotal = 0;
- private static double premium1[][] = {{4.99, 6.15, 7.30}, {9.99, 12.35, 14.55}, {14.99, 18.60, 21.82}};
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) throws FileNotFoundException, IOException {
- //Display Menu
- int choice = returnMenu();
- //Menu loop
- while (choice != 0) {
- if (choice == 1) {
- Policy newPolicy = new Policy();
- newPolicy = createNewPolicy(newPolicy);
- } else if (choice == 2) {
- displaySummary();
- } else if (choice == 3) {
- displayMonth();
- } else if (choice == 4) {
- } else {
- System.out.println("Only menu choises 1-4 are valid");
- System.out.print("\nSelect menu item: ");
- choice = UserInput.nextInt();
- }
- choice = returnMenu();
- }
- }
- public static int returnMenu() {
- System.out.println(" Gadget Policy Prototype");
- System.out.println("---------------------------------");
- System.out.println("1. Create New Policy");
- System.out.println("---------------------------------");
- System.out.println("2. Display Summary Of Policies");
- System.out.println("---------------------------------");
- System.out.println("3. Current Policies For The Month");
- System.out.println("---------------------------------");
- System.out.println("4. Find and Display Policy");
- System.out.println("---------------------------------");
- System.out.println("0. Exit");
- System.out.println("---------------------------------");
- System.out.print("\nPlease Enter Your Choice: ");
- int option = UserInput.nextInt();
- UserInput.nextLine();
- return option;
- }
- public static Policy createNewPolicy(Policy newPol) throws IOException {
- System.out.println("----------------------------------------------------");
- System.out.println("Today's date of " + getDate() + " has been recorded on the policy");
- newPol.setPolicyDate(getDate());
- System.out.println("----------------------------------------------------");
- Scanner keyboardString = new Scanner(System.in);
- Scanner keyboardInt = new Scanner(System.in);
- boolean validate = false;
- while (validate == false)//A NAME, ONLY NEEDS TO BE 20 CHARS OR LESS. SIMPLE VALIDATION
- {
- System.out.println("Client Name Needed?: ");
- name = keyboardString.nextLine();
- if (name.length() > 20) {
- System.out.println("Name must be a maximum of 20 characters");
- System.out.println("You used " + name.length() + " characters");
- } else {
- newPol.setClientName(name);
- validate = true;
- }
- }
- System.out.println("----------------------------------------------------");
- validate = false;
- while (validate == false) {
- System.out.println("Please enter reference number?: ");
- ref = keyboardString.next().toUpperCase();
- validate = validateReference(ref);
- if (validate == true) {
- newPol.setReference(ref);
- }
- }
- System.out.println("----------------------------------------------------");
- validate = false;
- while (validate == false) {
- System.out.println("Please enter number of items between 1 and 5: ");
- num = keyboardString.next();
- String items = num;
- while (!Character.isDigit(num.charAt(0))) {
- System.out.println("Your selection needs to be a number.");
- num = keyboardString.next();
- }
- System.out.println("----------------------------------------------------");
- validate = false;
- while (!validate) {
- System.out.print("Highest gadget value? £ ");
- try {
- val = keyboardInt.nextDouble();
- if (val <= 0) {
- System.out.println("Value must be a positive number");
- }
- if (val > 0) {
- val = val * 100;
- newPol.setHighestGadgetValue((int) val);
- validate = true;
- }
- } catch (Exception e) //Exception handling for non-numbers
- {
- System.out.println("Must be in pounds and pence e.g. xx.xx");
- keyboardInt.next();
- }
- }
- System.out.println("----------------------------------------------------");
- validate = false;
- while (!validate) {
- System.out.print("Please enter excess? £ ");
- try {
- excess = keyboardInt.nextInt();
- if (excess == 30 || excess == 40 || excess == 50 || excess == 60 || excess == 70) {
- newPol.setExcess(excess);
- validate = true;
- } else {
- System.out.println("must be (minimum £30, max £70 in multiples of £10)");
- }
- } catch (Exception e) {
- System.out.println("You entered an invalid input. Please try again");
- keyboardInt.next();
- }
- }
- System.out.println("----------------------------------------------------");
- do {
- System.out.println("Your payment method?: a = annual or m = monthly");
- payment = keyboardString.next();
- if ((payment.length() == 1) && ((payment.equals("m")) || (payment.equals("a")))) {
- break;
- } else {
- System.out.println("Incorrect input, please enter a valid input.");
- }
- } while ((payment.length() != 1) || (!payment.equals("m") || (!payment.equals("a"))));
- System.out.println("----------------------------------------------------");
- if (num.charAt(0) == '1' && mostExpensive > 0 && mostExpensive <= 500) {
- premium = premium1[0][0];
- gadgetLimit = 500;
- }
- if (num.charAt(0) == '1' && mostExpensive > 500 && mostExpensive <= 750) {
- premium = premium1[0][1];
- gadgetLimit = 750;
- } else if (num.charAt(0) == '1'
- && mostExpensive > 750 && mostExpensive <= 1000) {
- premium = premium1[0][2];
- gadgetLimit = 1000;
- }
- if (num.charAt(0) == '2' || num.charAt(0) == '3'
- && mostExpensive > 0 && mostExpensive <= 500) {
- premium = premium1[1][0];
- gadgetLimit = 500;
- }
- if (num.charAt(0) == '2' || num.charAt(0) == '3'
- && mostExpensive > 500 && mostExpensive <= 750) {
- premium = premium1[1][1];
- gadgetLimit = 750;
- } else if (num.charAt(0) == '2' || num.charAt(0) == '3'
- && mostExpensive > 750 && mostExpensive <= 1000) {
- premium = premium1[1][2];
- gadgetLimit = 1000;
- }
- if (num.charAt(0) == '4' || num.charAt(0) == '5'
- && mostExpensive > 0 && mostExpensive <= 500) {
- premium = premium1[2][0];
- gadgetLimit = 500;
- }
- if (num.charAt(0) == '4' || num.charAt(0) == '5'
- && mostExpensive > 500 && mostExpensive <= 750) {
- premium = premium1[2][1] + 0;
- gadgetLimit = 750;
- } else if (num.charAt(0) == '4' || num.charAt(0) == '5' && mostExpensive > 750 && mostExpensive <= 1000) {
- premium = premium1[2][2];
- gadgetLimit = 1000;
- }
- if (excess == 30) {
- total = premium;
- finishedPremium = total;
- }
- if (excess == 40) {
- total = premium * .05;
- finishedPremium = premium - total;
- }
- if (excess == 50) {
- total = premium * .1;
- finishedPremium = premium - total;
- }
- if (excess == 60) {
- total = premium * .15;
- finishedPremium = premium - total;
- } else if (excess == 70) {
- total = premium * .2;
- finishedPremium = premium - total;
- }
- if (payment.charAt(0) == 'a') {
- endPremium = finishedPremium * .1;
- finishedTotal = (finishedPremium - endPremium) * 12;
- }
- if (payment.charAt(0) == 'm') {
- finishedTotal = finishedPremium;
- }
- if (payment.charAt(0) == 'a') {
- payment = terms[0];
- } else if (payment.charAt(0) == 'm') {
- payment = terms[1];
- }
- if (num.charAt(0) > '5' || num.charAt(0) <= 0
- || mostExpensive > 1000 || mostExpensive < 0) {
- payment = "Rejected";
- }
- if (num.charAt(0) == '1') {
- num = items;
- num = nums[0];
- }
- if (num.charAt(0) == '2') {
- num = items;
- num = nums[1];
- }
- if (num.charAt(0) == '3') {
- num = items;
- num = nums[2];
- }
- if (num.charAt(0) == '4') {
- num = items;
- num = nums[3];
- } else if (num.charAt(0) == '5') {
- num = items;
- num = nums[4];
- }
- //start of the menu
- //output top lines
- System.out.println("+-----------------------------------+");
- System.out.println("| |");
- //Outputs client name line
- System.out.print("| Client: " + name);
- //Loops around to make sure that the name is displayed properly
- for (int i = 0; i < 25 - name.length(); i++) {
- System.out.print(" ");
- }
- System.out.println("|");
- System.out.println("| |");
- System.out.println("| Date: " + getDate() + " Ref: " + ref
- + " |");
- //Print terms and items line
- System.out.print("| Terms: " + payment);
- if (payment.length() == 6) {
- System.out.print(" ");
- } else if (payment.length() == 7) {
- System.out.print(" ");
- } else {
- System.out.print(" ");
- }
- System.out.print("Items: " + num);
- for (int i = 0; i < 8 - num.length(); i++) {
- System.out.print(" ");
- }
- System.out.println("|");
- //Prints excess line
- System.out.print("| Excess: ");
- System.out.print(excess);
- System.out.println(" |");
- System.out.println("| |");
- //Prints terms and Limit per line
- System.out.print("| " + payment);
- if (payment.length() == 6) {
- System.out.print(" ");
- } else if (payment.length() == 7) {
- System.out.print(" ");
- } else {
- System.out.print(" ");
- }
- System.out.println(" Limit per |");
- //Prints Premium and Gadget line
- System.out.print("| Premium: ");
- if (finishedTotal == -1) {
- System.out.print("-");
- System.out.print(" ");
- } else {
- System.out.printf("£%2.2f", finishedTotal);
- if (finishedTotal < 10) {
- System.out.print(" ");
- } else if (finishedTotal < 100) {
- System.out.print(" ");
- } else if (finishedTotal <= 1000) {
- System.out.print(" ");
- }
- }
- System.out.print("Gadget: " + gadgetLimit);
- if (gadgetLimit < 10) {
- System.out.print(" ");
- } else if (gadgetLimit < 100) {
- System.out.print(" ");
- } else if (gadgetLimit < 1000) {
- System.out.print(" ");
- } else {
- System.out.print(" ");
- }
- System.out.println(" |");
- //Prints out bottom two rows
- System.out.println("| |");
- System.out.println("+-----------------------------------+");
- //end of the menu
- if (num.charAt(0) > 5 && num.charAt(0) <= 0
- && mostExpensive > 1000 && mostExpensive <= 0) {
- System.out.println("\nPolicy Rejected");
- numOfItemsRejected += Integer.parseInt(num);
- }
- System.out.println("\nWould you like to re-enter your policy details.");
- System.out.println("Press 1 for Restart and 2 to End: ");
- b = keyboardString.nextInt();
- while (b != 1 && b != 2) {
- System.out.println("\nPress 1 for Restart and 2 to End: ");
- }
- if (b == 1) {
- Policy newPolicy = new Policy();
- newPolicy = createNewPolicy(newPolicy);
- } else {
- System.out.println("Thank you using Policy Manager, you have selected End.");
- }
- File Policy = new File("Policy.txt");
- try {
- PrintWriter pw = new PrintWriter(new FileOutputStream(Policy, true));
- pw.println(getDate() + "\t" + ref + "\t" + num + "\t"
- + val + "\t" + excess + "\t" + finishedTotal + " " + payment.charAt(0) + "\t"
- + name); // write data to file specified by output
- // tidy up and close the file
- pw.close();
- } catch (FileNotFoundException e) {
- // file already exists, prevent overwriting
- {
- }
- }
- System.exit(0);
- }
- return newPol;
- }
- public static boolean validateReference(String ref)// SEPARATE METHOD FOR VALIDATING REFERENCE NUMBER DUE TO COMPLEXITY
- {
- boolean validated = false;
- if (ref.length() == 6) // is it 6 digits long?
- {
- if (ref.charAt(0) >= 'A' && ref.charAt(0) <= 'Z') // is character a letter between A and Z?
- {
- if (ref.charAt(1) >= 'A' && ref.charAt(1) <= 'Z') {
- if (ref.charAt(2) >= '0' && ref.charAt(2) <= '9') // is it a number?
- {
- if (ref.charAt(3) >= '0' && ref.charAt(3) <= '9') {
- if (ref.charAt(4) >= '0' && ref.charAt(4) <= '9') {
- if (ref.charAt(5) >= 'A' && ref.charAt(5) <= 'Z') {
- validated = true;
- }
- }
- }
- }
- }
- }
- }
- if (validated == false) {
- System.out.println("Reference number not recognised format - try again");
- }
- return validated;
- }
- public static String getDate() {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
- return sdf.format(cal.getTime());
- }
- public static String[] readFile(File Read) throws FileNotFoundException {
- int count = 0;
- Scanner countFile = new Scanner(Read);
- while (countFile.hasNext()) {
- countFile.nextLine();
- count++;
- }
- String[] lines = new String[count];
- Scanner readFile = new Scanner(Read);
- for (int i = 0; i < count; i++) {
- lines[i] = readFile.nextLine();
- }
- return lines;
- }
- public static Policy[] Pol(File Read) throws IOException {
- Policy[] policies = new Policy[readFile(Read).length];
- for (int i = 0; i < readFile(Read).length; i++) {
- String line = readFile(Read)[i];
- Scanner lineScanner = new Scanner(line);
- policies[i] = new Policy();
- policies[i].setPolicyDate(lineScanner.next());
- policies[i].setReference(lineScanner.next());
- policies[i].setNumberOfGadgets(lineScanner.nextInt());
- policies[i].setHighestGadgetValue(lineScanner.nextInt());
- policies[i].setExcess(lineScanner.nextInt());
- policies[i].setPremium(lineScanner.nextInt());
- policies[i].setPaymentTerms(lineScanner.next());
- policies[i].setClientName(lineScanner.next());
- }
- return null;
- }
- public static void displaySummary() throws FileNotFoundException { //option 2
- File a = new File("Policy.txt");
- int totalPolicies = 0;
- int numberOfItems = 0;
- int numberPerPolicy = 0;
- double avgPremium = 0.0;
- int RejectedTotal =0;
- int AverageItems =0;
- int finishedTotal = 0;
- String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- int month1 = 0;
- int month2 = 0;
- int month3 = 0;
- int month4 = 0;
- int month5 = 0;
- int month6 = 0;
- int month7 = 0;
- int month8 = 0;
- int month9 = 0;
- int month10 = 0;
- int month11 = 0;
- int month12 = 0;
- int totalNumberOfLines = 0;
- Scanner keyboardString = new Scanner(a);
- int countMonthlyPolicys = 0;
- int notMonthlyPolicys = 0;
- int totalAvgPremium = 0;
- int annualItems = 0;
- int totalMonthlyItems = 0;
- // Value Holders
- char avgCharHolder;
- int setIntToCharHolder;
- int acceptedPolicyCount = 0;
- int calculateItems = 0;
- while (keyboardString.hasNextLine()){
- totalNumberOfLines++;
- String premiumLine = keyboardString.nextLine(); // SETS EVERYTHING TO ONE LINE
- avgCharHolder = premiumLine.charAt(19);
- setIntToCharHolder = Character.getNumericValue(avgCharHolder);
- // String getDate = keyboardString.next();
- // String ref = keyboardString.next();
- // numberOfItems = keyboardString.nextInt();
- // String mostExpensive = keyboardString.next();
- // String excess = keyboardString.next();
- // finishedTotal += keyboardString.nextInt();
- // String terms1 = keyboardString.next();
- // String name = keyboardString.nextLine();
- // locate last tab
- int findTerms = premiumLine.lastIndexOf("\t");
- String theTerms = premiumLine.substring(findTerms -1, findTerms);
- switch(theTerms) {
- case "A":
- notMonthlyPolicys++;
- case "R":
- break;
- case "M":
- int findPrem = premiumLine.indexOf("\t", 26); // find the first \t in the line
- String declarePrem = premiumLine.substring(findPrem + 1, findTerms - 2); // locating the premium cost
- countMonthlyPolicys++;
- totalAvgPremium = totalAvgPremium + Integer.parseInt(declarePrem);
- break;
- }
- avgCharHolder = premiumLine.charAt(19);
- setIntToCharHolder = Character.getNumericValue(avgCharHolder);
- if (avgCharHolder > '0' && avgCharHolder < '6') {
- //System.out.println(avgCharHolder);
- setIntToCharHolder = Character.getNumericValue(avgCharHolder);
- acceptedPolicyCount++;
- calculateItems = calculateItems + setIntToCharHolder;
- }
- switch(premiumLine.substring(3, 6)) { // LOCATES pos 3 and pos 6 of the String
- case "Jan":
- month1++;
- break;
- case "Feb":
- month2++;
- break;
- case "Mar":
- month3++;
- break;
- case "Apr":
- month4++;
- break;
- case "May":
- month5++;
- break;
- case "Jun":
- month6++;
- break;
- case "Jul":
- month7++;
- break;
- case "Aug":
- month8++;
- break;
- case "Sep":
- month9++;
- break;
- case "Oct":
- month10++;
- break;
- case "Nov":
- month11++;
- break;
- case "Dec":
- month12++;
- break;
- }
- }
- System.out.println("Monthly: "+ totalMonthlyItems);
- System.out.println("---------------------------------------------");
- System.out.println("Total Number of policies:" + totalNumberOfLines); //998
- System.out.println("Average number of items (Accepted policies):" + calculateItems / acceptedPolicyCount);
- System.out.printf("Average Monthly Premium: £%.2f ", totalAvgPremium / 100.00);//change this to a double
- System.out.println("");
- System.out.println("Number of Policies per Month (inc. non-accepted): ");
- System.out.println(months[0] + "\t" + months[1] + "\t" + months[2] + "\t" + months[3] + "\t" +
- months[4] + "\t" + months[5] + "\t" + months[6] + "\t" +
- months[7] + "\t" + months[8] + "\t" + months[9] + "\t" + months[10] + "\t" + months[11] + "\t");
- System.out.println(month1 + "\t" + month2 + "\t" + month3 + "\t" + month4 + "\t" + month5 + "\t" + month6 + "\t" +
- month7 + "\t" + month8 + "\t" + month9 + "\t" + month10 + "\t" + month11 + "\t" + month12 + "\t");
- System.out.println("\n---------------------------------------------");
- System.out.println("\nWould you like to re-start the program?.");
- System.out.println("Press 1 for Restart and 2 to End: ");
- Scanner KeyboardInput = new Scanner(System.in);
- b = KeyboardInput.nextInt();
- while (b != 1 && b != 2) {
- System.out.println("\nPress 1 for Restart and 2 to End: ");
- }
- if (b == 1) {
- returnMenu();
- } else {
- System.out.println("\n--------------------------------------------------------");
- System.out.println("Thank you for using Policy Manager, you have selected End.");
- System.out.println("\n--------------------------------------------------------");
- }
- System.exit(0);
- }
- public static void displayMonth() { //option 3
- String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
- Scanner keyboardString = new Scanner(System.in);
- System.out.println("---------------------------------------------");
- System.out.println("Please enter the name of the file you wish to search?");
- String filename = keyboardString.next();
- while (filename.equals("Policy.txt")) {
- File f = new File(filename);
- if (!f.exists())
- System.out.println("Error: This file does not Exist");
- System.out.println("---------------------------------------------");
- System.out.println("Please enter specific month: ");
- System.out.println("---------------------------------------------");
- System.out.println("Total Number of policies:");
- System.out.println("Average number of items (Accepted policies):");
- System.out.println("Average Monthly Premium:");
- System.out.println("Number of Policies for specific month (inc. non-accepted): ");
- System.out.println("---------------------------------------------");
- System.out.println("\nWould you like to re-start the program?.");
- System.out.println("Press 1 for Restart and 2 to End: ");
- b = keyboardString.nextInt();
- while (b != 1 && b != 2) {
- System.out.println("\nPress 1 for Restart and 2 to End: ");
- }
- if (b == 1) {
- returnMenu();
- } else {
- System.out.println("Thank you for using Policy Manager, you have selected End.");
- }
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement