I created a class object array and but now when I try to print it using printf method then it doesn't print those values, I tried checking if the values are correct using println and it works fine with it. One more thing which was weird is that when I pass the end index of the array then it prints them. for example class object array is of size 20 (0-19), when I pass 19th index to the method which prints it then it is working but for 0-18 it is not working.
package com.insurance.company;
import java.util.List;
/**
*
* @author Lenovo
*/
public class InsuranceCompany {
private String companyName;
private String telephone;
private String webAddress;
private double brokerPercentage;
private String description;
private List<String> insuranceTypes;
public InsuranceCompany(){}
public InsuranceCompany(String companyName, String telephone, String webAddress, double brokerPercentage, String description, List<String> insuranceTypes) {
this.companyName = companyName;
this.telephone = telephone;
this.webAddress = webAddress;
this.brokerPercentage = brokerPercentage;
this.description = description;
this.insuranceTypes = insuranceTypes;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(companyName);
sb.append(" " + telephone);
sb.append(" " + webAddress);
for(int i = 0; i < insuranceTypes.size(); i++){
sb.append(" " + insuranceTypes.get(i));
}
sb.append(" " + brokerPercentage);
sb.append(" " + description);
return sb.toString();
}
public String getCompanyName() {
return companyName;
}
public String getTelephone() {
return telephone;
}
public String getWebAddress() {
return webAddress;
}
public double getBrokerPercentage() {
return brokerPercentage;
}
public String getDescription() {
return description;
}
public List<String> getInsuranceTypes() {
return insuranceTypes;
}
}
package com.insurance.company;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class InsurancePortal {
//function to load data from file
private static String loadDataFromFile(String path)throws Exception{
String data;
data = new String(Files.readAllBytes(Paths.get(path)));
return data;
}
//function to list all insurance companies
private static void listInuranceCompanies(InsuranceCompany[] company){
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------");
System.out.printf("| %3s | %-20s | %-111s |%n", "ID", "Company Name", "Cover Types");
//System.out.flush();
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------");
for(int j = 0; j < company.length; j++){
System.out.printf("| %3d | %-20s |", j+1, company[j].getCompanyName());
for(int i = 0; i < 7; i++){
if(i < company[j].getInsuranceTypes().size()){
System.out.printf(" %15S", company[j].getInsuranceTypes().get(i));
}else{
System.out.printf(" %15s", " ");
}
}
System.out.print(" |\n");
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------");
}
}
//function to search cover
public static void listSingleCompanyDetails(InsuranceCompany selectedCompany){
System.out.println(selectedCompany.getCompanyName()+"\n--------------------------------------------------");
//System.out.flush();
System.out.printf("| %-18S | %-28S |%n", selectedCompany.getCompanyName(), selectedCompany.getDescription());
//System.out.flush();
System.out.println("--------------------------------------------------");
System.out.println("--------------------------------------------------");
System.out.println("--------------------------------------------------");
System.out.println("--------------------------------------------------");
System.out.println("--------------------------------------------------");
}
public static void main(String arge[]) throws Exception{
String dataFile = System.getProperty("user.dir") + File.separator + "insurance-data.txt";
String[] data = loadDataFromFile(dataFile).split("\n");
/*for(String str: data){
System.out.println(str);
}*/
int rows = data.length;
InsuranceCompany[] company = new InsuranceCompany[rows];
//Initialising objects for every row of data in the file
for(int j = 0; j < rows; j++){
String row = data[j];
//System.out.println(row);
String companyName, telephone, webAddress, description;
double brokerPercentage;
List<String> insuranceTypes = new ArrayList<>();
//Splitting for Company Name
String[] splittedArray = row.split(":", 2);
companyName = splittedArray[0];
row = splittedArray[1];
//Splitting for telephone
splittedArray = row.split(":", 2);
telephone = splittedArray[0];
row = splittedArray[1];
//splitting for web address
splittedArray = row.split(":", 2);
webAddress = splittedArray[0];
row = splittedArray[1];
//splitiing for insurance types
splittedArray = row.split(":", 2);
String[] insuranceTypeList = splittedArray[0].split(",");
for(String str : insuranceTypeList){
insuranceTypes.add(str);
}
row = splittedArray[1];
//splitting for broker percentage
splittedArray = row.split(":", 2);
brokerPercentage = Double.parseDouble(splittedArray[0]);
row = splittedArray[1];
//splitting for description
splittedArray = row.split(":", 2);
description = splittedArray[0];
//System.out.println(companyName+ " " + telephone + " " +webAddress + " " + brokerPercentage + " " + description);
/*for(int i = 0; i < insuranceTypes.size(); i++){
System.out.print(insuranceTypes.get(i)+" ");
}*/
company[j] = new InsuranceCompany(companyName, telephone, webAddress, brokerPercentage, description, insuranceTypes);
//System.out.println(company[j].getCompanyName());
}
boolean run = true;
while(run){
int choice;
Scanner sc = new Scanner(System.in);
System.out.print("\nList insurance companies.......1\n"
+ "Select insurance company.......2\n"
+ "Search cover...................3\n"
+ "Exit...........................0\n"
+ "\nEnter choice:> ");
choice = sc.nextInt();
switch(choice){
case 1 :
listInuranceCompanies(company);
break;
case 2 :
System.out.print("Enter company ID from list [1 - "+(company.length)+"] :> ");
int selectedCompany;
selectedCompany = sc.nextInt();
listSingleCompanyDetails(company[selectedCompany-1]);
break;
case 0 :
System.exit(0);
break;
default: System.out.println("Invalid choice!, please try again.");
}
}
}
}