JAVA Streams and Writers:

ITEMTYPE DETAILS IN FILE

The length and position of attributes are as follows.
 

AttributeLengthPosition
name151
deposit716
costPerDay522

Input format:

Read the details from the input file "input.txt"

Output format:


Use "%-15s %-15s %s\n" for displaying ItemType details in tabular form in the console
Print one digit after the decimal for the double datatype.
Print the statement for the heading of the details is present in the main method.
Refer to the sample output for the formatting specifications.

Sample Input:



Sample Output :

Item type       Deposit         Cost per day
Electrical      2100.0          500.0
Construction    5000.0          1000.0

CODE:


public class ItemType {
     String name;
     double deposit;
     double costPerDay;
     
     ItemType() {}
      
      public ItemType(String name, double deposit, double costPerDay) {
          this.name = name;
          this.deposit = deposit;
          this.costPerDay = costPerDay;
          
      }
      public String getName() {
          return name;
          
      }
      public void setName(String name) {
          this.name = name;
          
      }
      public double getDeposit() {
          return deposit;
          
      }
      public void setDeposit(double deposit) {
          this.deposit = deposit;
          
      }
      public double getCostPerDay() {
          return costPerDay;
      }
}


import java.util.*;
import java.io.*;


public class ItemTypeBO {
     public List<ItemType> readFromFile(BufferedReader br) throws Exception{
         List<ItemType> ItemType1 = new ArrayList<ItemType>();
         String line=new String();
         while((line=br.readLine())!=null) {
             //any number of spaces
        String val[]=line.split("\\s+");
        //System.out.println("hi");
        ItemType it = new ItemType(val[0],Double.parseDouble(val[1]),Double.parseDouble(val[2]));
        // System.out.println("hi");
        ItemType1.add(it);
        val=null;
        it=null;
        line=null;
         }
           return ItemType1;
           
      } 
      public List<ItemType> depositList(List<ItemType> list) {
          List<ItemType> ItemType2 = new ArrayList<ItemType>();
          for(int i=0; i<list.size(); i++) {
          if((list.get(i).getDeposit())>2000) {
          ItemType it = new ItemType(list.get(i).getName(),list.get(i).getDeposit(),list.get(i).getCostPerDay());
          ItemType2.add(it);
          
          }
          }
          return ItemType2;
          
      }
      public void display(List<ItemType> list) {
      if(list.size()==0) {
      System.out.println("No item type has deposit more than 2000");
      }else {
      System.out.printf("%-15s %-15s %s\n","Item type","Deposit","Cost per day");
          for(ItemType it:list) {
          System.out.printf("%-15s %-15s %s\n",it.getName(),it.getDeposit(),it.getCostPerDay());
          }
      }
      
      }
}
      

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception{
try {
FileReader fr = new FileReader("input.txt");
BufferedReader br = new BufferedReader(fr);
ItemTypeBO it = new ItemTypeBO();
List<ItemType> itemType=it.readFromFile(br);
List<ItemType> IT = it.depositList(itemType);
it.display(IT);
}catch(Exception e) {
System.out.println(e);
}
}

}

Comments

Popular posts from this blog