From f2726846790d2cd2d1a222f756bdabff6a912214 Mon Sep 17 00:00:00 2001 From: SRVergasov Date: Tue, 30 Nov 2021 22:14:09 +0300 Subject: [PATCH 1/4] add keyboard input, storing data in RAM --- .../src/info/istamendil/notebook/App.java | 5 ++-- .../info/istamendil/notebook/data/RAMDb.java | 25 +++++++++++++++++++ .../utils/PunchedCardUserInteractor.java | 21 ++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 NoteBook/src/info/istamendil/notebook/data/RAMDb.java diff --git a/NoteBook/src/info/istamendil/notebook/App.java b/NoteBook/src/info/istamendil/notebook/App.java index a304673..752e8ae 100644 --- a/NoteBook/src/info/istamendil/notebook/App.java +++ b/NoteBook/src/info/istamendil/notebook/App.java @@ -3,6 +3,7 @@ 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.UserInteractor; import info.istamendil.notebook.utils.PunchedCardUserInteractor; import info.istamendil.notebook.utils.UserInteractorException; @@ -41,8 +42,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 PunchedCardUserInteractor(); + 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..ac4a27b 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; /** * @@ -28,6 +29,26 @@ public PunchedCardUserInteractor(Path path) throws UserInteractorException { } } + public PunchedCardUserInteractor() 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; From dcab42452bcadbdd47aba14de40cd3b9b40bb547 Mon Sep 17 00:00:00 2001 From: SRVergasov Date: Tue, 30 Nov 2021 22:24:03 +0300 Subject: [PATCH 2/4] NoteBook: add keyboard input, storing data in RAM --- .../src/info/istamendil/notebook/App.java | 5 ++-- .../info/istamendil/notebook/data/RAMDb.java | 25 +++++++++++++++++++ .../utils/PunchedCardUserInteractor.java | 21 ++++++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 NoteBook/src/info/istamendil/notebook/data/RAMDb.java diff --git a/NoteBook/src/info/istamendil/notebook/App.java b/NoteBook/src/info/istamendil/notebook/App.java index a304673..752e8ae 100644 --- a/NoteBook/src/info/istamendil/notebook/App.java +++ b/NoteBook/src/info/istamendil/notebook/App.java @@ -3,6 +3,7 @@ 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.UserInteractor; import info.istamendil.notebook.utils.PunchedCardUserInteractor; import info.istamendil.notebook.utils.UserInteractorException; @@ -41,8 +42,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 PunchedCardUserInteractor(); + 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..ac4a27b 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; /** * @@ -28,6 +29,26 @@ public PunchedCardUserInteractor(Path path) throws UserInteractorException { } } + public PunchedCardUserInteractor() 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; From 7a86a899a845b1d86de89347932fd915d1de13cc Mon Sep 17 00:00:00 2001 From: SRVergasov Date: Tue, 21 Dec 2021 20:37:43 +0300 Subject: [PATCH 3/4] add RAMDb and RAMInteractor --- .../src/info/istamendil/notebook/App.java | 3 +- .../utils/PunchedCardUserInteractor.java | 20 --------- .../notebook/utils/RAMInteractor.java | 45 +++++++++++++++++++ 3 files changed, 47 insertions(+), 21 deletions(-) create mode 100644 NoteBook/src/info/istamendil/notebook/utils/RAMInteractor.java diff --git a/NoteBook/src/info/istamendil/notebook/App.java b/NoteBook/src/info/istamendil/notebook/App.java index 752e8ae..424d686 100644 --- a/NoteBook/src/info/istamendil/notebook/App.java +++ b/NoteBook/src/info/istamendil/notebook/App.java @@ -4,6 +4,7 @@ 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; @@ -42,7 +43,7 @@ public App(String[] args) { @Override public void init() { try { - this.terminal = new PunchedCardUserInteractor(); + this.terminal = new RAMInteractor(); this.db = new RAMDb(); } catch (UserInteractorException ex) { System.out.println("Couldn't start application due error:"); diff --git a/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java b/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java index ac4a27b..b2ff432 100644 --- a/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java +++ b/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java @@ -29,26 +29,6 @@ public PunchedCardUserInteractor(Path path) throws UserInteractorException { } } - public PunchedCardUserInteractor() 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; 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); + } +} From d5400bda7a9b5404e3aaad21acb8deda69494ab1 Mon Sep 17 00:00:00 2001 From: SRVergasov Date: Tue, 21 Dec 2021 21:04:25 +0300 Subject: [PATCH 4/4] fix import --- NoteBook/src/info/istamendil/notebook/App.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/NoteBook/src/info/istamendil/notebook/App.java b/NoteBook/src/info/istamendil/notebook/App.java index 07cdb42..424d686 100644 --- a/NoteBook/src/info/istamendil/notebook/App.java +++ b/NoteBook/src/info/istamendil/notebook/App.java @@ -4,10 +4,7 @@ import info.istamendil.notebook.data.DbException; import info.istamendil.notebook.data.FileDb; import info.istamendil.notebook.data.RAMDb; -<<<<<<< HEAD import info.istamendil.notebook.utils.RAMInteractor; -======= ->>>>>>> dcab42452bcadbdd47aba14de40cd3b9b40bb547 import info.istamendil.notebook.utils.UserInteractor; import info.istamendil.notebook.utils.PunchedCardUserInteractor; import info.istamendil.notebook.utils.UserInteractorException;