Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //A program to calculate a simple premium rate for a gadget protection company.
- package policy.mannager;
- 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 PolicyMannager {
- //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();
- }
- 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
- }// end of main 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(getExpensive());
- newPolicy.setExcess(getExcess());
- newPolicy.setTerm(getTerms());
- newPolicy.setDate();
- newPolicy.setPremium();
- //calls the method that allows the system to write to a file
- writeToFile(newPolicy.getDate(), newPolicy.getNumber(), newPolicy.getGadget(),newPolicy.getExpensive(), newPolicy.getExcess(), newPolicy.getTerms());
- //Calls the method that displays the policy method
- displayPolicy(newPolicy.getDate(), newPolicy.getNumber(), newPolicy.getGadget(),newPolicy.getExpensive(), newPolicy.getExcess(), newPolicy.getTerms());
- //returns the client back to the menu
- backToMenu();
- }//end of new policy string
- public 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
- public static String getDate()
- //gets the date
- {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
- return sdf.format(cal.getTime());
- }
- public 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
- public 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
- public static double getExpensive()
- //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 newPolicy.getExpensive();
- int gadgetLimit;
- newPolicy.getExpensive() = keyboard.nextDouble();
- //ensures the value isn't below 0
- while (getExpensive() <= 0)
- {
- System.out.println("A negative value has been entered.");
- System.out.print("Please enter a positive value : ");
- getExpensive() = keyboard.nextDouble();
- }
- //ensures the cost doesn't exceed £1000.00
- while (getExpensive() > 1000.00)
- {
- System.out.println("Please enter a value below £1000");
- getExpensive() = keyboard.nextDouble();
- }
- //code to asign the correct limit to the value entered
- if (getExpensive() <= 1000 && getExpensive() > 800)
- {
- gadgetLimit = 1000;
- }
- else if (getExpensive() <= 800 && getExpensive() > 550)
- {
- gadgetLimit = 800;
- }
- else if ( getExpensive() <=550 && getExpensive() >0)
- {
- gadgetLimit = 550;
- }
- else
- {
- gadgetLimit = (int)getExpensive();
- }
- return getExpensive();
- }//end of getExpensive string
- public static double getExcess()
- //gets the amount of newPolicy.getExcess() the client would like to pay
- {
- System.out.println("newPolicy.getExcess() must be between £30.00 and £70.00");
- System.out.println("Please enter how much newPolicy.getExcess() you would like to pay (e.g. £30.00) : ");
- double excess = keyboard.nextDouble();
- //validation to ensure that the newPolicy.getExcess() doesn't go below or above the boundaries
- while (newPolicy.getExcess() <30.00 && newPolicy.getExcess() >70.00)
- {
- System.out.println("Please enter a value above £30.00 and below or equal to £70.00");
- newPolicy.getExcess() = keyboard.nextDouble();
- }
- return newPolicy.getExcess();
- }//end of getnewPolicy.getExcess() 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;
- }
- }
- static void readFromFile(String file, int option)
- {
- //variables that are used to read the file into the computer
- int total = 0;
- int monthTotal = 0;
- int jan = 0;
- int feb = 0;
- int mar = 0;
- int apr = 0;
- int may = 0;
- int jun = 0;
- int jul = 0;
- int aug = 0;
- int sep = 0;
- int oct = 0;
- int nov = 0;
- int dec = 0;
- String month = " ";
- String search = " ";
- //sees which option the client is coming from
- if (option == 2)
- {
- System.out.println("Please enter the abreviated foorm of the month you want e.g. Jul");
- month = keyboard.next().toLowerCase();
- 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")));
- {
- System.out.println("Invalid entry please try again");
- month = keyboard.next().toLowerCase();
- }
- }// end of if
- else if (option == 3)
- {
- System.out.println("Please enter search");
- search = keyboard.next().toLowerCase();
- }//end of else if
- try
- {
- File currentFile = new File (file);
- if (currentFile.exists())
- {
- while (lnr.readLine() !=null)
- {
- total++;
- }
- }
- else
- {
- System.out.println("File does not exist");
- }
- }
- } catch (IOExeption e) {}
- Scanner input = null;
- //String to assign the word to the number of gadgets being bought
- String number;
- if (newPolicy.getGadget() == 1)
- {
- number = "One";
- }
- else if (newPolicy.getGadget() == 2)
- {
- number = "Two";
- }
- else if (newPolicy.getGadget() == 3)
- {
- number = "Three";
- }
- else if (newPolicy.getGadget() == 4)
- {
- number = "Four";
- }
- else if (newPolicy.getGadget() == 5)
- {
- number = "Five";
- }
- else
- {
- number = 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:", number, "|");
- 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 (getExpensive() > 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:" , getExpensive() + " |");
- }
- System.out.println(" | |");
- System.out.println(" +=============================================+");
- }//end of create policy string
- }//end of class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement