Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package dictionary;
- import java.util.List;
- import java.util.ArrayList;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.Arrays;
- import java.io.FileNotFoundException;
- import java.util.Map;
- import java.util.HashMap;
- /**
- *
- * @author colin
- */
- public class MindfulDictionary {
- private String file;
- private Map<String,String> dict = new HashMap<String,String>();
- public MindfulDictionary(){
- }
- public MindfulDictionary(String file){
- this.file = file;
- }
- //makes the contents of the file accessible
- //by putting the word and the translation into the instance hashMap
- public boolean load(){
- try{
- Scanner reader = new Scanner(new File(file));
- while (reader.hasNextLine()){
- String line = reader.nextLine();
- System.out.println("current line: " +line);
- String addition[] = line.split(":");
- dict.put(addition[0], addition[1]);
- }
- return true;
- }catch(Exception e){
- return false;
- }
- }
- public void printDict(){
- for (String key : dict.keySet()){
- System.out.println("Key: " +key+ " | Value: " +dict.get(key));
- }
- }
- public boolean save(){
- try{
- FileWriter writer = new FileWriter(file);
- System.out.print("save(): ");
- printDict();
- for (String key : dict.keySet()){
- System.out.println("current key: " +key+ " | current value: " +dict.get(key) );
- writer.write(key+ ":" +dict.get(key)+ "\n");
- }
- writer.close();
- System.out.println("end of save()");
- return true;
- }catch (Exception e){
- return false;
- }
- }
- public void add(String word, String translation){
- if (!dict.containsKey(word)){
- dict.put(word, translation);
- }
- }
- public String translate(String word){
- for (String key : dict.keySet()){
- if (key.equals(word)){
- return dict.get(key);
- }
- else if(dict.get(key).equals(word)){
- return key;
- }
- }
- return null;
- }
- public void remove(String word){
- for (String key : dict.keySet()){
- if (key.equals(word)){
- dict.remove(key);
- }
- else if (dict.get(key).equals(word)){
- dict.remove(key);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement