diff --git a/NoteBook/src/info/istamendil/notebook/App.java b/NoteBook/src/info/istamendil/notebook/App.java index a304673..424d686 100644 --- a/NoteBook/src/info/istamendil/notebook/App.java +++ b/NoteBook/src/info/istamendil/notebook/App.java @@ -3,6 +3,8 @@ import info.istamendil.notebook.data.Db; import info.istamendil.notebook.data.DbException; import info.istamendil.notebook.data.FileDb; +import info.istamendil.notebook.data.RAMDb; +import info.istamendil.notebook.utils.RAMInteractor; import info.istamendil.notebook.utils.UserInteractor; import info.istamendil.notebook.utils.PunchedCardUserInteractor; import info.istamendil.notebook.utils.UserInteractorException; @@ -41,8 +43,8 @@ public App(String[] args) { @Override public void init() { try { - this.terminal = new PunchedCardUserInteractor(Paths.get(App.PUNCH_CARD)); - this.db = new FileDb(App.DB); + this.terminal = new RAMInteractor(); + this.db = new RAMDb(); } catch (UserInteractorException ex) { System.out.println("Couldn't start application due error:"); System.err.println(ex.getMessage()); diff --git a/NoteBook/src/info/istamendil/notebook/data/RAMDb.java b/NoteBook/src/info/istamendil/notebook/data/RAMDb.java new file mode 100644 index 0000000..553f2ca --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/data/RAMDb.java @@ -0,0 +1,25 @@ +package info.istamendil.notebook.data; + +public class RAMDb implements Db { + + protected String[] text; + + public RAMDb() { + this.text = new String[0]; + } + + @Override + public void save(Object obj) throws DbException { + String[] text1 = new String[this.text.length + 1]; + for (int i = 0; i < this.text.length; i++) { + text1[i] = this.text[i]; + } + text1[this.text.length] = (String) obj; + this.text = text1.clone(); + } + + @Override + public Object[] findAll() throws DbException { + return (Object[]) this.text; + } +} diff --git a/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java b/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java index 48ccb2b..b2ff432 100644 --- a/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java +++ b/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java @@ -4,6 +4,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import java.util.Scanner; /** * diff --git a/NoteBook/src/info/istamendil/notebook/utils/RAMInteractor.java b/NoteBook/src/info/istamendil/notebook/utils/RAMInteractor.java new file mode 100644 index 0000000..9841728 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/utils/RAMInteractor.java @@ -0,0 +1,45 @@ +package info.istamendil.notebook.utils; + +import java.util.Scanner; + +public class RAMInteractor implements UserInteractor { + + protected final String[] lines; + protected int currentLine = 0; + + public RAMInteractor() throws UserInteractorException { + Scanner scanner = new Scanner(System.in); + String line = scanner.nextLine(); + if (line.equals("/")) { + throw new UserInteractorException("Nothing to input"); + } + String[] lines = {line}; + while (!line.equals("/")) { // enter "/" for the end of the input + String[] lines1 = new String[lines.length + 1]; + for (int i = 0; i < lines.length; i++) { + lines1[i] = lines[i]; + } + lines1[lines.length] = line; + lines = lines1.clone(); + line = scanner.nextLine(); + } + this.lines = lines.clone(); + this.currentLine = 0; + } + + @Override + public String readCommand() throws UserInteractorReadException { + String command = null; + if(this.currentLine < this.lines.length){ + command = this.lines[this.currentLine]; + this.currentLine++; + System.out.println("<< " + command); + } + return command; + } + + @Override + public void print(String output) throws UserInteractorWriteException { + System.out.println(">> " + output); + } +}