Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Reseptihaku {
- public static void main(String[] args) {
- Scanner lukija = new Scanner(System.in);
- ArrayList<ArrayList<String>> reseptilistat = new ArrayList<>();
- ArrayList<Resepti> reseptit = new ArrayList<>();
- System.out.println("Mistä luetaan?");
- String tiedosto = lukija.nextLine();
- try (Scanner tiedostonLukija = new Scanner(new File(tiedosto))) {
- int indeksi = 0;
- while (tiedostonLukija.hasNextLine()) {
- if (reseptilistat.size()==0) {
- reseptilistat.add(new ArrayList<>());
- }
- String rivi = tiedostonLukija.nextLine();
- if (!rivi.isEmpty()) {
- reseptilistat.get(indeksi).add(rivi);
- } else {
- indeksi++;
- reseptilistat.add(new ArrayList<>());
- }
- }
- } catch (Exception e) {
- System.out.println("Virhe: "+e.getMessage());
- }
- for (int i = 0;i<reseptilistat.size();i++) {
- reseptit.add(new Resepti(reseptilistat.get(i)));
- }
- System.out.println("Komennot:");
- System.out.println("listaa - listaa reseptit");
- System.out.println("lopeta - lopettaa ohjelman");
- System.out.println("hae nimi - hakee reseptiä nimen perusteella");
- System.out.println("hae keittoaika - hakee reseptiä keittoajan perusteella");
- System.out.println("hae aine - hakee reseptiä raaka-aineen perusteella");
- while (true) {
- System.out.println("Syötä komento: ");
- String komento = lukija.nextLine();
- if (komento.equals("lopeta")) {
- break;
- }
- if (komento.equals("listaa")) {
- System.out.println("Reseptit:");
- for (Resepti resepti : reseptit) {
- System.out.println(resepti.toString());
- }
- }
- if (komento.equals("hae nimi")) {
- System.out.println("Mitä haetaan:");
- String haku = lukija.nextLine();
- for (Resepti resepti : reseptit) {
- if (resepti.getNimi().contains(haku)) {
- System.out.println(resepti.toString());
- }
- }
- }
- if (komento.equals("hae keittoaika")) {
- System.out.println("Keittoaika korkeintaan:");
- int keittoaika = Integer.valueOf(lukija.nextLine());
- for (Resepti resepti : reseptit) {
- if (resepti.getKesto() <= keittoaika) {
- System.out.println(resepti.toString());
- }
- }
- }
- if (komento.equals("hae aine")) {
- System.out.println("Mitä raaka-ainetta haetaan;");
- String raakaAine = lukija.nextLine();
- for (Resepti resepti : reseptit) {
- if (resepti.sisaltaako(raakaAine)) {
- System.out.println(resepti.toString());
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement