ItemType details in a file

 ITEMTYPE.JAVA:

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 void setCostPerDay(double costPerDay) {
		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;
  	}
}




ITEMTYPEBO.JAVA:

import java.util.*;
import java.io.*;
public class ItemTypeBO {
public List<ItemType> readFromFile(BufferedReader br) throws Exception{
List<ItemType>itList = new ArrayList<ItemType>();
ItemType it;
try {
String line;
while((line=br.readLine())!=null) {
String sp[] = line.split("\\s+");
it = new ItemType(sp[0],Double.parseDouble(sp[1]),Double.parseDouble(sp[2]));
itList.add(it);
}
br.close();
}catch(IOException ex) {
ex.printStackTrace();
}
return itList;
}
public List<ItemType> depositList(List<ItemType> list) {
List<ItemType> itList = new ArrayList<ItemType>();
for(int i=0;i<list.size();i++) {
if(list.get(i).getDeposit()>2000) {
itList.add(list.get(i));
}
}
return itList;
}
public void display(List<ItemType>list)
{
String type="Item type",dep="Deposit",cost="Cost per day";
System.out.printf("%-15s %-15s %s\n",type,dep,cost);
for(int i =0;i<list.size();i++)
{
System.out.printf("%-15s %-15s %s\n",list.get(i).getName(),list.get(i).getDeposit(),list.get(i).getCostPerDay());
}
}
}




MAIN.JAVA:

import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
    BufferedReader br=new BufferedReader(new FileReader("input.txt"));
    ItemTypeBO it=new ItemTypeBO();
    List<ItemType> itList=it.readFromFile(br);
    List<ItemType> itList1=it.depositList(itList);
    if(itList1.isEmpty())
    {
    System.out.println("No itemtype has deposit more than 2000");
    }
    else
    {
    it.display(itList1);
    }
    }
    }

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.