Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package policymanager;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.LineNumberReader;
- import java.io.FileReader;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- /**
- *
- * @author s6085604
- */
- public class PolicyManager {
- //holds the main scanner allowing input from the client
- public static Scanner keyboard = new Scanner(System.in);
- public static void main(String[] args) {
- //calls the main menu
- displayMenu();
- }//end of main
- static void displayMenu()
- { //Shows the main menu which the client has to choose from
- String mainMenu;
- System.out.println("Please choose an option from below");
- System.out.println(" ");
- System.out.println("1. Enter new policy");
- System.out.println("2. Display summary of policies");
- System.out.println("3. Display summary of policies for selected month");
- System.out.println("4. Find and display policy");
- System.out.println("0. Exit");
- //takes the input from the client
- mainMenu = keyboard.nextLine();
- //validates the clients choice ensuring it's valid
- while (!(mainMenu.matches("[0-4]{1}")))
- {
- System.out.println("Invalid option, please try again: ");
- mainMenu = keyboard.nextLine();
- }
- switch(mainMenu)
- {
- case "1":
- newPolicy();
- break;
- case "2":
- fileSummary();
- break;
- case "3":
- monthSummary();
- break;
- case "4":
- searchFile();
- break;
- case "0":
- System.out.println("Exiting...");
- break;
- }//end of switch
- }//emd of displau menu
- static void newPolicy()
- {
- //holds the methods that call the getters and setters in the systems, allowing the attributes to be set for them
- Policy newPolicy = new Policy();
- newPolicy.setName(getName());
- newPolicy.setRefNum(getNumber());
- newPolicy.setGadgetNumber(getGadget());
- newPolicy.setGadgetValue(getGadgetValue());
- newPolicy.setExcess(getExcess());
- newPolicy.setTerm(getTerms());
- newPolicy.setDate();
- newPolicy.setPremium();
- String gadgetNumber = " ";
- if (newPolicy.getGadget() == 1)
- {
- gadgetNumber = "One";
- }
- else if (newPolicy.getGadget() == 2)
- {
- gadgetNumber = "Two";
- }
- else if (newPolicy.getGadget() == 3)
- {
- gadgetNumber = "Three";
- }
- else if (newPolicy.getGadget() == 4)
- {
- gadgetNumber = "Four";
- }
- else if (newPolicy.getGadget() == 5)
- {
- gadgetNumber = "Five";
- }
- else
- {
- gadgetNumber = Integer.toString(newPolicy.getGadget());
- }
- //code to show the output box
- System.out.println(" ");
- System.out.println(" +=============================================+");
- System.out.println(" | |");
- System.out.printf (" |" + "%10s %-20s %14s %n", "Client: " ,newPolicy.getName(), " " + "|");
- System.out.println(" | |");
- System.out.printf (" |" + "%10s %-20s %5s %-25s %n", "Date", newPolicy.getDate(), "Ref", newPolicy.getNumber() + " |");
- System.out.printf (" |" + "%10s %-19s %6s %-5s %2s %n", "Terms: ", newPolicy.getTerms(), "Items:", newPolicy.getGadget(), "|");
- System.out.printf (" |" + "%10s £%-20.2f %13s %n", "newPolicy.getExcess(): ", newPolicy.getExcess(), " " + "|" );
- System.out.println(" | |");
- //code to display any needed error messages in the clients inputs
- if (newPolicy.getGadget() > 5)
- {
- System.out.println(" | Your payment has been rejected due to the |");
- System.out.println(" | number of gadgets exceeding the limit |");
- }
- else if (newPolicy.getGadgetValue() > 1000)
- {
- System.out.println(" | Your payment has been rejected due to the |");
- System.out.println(" | value of gadgets exceeding the limit |");
- }
- else
- {
- System.out.printf(" |" + "%9s %-16s %-10s %8s %n", "Annual ","", "Limit per" , " |");
- System.out.printf(" |" + "%10s £%-15.2f %1s %2s %8s %n", "Premium: " , newPolicy.getPremium() ,"", "Gadget:" , newPolicy.getGadgetValue() + " |");
- }
- System.out.println(" | |");
- System.out.println(" +=============================================+");
- }//end of new policy
- static String getName()
- //gets the name from the client
- {
- System.out.println("Please enter your name (e.g. John Smith) : "); //20 character maximum
- String name = keyboard.nextLine();
- //validates and ensures the name doesn't exceed 20 characters
- while (name.length() <1 || name.length() >20)
- {
- System.out.println("Name must be between 1 and 20 chars long");
- System.out.println("Name inputted is too long. Please re-enter staff name (e.g. John Smith): ");
- name = keyboard.nextLine();
- }
- return name;
- }//end of getName string
- static String getNumber()
- //gets the customer reference number from the client
- {
- System.out.println("Please enter your customer reference number (e.g. AB123C) : "); //validate
- String customerNum = keyboard.nextLine().toUpperCase();
- //checks that the customer reference number is of the correct length and formatted in the correct way
- boolean valid = false;
- while (!valid)
- {
- valid = true;
- if (customerNum.length() != 6)
- {
- System.out.println("Customer reference number should be 6 charaters long (e.g. AB123C)");
- valid = false;
- }
- else if (!Character.isLetter(customerNum.charAt(0)) || !Character.isLetter(customerNum.charAt(1)) ||
- !Character.isDigit(customerNum.charAt(2)) || !Character.isDigit(customerNum.charAt(3)) ||
- !Character.isDigit(customerNum.charAt(4)) || !Character.isLetter(customerNum.charAt(5)))
- {
- System.out.println("Customer reference number should be 2 letters followed by 3 digits, followed by a single letter (e.g. AB123C)" );
- valid = false;
- }
- if (!valid)
- {
- System.out.print("Please enter customer reference number (e.g. AB123C) : ");
- customerNum = keyboard.nextLine();
- }
- }
- return customerNum;
- }//end of getNumber string
- static int getGadget()
- //gets the total number of gadgets that the client is buying
- {
- String quantity;
- System.out.println("Please enter your total number of gadgets : ");
- quantity = keyboard.next();
- while(!(quantity.matches(".*[0-9].*")))
- {
- System.out.println("You have inputted an invalled data type");
- System.out.println("Please try again");
- }
- //validates and ensures that only positive amounts of gadgets can be bought and that the client can't miss the feild
- while (Integer.parseInt(quantity) <1)
- {
- System.out.println("Please enter your total number of gadgets : ");
- quantity = keyboard.next();
- }
- return Integer.parseInt(quantity);
- }//end of getGadget() string
- static double getGadgetValue()
- //gets the total of the most expensive gadget the client has bought
- {
- System.out.println("Please enter the value of your most expensive gadget (e.g. £960) : ");
- double gadgetValue;
- int gadgetLimit;
- gadgetValue = keyboard.nextDouble();
- //ensures the value isn't below 0
- while (gadgetValue <= 0)
- {
- System.out.println("A negative value has been entered.");
- System.out.print("Please enter a positive value : ");
- gadgetValue= keyboard.nextDouble();
- }
- //ensures the cost doesn't exceed £1000.00
- while (gadgetValue > 1000.00)
- {
- System.out.println("Please enter a value below £1000");
- gadgetValue = keyboard.nextDouble();
- }
- return gadgetValue;
- }
- static double getExcess()
- //gets the amount of excess the client would like to pay
- {
- System.out.println("Excess must be between £30.00 and £70.00");
- System.out.println("Please enter how much excess you would like to pay (e.g. £30.00) : ");
- double excess = keyboard.nextDouble();
- //validation to ensure that the excess doesn't go below or above the boundaries
- while (excess <30.00 && excess > 70.00)
- {
- System.out.println("Please enter a value above £30.00 and below or equal to £70.00");
- excess = keyboard.nextDouble();
- }
- return excess;
- }//end of excess string
- static String getTerms()
- // gets the payment terms from the client
- {
- char payment;
- String term;
- System.out.print("Would you like to pay monthly (M) or annually (A) : ");
- payment = Character.toUpperCase(keyboard.next().charAt(0));
- //ensures the correct characters have been entered
- while (payment != 'A' && payment != 'M')
- {
- System.out.print("Invalid entry, please try again (A or M) : ");
- payment = Character.toUpperCase(keyboard.next().charAt(0));
- }
- //asigns the correct term to the input the client gave
- if (payment == 'A')
- {
- term = "Annual";
- }
- else
- {
- term = "Monthly";
- }
- return term;
- }//end of get terms string
- static void writeToFile(String date, String reference, int gadgetNum, double gadgetCost, double excessCost, double premium, String paymentType, String name)
- {
- PrintWriter output = null;
- File policy = new File("policy.txt");
- // checks to see if the file the client requested exists
- if ( ! policy.exists())
- {
- try {
- output = new PrintWriter(policy);
- } catch (FileNotFoundException e)
- {
- System.out.println("The file can not be created. Exiting...");
- System.exit(0); // this closes the system
- } catch (IOException ex)
- {
- System.out.println("The file can not be created. Exiting...");
- System.exit(0);
- }
- if (gadgetNum > 5 || gadgetCost > 1000)
- {
- premium = -1;
- paymentType = "r";
- }
- else
- {
- premium = premium * 100;
- }
- output.print(date + "/t");
- output.print(reference + "/t");
- output.print(gadgetNum + "/t");
- output.print(gadgetCost + "/t");
- output.print(excessCost + "/t");
- output.print(premium + "/t");
- output.print(paymentType + "/t");
- output.print(name + "/t");
- output.close();
- }//end of if statement
- }//end of write to file string
- }//end of class policy manager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement