Package Problem(In Java)

You want to send your friend a package with different things.
Each thing you put inside the package has such parameters as index number, weight and cost.
The package has a weight limit.
Your goal is to determine which things to put into the package so that the total weight is less than or equal to the package limit and the total cost is as large as possible.

You would prefer to send a package which weights less in case there is more than one package with the same price.

INPUT SAMPLE:

Your program should accept as its first argument a path to a filename. The input file contains several lines. Each line is one test case.

Each line contains the weight that the package can take (before the colon) and the list of things you need to choose. Each thing is enclosed in parentheses where the 1st number is a thing’s index number, the 2nd is its weight and the 3rd is its cost. Eg:

81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48)
8 : (1,15.3,$34)
75 : (1,85.31,$29) (2,14.55,$74) (3,3.98,$16) (4,26.24,$55) (5,63.69,$52) (6,76.25,$75) (7,60.02,$74) (8,93.18,$35) (9,89.95,$78)
56 : (1,90.72,$13) (2,33.80,$40) (3,43.15,$10) (4,37.97,$16) (5,46.81,$36) (6,48.77,$79) (7,81.80,$45) (8,19.36,$79) (9,6.76,$64)

OUTPUT SAMPLE:

For each set of things that you put into the package provide a list (items’ index numbers are separated by comma). E.g.
4

2,7
8,9

CONSTRAINTS:

Max weight that a package can take is ≤ 100
There might be up to 15 items you need to choose from
Max weight and cost of an item is ≤ 100

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.ArrayList;
class Packet{
float weight;
int price;
}
class Package
{
HashMap<Integer,Packet> packets;
int weight;
public static void main(String args[]) throws Exception{
BufferedReader br=new BufferedReader(new FileReader(“input.txt”));
String line=br.readLine();
Package pack=new Package();
while(line!=null && line.length()>12){
pack.packets=new HashMap<Integer,Packet>();
int index=line.indexOf(":");
pack.weight=Integer.parseInt(line.substring(0,index-1));
boolean check=false;
if(pack.weight<=100){
line=line.substring(index+3,line.length());
if(pack.init(line) && pack.packets.size()<=15){
pack.packing();
}else{
System.out.println("-");
}
}
line=br.readLine();
}
}
//packing process
void packing(){
float tempWeight1=0;
int tempPrice1=0;
String result="";
ArrayList key=new ArrayList(packets.keySet());
for(int i=0;i<key.size();i++){
Packet p=packets.get(key.get(i));
float tempWeight2=p.weight; int tempPrice2=p.price;// get packet from inner loop
float tempWeight4=p.weight; int tempPrice4=p.price;// for other combinations in the inner loop
String tempResult2=""+key.get(i)+","; String tempResult4=tempResult2;
int iterate=1; //will iterate other combinations
for(int j=i+iterate;j<key.size();){
float tempWeight3=packets.get(key.get(j)).weight;
if(tempWeight3+tempWeight2<=weight){
tempWeight2+=tempWeight3;
tempPrice2+=packets.get(key.get(j)).price;
tempResult2+=key.get(j)+",";
}
j++;
if(j<key.size()){ //to pack after break and to compare if pack has last index
continue;
}
if(tempPrice4<tempPrice2 ||(tempPrice4==tempPrice2 && tempWeight4>tempWeight2)){
tempPrice4=tempPrice2;
tempWeight4=tempWeight2;
tempResult4=tempResult2;
}
iterate++;
j=i+iterate;
tempResult2=key.get(i)+",";
tempWeight2=p.weight;
tempPrice2=p.price;
}
if(tempPrice1<tempPrice4 ||(tempPrice1==tempPrice4 && tempWeight1>tempWeight4)){
tempPrice1=tempPrice4;
tempWeight1=tempWeight4;
result=tempResult4.substring(0,tempResult4.length()-1);
}
}
if(result.length()>0){
System.out.println(result);
}else{
System.out.println("-");
}
}
//intializing
boolean init(String line){
int index1=line.indexOf(",");
int index2=line.indexOf(",$");
int index3=line.indexOf(")");
Packet p=new Packet();
p.weight=Float.parseFloat(line.substring(index1+1,index2));
p.price=Integer.parseInt(line.substring(index2+2,index3));
if(p.price>100 || p.weight>100){
return false;
}
if(p.weight<=weight){
index1=Integer.parseInt(line.substring(0,index1));
packets.put(index1,p);
}
if(index3+1!=line.length()){
line=line.substring(index3+3,line.length());
return init(line);
}
return true;
}
}