Mein kleines Wörterbuch



  • Ich habe als kleine Übung ein textuelles Wortverwaltungsprogramm geschrieben. Vorschläge, was man wie verbessern könnte sind gerne gesehen 🙂 Über jeden gemeldeten Fehler bin ich ebenfalls dankbar!

    Ich erläutere mal kurz die Befehle:
    help - ein Hilfetext wird angezeigt
    show <argument> - Wörter werden angezeigt, mit Eingabe von "*" werden alle Wörter bzw Zeilen angezeigt
    add <argument> - Zeile wird hinzugefügt
    del <argument> - Wörter werden gelöscht
    save - Wörter werden auf Festplatte gespeichert
    exit - Programm wird geschlossen

    Dictionary.java

    import java.io.File;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.FileNotFoundException;
    
    public class Dictionary
    {
        private String[] words;
        private File file;
    
        public Dictionary()
        {
    
            words = new String[1000];
            file = new File("words.txt");
    
            int i = 0;
            if (file.exists())
            {
                try
                {
                    FileReader file_reader = new FileReader("words.txt");
                    BufferedReader buffered_reader = new BufferedReader(file_reader);
    
                    String word;
                    while((word = buffered_reader.readLine()) != null)
                    {
                        words[i] = word;
                        i++;
                    }
    
                    buffered_reader.close();
                }
                catch(FileNotFoundException e)
                {
                    e.printStackTrace();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                try
                {
                    file.createNewFile();
                }
                catch(IOException e)
                {
                    e.printStackTrace();
                }
            }
    
            for(;i < 1000; i++)
                words[i] = "";
        }
    
        public void show_words(String filter)
        {
            if (filter.equals("*"))
            {
                for(String word : words)
                {
                    if(word.equals(""))
                    {
                        break;
                    }
                    else if(!(word.equals("<[d]>")))
                    {
                        System.out.println(word);
                    }
                }
            }
            else
            {
                for(String word : words)
                {
                    if(word.toLowerCase().contains(filter.toLowerCase()))
                    {
                        System.out.println(word);
                    }
                    else if(word.equals(""))
                    {
                        break;
                    }
                }
            }
        }
    
        public void add_word(String word)
        {
            for(int i = 0; i < words.length; i++)
            {
                if (words[i].equals("") || word.equals("<[d]>"))
                {
                    words[i] = word;
                    break;
                }
            }
        }
    
        public void del_words(String string)
        {
            for(int i = 0; i < words.length; i++)
            {
                if (words[i].contains(string))
                {
                    System.out.println("\"" + words[i] + "\" deleted");
                    words[i] = "<[d]>";
                }
            }      
        }
    
        public void save_words()
        {
            try
            {
                FileWriter file_writer = new FileWriter("words.txt");
                BufferedWriter buffered_writer = new BufferedWriter(file_writer);
                int i = 0;
                while(words[i] != "")
                {
                    if(!(words[i].equals("<[d]>")))
                    {
                        buffered_writer.write(words[i]);
                        buffered_writer.newLine();
                    }
                    i++;
                }
                buffered_writer.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    

    Commander.java

    import java.util.Scanner;
    
    public class Commander
    {
        private Dictionary dictionary;
        private Scanner input;
    
        public Commander()
        {
            input = new Scanner(System.in);
            dictionary = new Dictionary();
        }
    
        public void init()
        {
            while (true)
            {
                System.out.print("> ");
                String command = input.nextLine();
    
                if(command.equals("help"))
                {
                    help();
                }
                else if(command.length() >= 4 && command.substring(0, 4).equals("show"))
                {
                    dictionary.show_words(parse(command));
                }
                else if(command.length() >= 3 && command.substring(0, 3).equals("add"))
                {
                    dictionary.add_word(parse(command));
                }
                else if(command.length() >= 3 && command.substring(0, 3).equals("del"))
                {
                    dictionary.del_words(parse(command));
                }
                else if(command.equals("save"))
                {
                    dictionary.save_words();
                }
    
                else if(command.equals("exit"))
                {
                    dictionary.save_words();
                    break;
                }
                else
                {
                    System.out.println("invalid syntax");
                }
            }
        }
    
        private String parse(String input)
        {
            String parsed = input.substring(input.indexOf(' ') + 1, input.length());
            return parsed;
        }
    
        private void help()
        {
    
            System.out.println("help - no arguments    - shows information");
            System.out.println("save - no arguments    - writing changes on hard drive");
            System.out.println("show - <string> or <*> - shows words");
            System.out.println("add  - <string>        - adds word to dictionary");
            System.out.println("del  - <string>        - deletes entrys that contain string");
        }
    }
    

    Start.java

    public class Start
    {
        public static void main(String[] args)
        {
            Commander commander = new Commander();
            commander.init();
        }
    }
    

    Die Datei "words.txt" könnte so aussehen:

    Hallo - hello
    Apfel - apple
    Zeichenfolge - string
    rot - red
    gelb - yellow
    neu - new
    alt - old
    Wie geht es dir? - How are you?
    Insel - island
    Welt - world