Sunday, February 21, 2016

Cashless transaction with local Transport - part 2

Readdatabase.class

package JavaSMSProject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;



public class ReadDatabase {

public void writeToExcel(String fileName, String uid, String amount) throws IOException {

Integer cellValue = 0;
Integer cellAmount = 0;
Integer bal = 0;
Integer phoneNumber = 0;

FileInputStream fsIP= new FileInputStream(new File(fileName));
XSSFWorkbook workbook = new XSSFWorkbook(fsIP);
XSSFSheet sheet = workbook.getSheetAt(0);

int count = sheet.getPhysicalNumberOfRows();
System.out.println(count);

List<String> list = new ArrayList<String>();
/*int size = list.size(); */

  for(int i=0; i < count - 1; i++){

  XSSFRow row1 = sheet.getRow(i + 1);
  Cell cell1 = row1.getCell(0);

  cellValue = (int)Math.round(cell1.getNumericCellValue());

  System.out.println("UserId:" + cellValue);

  String matchuid = cellValue.toString();

  if(uid.equalsIgnoreCase(matchuid)){

  Cell cell2 = row1.getCell(1);

  cellAmount = (int)Math.round(cell2.getNumericCellValue());

  System.out.println("Amount:" + cellAmount);

  bal = cellAmount - Integer.parseInt(amount);

  System.out.println("Bal remaning:" + bal);

  Cell cell3 = row1.getCell(2);

  phoneNumber = (int)Math.round(cell3.getNumericCellValue());

  System.out.println("Phone number:" + phoneNumber);

  cell2.setCellValue(bal.doubleValue());

 
  CopyOfSMSclass sms = new CopyOfSMSclass();
  sms.sendSMS(Integer.parseInt(amount), bal, phoneNumber.toString());

  break;



  }//if


  //list.add(cell1.getStringCellValue());

  }//for

  System.out.println("Sorry no such user found");

  fsIP.close();
 try (FileOutputStream outputStream = new FileOutputStream(new File(fileName))) {
 workbook.write(outputStream);
}

catch (FileNotFoundException e) {
   
       e.printStackTrace();
   }


}

}

No comments:

Post a Comment