Having successfully created a policy holder and writing the information to csv I try to run the program again using a do while loop but receive an inputmismatchexception after typing surname.
Can someone point me in the right direction or advise what I am doing wrong with this piece of code?
Here is example of what happens... console
/**
*
*/
package InsurancePolicySystem;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Scanner;
import java.util.stream.Collectors;
/**
* @author
*
*/
public class InsurancePolicy {
// create variables
private String surname;
private int age;
private String motorType;
private String motorPolicyId;
private String addAnother;
private String pattern = "^[a-zA-Z](\\s?[a-zA-Z]){2,29}$";
// import scanner for user input
Scanner scanner = new Scanner(System.in);
// time stamp number. -1 used on month as time stamp displays next year instead
// of current year
int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH) - 1;
// sum of the year and month
int timeStamp = year + month;
/**
* default constructor
*/
public InsurancePolicy() {
}
/**
* non default constructor
*
* @param surname
* @param age
* @param motorType
* @param motorPolicyId
*/
public InsurancePolicy(String surname, int age, String motorType, String motorPolicyId) {
this.surname = surname;
this.age = age;
this.motorType = motorType;
this.motorPolicyId = motorPolicyId;
}
/**
*
* @return
*/
public String getSurname() {
return surname;
}
/**
* Surname should be more than 3 letters and not greater than 20. Anything more
* will cause a not valid error!
*
* @param surname
* @throws IllegalArgumentException
*/
public void setSurname(String surname) throws IllegalArgumentException {
this.surname = surname;
}
/**
*
* @return
*/
public int getAge() {
return age;
}
/**
* Age must be between 18 & 50 Any other age will produce not eligible error!
*
* @param age
* @throws IllegalArgumentException
*/
public void setAge(int age) throws IllegalArgumentException {
if ((age >= 18) && (age <= 50)) {
this.age = age;
} else {
throw new IllegalArgumentException("Not eligible for an insurance policy!");
}
}
/**
*
* @return
*/
public String getMotorType() {
return motorType;
}
/**
* motor type should only be either CAR, TAXI or BUS. if statement to prevent
* other vehicle types and throw error if user inputs a different vehicle.
*
* @param motorType
* @throws IllegalArgumentException
*/
public void setMotorType(String motorType) throws IllegalArgumentException {
if (motorType.equals("CAR") || motorType.equals("TAXI") || motorType.equals("BUS")) {
this.motorType = motorType;
} else {
throw new IllegalArgumentException("Not eligible for an insurance policy!");
}
this.motorType = motorType;
}
/**
*
* @return
*/
public String getMotorPolicyId() {
return motorPolicyId;
}
/**
*
* @param motorPolicyId
*/
public void setMotorPolicyId(String motorPolicyId) {
this.motorPolicyId = motorPolicyId;
}
/**
* Method to write policy holder particulars to a csv file. Creates ArrayList
* and uses collect joining to add comma for file.
*
* @throws IOException
*/
public void writeToFile() throws IOException {
BufferedWriter writer = new BufferedWriter(
new FileWriter("/Users/johnj/eclipse-workspace/InsuranceProject/insurance.csv", true));
// creates policy holder (insurance) array
ArrayList<String> insurance = new ArrayList<String>();
insurance.add(surname);
insurance.add(Integer.toString(age));
insurance.add(motorType);
insurance.add(motorPolicyId);
// write array to csv file
String collect = insurance.stream().collect(Collectors.joining(", "));
System.out.println(collect);
writer.write(collect);
writer.newLine();
writer.flush();
writer.close();
}
/**
* Builds and generates user input. Will then add it into an array list and
* append to writeToFile(). Included do while loop for user to continue creating
* policy profiles without having to re run program.
*
* @param motorType
*/
public void addPolicyHolder() throws IOException {
try {
do {
// enter policy holder surname. Checks pattern to ensure compliance
System.out.println("Please type your surname...");
surname = scanner.next(pattern).toUpperCase().trim();
setSurname(this.surname);
// user input age
System.out.println("Please type your age...");
age = scanner.nextInt();
setAge(this.age);
// user input motor type which is converted to upper case
System.out.println("Please type your motor type i.e. CAR, TAXI or BUS...");
motorType = scanner.next().toUpperCase().trim();
setMotorType(this.motorType);
System.out.println(); // used to create next line space
// Motor Policy Id should be 3 characters from surname as well as time stamp. If
// time stamp odd number add 1 or else add 0 if even.
motorPolicyId = surname.substring(0, 3) + timeStamp;
if (timeStamp % 2 == 0) {
motorPolicyId = this.motorPolicyId + 0;
} else {
motorPolicyId = this.motorPolicyId + 1;
}
// creates csv file
writeToFile();
// prints completed user input of policy holder
System.out.println();
System.out.println("Surname :" + this.surname);
System.out.println("Age :" + this.age);
System.out.println("Policy Ref :" + this.motorPolicyId);
System.out.println("Motor Type: :" + this.motorType);
System.out.println(); // used to create next line space
// asks user if they would like to create a different policy holder
System.out.println("Add another Policy Holder (Y or N)..");
addAnother = scanner.next();
} while (addAnother.equalsIgnoreCase("Y")); // If N program ends.
} catch (Exception exception) { // if exception program ends
System.out.println(
"There is a problem ... Policy Holder does meet necessary criteria, run the program again.");
} finally {
scanner.close();
System.out.println("Program ended!"); // end of program
}
}
public static void main(String[] args) throws IOException {
InsurancePolicy insurancePolicy = new InsurancePolicy();
insurancePolicy.addPolicyHolder();
}
}
I am able to complete a first run but do while loop fails when trying to add another policy holder.