JAVA Streams and Writers:

FIXED LENGTH FORMAT

In Fixed Length Format files, each field has a fixed length. If a variable of the field is shorter than its size, it is filled with space so that all variables of a particular field is of the same size. 

Write a program to read event details from the fixed-length format file and display the events organized by specific persons.

The sizes of fields in the fixed-length file are,
Field
Size
name
0-19
details
19-39
type
39-51
organizer
51-61
attendeesCount
61-67
projectedExpense
67-74

Sample Input and Output 1:

Enter the name of the person whose events to be shown:
Jane
Name           Detail              Type           Attendees CountProjected Expense
Magic Show     Magicwithoutlogicstageshow      1000           10000.0        
Marathon       Run for a cause    sportsmeet
     500            25000.0        
Do you want to continue?(y/n)
y
Enter the name of the person whose events to be shown:
Jack
Name           Detail              Type           Attendees CountProjected Expense
Book Fair      @5% discount        exhibition     5000           15000.0        
Do you want to continue?(y/n)
y
Enter the name of the person whose events to be shown:
Jim
The given person has no upcoming events
Do you want to continue?(y/n)
n

CODE:

public class Event {
String name, detail, type,organiser;
int attendeesCount;
double projectedExpense;
public Event(String name, String detail,String type,String organiser,int attendeesCount, double projectedExpense)
{
super();
this.name = name;
this.detail = detail;
this.type = type;
this.organiser = organiser;
this.attendeesCount = attendeesCount;
this.projectedExpense = projectedExpense;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getOrganiser() {
return organiser;
}
public void setOrganiser(String organiser) {
this.organiser = organiser;
}
public int getAttendeesCount() {
return attendeesCount;
}
public void setAttendeesCount(int attendeesCount) {
this.attendeesCount = attendeesCount;
}
public double getProjectedExpense() {
return projectedExpense;
}
public void setProjectedExpense(double projectedExpense) {
this.projectedExpense = projectedExpense;
}
@Override
public String toString()
{
return String.format("%-15s%-20s%-15s%-15s%-15s\n",this.getName(),this.getDetail(),this.type,this.attendeesCount,this.projectedExpense);
}
}

import java.util.*;
import java.io.*;
public class EventBO {
public static ArrayList<Event> readFile(BufferedReader br) throws Exception {
ArrayList<Event> e = new ArrayList<Event>();
Event event;
String line;
while((line=br.readLine())!=null)
{
String name = line.substring(0,19).trim();
String detail = line.substring(19,39).trim();
String type = line.substring(39,51).trim();
String organiser = line.substring(51,61).trim();
String attendeesCount = line.substring(61,67).trim();
String projectedExpense = line.substring(67,74).trim();
event = new Event(name,detail,type,organiser,Integer.parseInt(attendeesCount),Double.parseDouble(projectedExpense));
e.add(event);
}
br.close();
return e;
}
public static ArrayList<Event> eventsByPerson(ArrayList<Event> eventList,String organiser) {
ArrayList<Event>e = new ArrayList<Event>();
for(int i =0; i<eventList.size();i++)
{
if(eventList.get(i).getOrganiser().equals(organiser))
{
e.add(eventList.get(i));
}
}
return e;
}
}

import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws Exception{
Scanner sc = new Scanner(System.in);
String ch = null;
BufferedReader br = new BufferedReader(new FileReader("input.csv"));
ArrayList<Event> e = EventBO.readFile(br);
do
{
System.out.println("Enter the name of the person whose events to be shown:");
String s = sc.nextLine();
ArrayList<Event> e1 = EventBO.eventsByPerson(e,s);
if(e1.isEmpty())
{
System.out.println("The given person has no upcoming events");
}
else {
String name="Name",detail="Detail",type="Type",attendees="Attendees Count",cpe="Projected Expense";
System.out.printf("%-15s%-20s%-15s%-15s%-15s\n",name,detail,type,attendees,cpe);
for(int i =0; i<e1.size();i++)
{
System.out.println(e1.get(i).toString());
}
}
System.out.println("Do you want to continue?(y/n)");
ch = sc.nextLine();
}
while(ch.equals("y"));
}
}


 

Comments

Popular posts from this blog