JAVA Streams and Writers:

Hall details

 

Write a program to get the hall details in CSV format in the console and store them as the list of objects. Then write this list into the file "output.txt".

Sample Input:
[All text in bold corresponds to input and rest corresponds to output]

Enter the number of halls:
3
Party hall,9876543210,4000.0,Jarviz
Disco hall,9876543201,5000.0,Starc
Dining hall,9873216540,3000.0,Chris


CODE:

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class Hall {
    private String name;
    private String contact;
    private Double costPerDay;
    private String owner;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public Double getCostPerDay() {
return costPerDay;
}
public void setCostPerDay(Double costPerDay) {
this.costPerDay = costPerDay;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public Hall(String name, String contact, Double costPerDay, String owner) {
super();
this.name = name;
this.contact = contact;
this.costPerDay = costPerDay;
this.owner = owner;
}
public static void writeHallDetails(List<Hall> halls) throws IOException{
FileWriter fi = new FileWriter("hall.csv");
try {
for(int i=0; i<halls.size(); i++) {
String str = halls.get(i).getName()+","+halls.get(i).getContact()+","+halls.get(i).getCostPerDay()+","+halls.get(i).getOwner()+"\n";
fi.write(str);
}
}catch(Exception e) {
System.out.println(e);
}
fi.close();
}
}


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of halls:");
int num = Integer.parseInt(sc.nextLine());
List<Hall> list = new ArrayList<Hall>();
for(int i=0; i<num; i++) {
String rec = sc.nextLine();
String val[]=rec.split(",");
Hall hall = new Hall(val[0],val[1],Double.parseDouble(val[2]),val[3]);
list.add(hall);
hall=null;
rec=null;
val=null;
}
Hall.writeHallDetails(list);
}
}

Comments

Popular posts from this blog