MiniMi2022

Small shop

Apr 8th, 2023 (edited)
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  2. import org.apache.poi.ss.usermodel.Cell;
  3. import org.apache.poi.ss.usermodel.Row;
  4. import org.apache.poi.ss.usermodel.Sheet;
  5.  
  6. import java.io.FileInputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.Locale;
  10. import java.util.Scanner;
  11.  
  12.  
  13. public class SmallShop {
  14.     public static void main(String[] args) throws IOException {
  15.         Scanner myScan = new Scanner(System.in);
  16.         String product = myScan.nextLine();
  17.         String town = myScan.nextLine();
  18.         double quan = Double.parseDouble(myScan.nextLine());
  19.  
  20.         Locale.setDefault(Locale.ROOT);
  21.  
  22.         InputStream excelFileToRead = new FileInputStream
  23.                 ("D:\\Documents\\IdeaProjects\\1Steps\\src\\SmallShopInfo.xlsx");
  24.         HSSFWorkbook wb = new HSSFWorkbook(excelFileToRead);
  25.         Sheet sheet = wb.getSheetAt(0);
  26.         int indexR = getIndex(sheet,town);
  27.         int indexC = getIndex(sheet,product);
  28.  
  29.         if (!(indexR == 0 || indexC == 0)) {
  30.             Cell cell = sheet.getRow(indexR).getCell(indexC);
  31.             double price = cell.getNumericCellValue();
  32.             double sum = quan * price;
  33.             System.out.println(sum);
  34.         }
  35.     }
  36.  
  37.     private static int getIndex(Sheet sheet, String text) {
  38.         for (Row row : sheet) {
  39.             for (Cell cell : row) {
  40.                 if (cell.getCellType() == Cell.CELL_TYPE_STRING
  41.                         &&cell.getStringCellValue().equals(text)
  42.                         &&(row.getRowNum()==0||cell.getColumnIndex()==0)){
  43.                     return row.getRowNum()+cell.getColumnIndex();
  44.                 }
  45.             }
  46.         }
  47.         return 0;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment