Advertisement
chorium

Reseptihaku

Feb 23rd, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.37 KB | None | 0 0
  1.  
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Reseptihaku {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner lukija = new Scanner(System.in);
  10.         ArrayList<ArrayList<String>> reseptilistat = new ArrayList<>();
  11.         ArrayList<Resepti> reseptit = new ArrayList<>();
  12.        
  13.         System.out.println("Mistä luetaan?");
  14.         String tiedosto = lukija.nextLine();
  15.        
  16.         try (Scanner tiedostonLukija = new Scanner(new File(tiedosto))) {
  17.             int indeksi = 0;
  18.             while (tiedostonLukija.hasNextLine()) {
  19.                 if (reseptilistat.size()==0) {
  20.                     reseptilistat.add(new ArrayList<>());
  21.                 }
  22.                 String rivi = tiedostonLukija.nextLine();
  23.                
  24.                 if (!rivi.isEmpty()) {
  25.                     reseptilistat.get(indeksi).add(rivi);
  26.                 } else {
  27.                     indeksi++;
  28.                     reseptilistat.add(new ArrayList<>());
  29.                 }
  30.             }
  31.         } catch (Exception e) {
  32.             System.out.println("Virhe: "+e.getMessage());
  33.         }
  34.        
  35.        
  36.         for (int i = 0;i<reseptilistat.size();i++) {
  37.             reseptit.add(new Resepti(reseptilistat.get(i)));
  38.         }
  39.        
  40.         System.out.println("Komennot:");
  41.         System.out.println("listaa - listaa reseptit");
  42.         System.out.println("lopeta - lopettaa ohjelman");
  43.         System.out.println("hae nimi -  hakee reseptiä nimen perusteella");
  44.         System.out.println("hae keittoaika - hakee reseptiä keittoajan perusteella");
  45.         System.out.println("hae aine - hakee reseptiä raaka-aineen perusteella");
  46.        
  47.         while (true) {
  48.             System.out.println("Syötä komento: ");
  49.             String komento = lukija.nextLine();
  50.             if (komento.equals("lopeta")) {
  51.                 break;
  52.             }
  53.             if (komento.equals("listaa")) {
  54.                 System.out.println("Reseptit:");
  55.                 for (Resepti resepti : reseptit) {
  56.                     System.out.println(resepti.toString());
  57.                 }
  58.             }
  59.             if (komento.equals("hae nimi")) {
  60.                 System.out.println("Mitä haetaan:");
  61.                 String haku = lukija.nextLine();
  62.                 for (Resepti resepti : reseptit) {
  63.                     if (resepti.getNimi().contains(haku)) {
  64.                         System.out.println(resepti.toString());
  65.                     }
  66.                 }
  67.             }
  68.             if (komento.equals("hae keittoaika")) {
  69.                 System.out.println("Keittoaika korkeintaan:");
  70.                 int keittoaika = Integer.valueOf(lukija.nextLine());
  71.                 for (Resepti resepti : reseptit) {
  72.                     if (resepti.getKesto() <= keittoaika) {
  73.                         System.out.println(resepti.toString());
  74.                     }
  75.                 }
  76.             }
  77.             if (komento.equals("hae aine")) {
  78.                 System.out.println("Mitä raaka-ainetta haetaan;");
  79.                 String raakaAine = lukija.nextLine();
  80.                
  81.                 for (Resepti resepti : reseptit) {
  82.                     if (resepti.sisaltaako(raakaAine)) {
  83.                         System.out.println(resepti.toString());
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement