Advertisement
Guest User

csvcreator

a guest
Sep 9th, 2018
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.63 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class CsvFileWriter {
  9.    
  10.     //delimiter
  11.     private static final String COMMA_DELIMITER = ",";
  12.     private static final String NEW_LINE_SEPARATOR = "\n";
  13.    
  14.     //header
  15.     private static final String FILE_HEADER = "amount, unit, ingredient, tags";
  16.    
  17.     public static void writeCsvFile(String recipeName) {
  18.        
  19.         ArrayList<Ingredients> ingredientList = createIngredientList();
  20.        
  21.         FileWriter fileWriter = null;
  22.        
  23.         try {
  24.            
  25.             fileWriter = new FileWriter(recipeName + ".csv");
  26.            
  27.             fileWriter.append(FILE_HEADER.toString());
  28.             fileWriter.append(NEW_LINE_SEPARATOR);
  29.            
  30.             for(Ingredients i : ingredientList) {
  31.                
  32.                 fileWriter.append(String.valueOf(i.getAmount()));
  33.                 fileWriter.append(COMMA_DELIMITER);
  34.                 fileWriter.append(i.getUnit());
  35.                 fileWriter.append(COMMA_DELIMITER);
  36.                 fileWriter.append(i.getTags());
  37.                 fileWriter.append(COMMA_DELIMITER);
  38.                 fileWriter.append(i.getIngredient());
  39.                 fileWriter.append(NEW_LINE_SEPARATOR);
  40.                
  41.             }
  42.            
  43.             System.out.print("Recipe " + recipeName + " was created!");
  44.            
  45.         } catch (Exception e) {
  46.            
  47.             System.out.print("Error in CSV-FileWriter!!");
  48.             e.printStackTrace();
  49.            
  50.         }finally {
  51.                    
  52.             try {
  53.                 fileWriter.flush();
  54.                 fileWriter.close();
  55.             } catch (IOException e) {
  56.                 System.out.println("Error while flushing/closing fileWriter !!!");
  57.                 e.printStackTrace();
  58.             }
  59.                              
  60.         }
  61.     }
  62.  
  63.  
  64.     public static ArrayList<Ingredients> createIngredientList() {
  65.        
  66.         Scanner scan =  new Scanner(System.in);
  67.         ArrayList<Ingredients> list = new ArrayList<Ingredients>();
  68.         char quit = 'Y';
  69.         String unit, ingredient, tags;
  70.         double amount;
  71.        
  72.         while(quit == 'Y') {
  73.             System.out.print("Please use underscores instead of spaces e.g. real_big_boats.\n");
  74.             System.out.print("Amount: ");
  75.             amount = scan.nextDouble();
  76.            
  77.             System.out.print("Unit: ");
  78.             unit = scan.next();
  79.            
  80.             System.out.print("Ingredient: ");
  81.             ingredient = scan.nextLine();
  82.            
  83.             System.out.print("Tags (e.g. tag;tag;tag): ");
  84.             tags = scan.nextLine();
  85.            
  86.             list.add(new Ingredients(amount, unit, ingredient, tags));
  87.            
  88.             System.out.print("More Ingredients? (Y/N) ");
  89.             String s = scan.next();
  90.             s = s.toUpperCase();
  91.             quit = s.charAt(0);
  92.            
  93.         }
  94.        
  95.         return list;
  96.    
  97.     }
  98.    
  99.     public static void main (String[] args) {
  100.         System.out.print("Type recipe name here: ");
  101.        
  102.         Scanner scan = new Scanner(System.in);
  103.         String recipeName = scan.next();
  104.        
  105.         writeCsvFile(recipeName);
  106.     }
  107.    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement