From ef06bbaf1b53be104a5a9c17f9c38540163c92af Mon Sep 17 00:00:00 2001 From: Yasha Ivanov Date: Tue, 7 Dec 2021 18:03:08 +0300 Subject: [PATCH 1/2] update console input and output --- .../info/istamendil/notebook/.idea/.gitignore | 8 ++ .../info/istamendil/notebook/.idea/misc.xml | 6 + .../istamendil/notebook/.idea/modules.xml | 8 ++ .../notebook/.idea/runConfigurations.xml | 10 ++ .../info/istamendil/notebook/.idea/vcs.xml | 6 + .../src/info/istamendil/notebook/App.java | 111 +++++++++--------- .../info/istamendil/notebook/data/FileDb.java | 22 +++- .../info/istamendil/notebook/.idea/.gitignore | 8 ++ .../info/istamendil/notebook/.idea/misc.xml | 6 + .../istamendil/notebook/.idea/modules.xml | 8 ++ .../notebook/.idea/runConfigurations.xml | 10 ++ .../info/istamendil/notebook/.idea/vcs.xml | 6 + .../utils/PunchedCardUserInteractor.java | 58 +++++---- .../utils/UserReadCommandInteractor.java | 18 +++ 14 files changed, 200 insertions(+), 85 deletions(-) create mode 100644 NoteBook/src/info/istamendil/notebook/.idea/.gitignore create mode 100644 NoteBook/src/info/istamendil/notebook/.idea/misc.xml create mode 100644 NoteBook/src/info/istamendil/notebook/.idea/modules.xml create mode 100644 NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml create mode 100644 NoteBook/src/info/istamendil/notebook/.idea/vcs.xml create mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore create mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml create mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml create mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml create mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml create mode 100644 NoteBook/src/info/istamendil/notebook/utils/UserReadCommandInteractor.java diff --git a/NoteBook/src/info/istamendil/notebook/.idea/.gitignore b/NoteBook/src/info/istamendil/notebook/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/NoteBook/src/info/istamendil/notebook/.idea/misc.xml b/NoteBook/src/info/istamendil/notebook/.idea/misc.xml new file mode 100644 index 0000000..8ed5f34 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/.idea/modules.xml b/NoteBook/src/info/istamendil/notebook/.idea/modules.xml new file mode 100644 index 0000000..ed5542c --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml b/NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/.idea/vcs.xml b/NoteBook/src/info/istamendil/notebook/.idea/vcs.xml new file mode 100644 index 0000000..bc59970 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/App.java b/NoteBook/src/info/istamendil/notebook/App.java index a304673..a62802c 100644 --- a/NoteBook/src/info/istamendil/notebook/App.java +++ b/NoteBook/src/info/istamendil/notebook/App.java @@ -15,72 +15,71 @@ * * Note Book. UserInteractor and DB modules. * @author Alexander Ferenets (Istamendil) - * + * * Code for studying purposes. Programming course. Kazan Federal University, ITIS. * http://study.istamendil.info/ */ public class App extends Application{ - - private static final String DB = "/tmp/db.txt"; - private static final String PUNCH_CARD = "/tmp/card.txt"; - - protected UserInteractor terminal; - protected Db db; - /** - * @param args the command line arguments - */ - public static void main(String[] args) { - App app = new App(args); - } + private static final String DB = "C:\\Users\\ICL\\Desktop\\data\\db.txt"; + private static final String PUNCH_CARD = "C:\\Users\\ICL\\Desktop\\data\\card.txt"; + + protected UserInteractor terminal; + protected Db db; - public App(String[] args) { - super(args); - } + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + App app = new App(args); + } - @Override - public void init() { - try { - this.terminal = new PunchedCardUserInteractor(Paths.get(App.PUNCH_CARD)); - this.db = new FileDb(App.DB); - } catch (UserInteractorException ex) { - System.out.println("Couldn't start application due error:"); - System.err.println(ex.getMessage()); - System.exit(1); + public App(String[] args) { + super(args); } - } - @Override - public void start() { - try { - String command; - while((command = this.terminal.readCommand()) != null){ - switch(command){ - case "readAll": - this.terminal.print(Arrays.toString(this.db.findAll())); - break; - case "save": - if((command = this.terminal.readCommand()) != null){ - this.db.save(command); + @Override + public void init() { + try { + this.terminal = new PunchedCardUserInteractor(); + this.db = new FileDb(App.DB); + } catch (UserInteractorException ex) { + System.out.println("Couldn't start application due error:"); + System.err.println(ex.getMessage()); + System.exit(1); + } + } + + @Override + public void start() { + try { + String command; + while((command = this.terminal.readCommand()) != null){ + switch(command){ + case "readAll": + this.terminal.print(Arrays.toString(this.db.findAll())); + break; + case "save": + if((command = this.terminal.readCommand()) != null){ + this.db.save(command); + } + break; + default: + this.terminal.print("Unknown command"); + } } - break; - default: - this.terminal.print("Unkown command"); + } catch (UserInteractorReadException ex) { + System.out.println("Can't read user input due error:"); + System.err.println(ex.getMessage()); + System.exit(1); + } catch (UserInteractorWriteException ex) { + System.out.println("Can't print data to user due error:"); + System.err.println(ex.getMessage()); + System.exit(1); + } catch (DbException ex) { + System.err.println(ex.getMessage()); + System.exit(1); } -// this.terminal.print(command); - } - } catch (UserInteractorReadException ex) { - System.out.println("Can't read user input due error:"); - System.err.println(ex.getMessage()); - System.exit(1); - } catch (UserInteractorWriteException ex) { - System.out.println("Can't print data to user due error:"); - System.err.println(ex.getMessage()); - System.exit(1); - } catch (DbException ex) { - System.err.println(ex.getMessage()); - System.exit(1); } - } - + } diff --git a/NoteBook/src/info/istamendil/notebook/data/FileDb.java b/NoteBook/src/info/istamendil/notebook/data/FileDb.java index 18a1e1e..b3ac8ac 100644 --- a/NoteBook/src/info/istamendil/notebook/data/FileDb.java +++ b/NoteBook/src/info/istamendil/notebook/data/FileDb.java @@ -11,6 +11,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; /** * @@ -23,27 +24,38 @@ public class FileDb implements Db { protected final String path; + private Object[] dataDb; public FileDb(String path) { + this.dataDb = new Object[0]; this.path = path; } @Override public void save(Object obj) throws DbException { Object[] data = this.findAll(); - try (FileOutputStream stream = new FileOutputStream(this.path)) { + Object[] newData = new Object[data.length + 1]; + System.arraycopy(data, 0, newData, 0, data.length); + newData[newData.length - 1] = obj; + this.dataDb = newData; + /*try (FileOutputStream stream = new FileOutputStream(this.path)) { Object[] newData = new Object[data.length + 1]; System.arraycopy(data, 0, newData, 0, data.length); newData[newData.length - 1] = obj; stream.write(this.convertToBytes(newData)); } catch (IOException ex) { throw new DbException("DB error: " + ex.getMessage()); - } + }*/ } - + @Override public Object[] findAll() throws DbException { - try { + if(this.dataDb.length > 0){ + return this.dataDb; + }else{ + return new Object[0]; + } + /*try { Path path = Paths.get(this.path); byte[] data = Files.readAllBytes(path); if (data.length > 0) { @@ -56,7 +68,7 @@ public Object[] findAll() throws DbException { throw new DbException("DB error: " + ex.getMessage()); } catch (ClassNotFoundException ex) { throw new DbException("DB error: " + ex.getMessage()); - } + }*/ } private byte[] convertToBytes(Object object) throws IOException { diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml new file mode 100644 index 0000000..8ed5f34 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml new file mode 100644 index 0000000..ed5542c --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml new file mode 100644 index 0000000..797acea --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml @@ -0,0 +1,10 @@ + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml new file mode 100644 index 0000000..bc59970 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java b/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java index 48ccb2b..7a540fb 100644 --- a/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java +++ b/NoteBook/src/info/istamendil/notebook/utils/PunchedCardUserInteractor.java @@ -3,7 +3,10 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.Scanner; /** * @@ -15,33 +18,40 @@ */ public class PunchedCardUserInteractor implements UserInteractor { - protected final String[] lines; - protected int currentLine = 0; + protected final String[] lines; + protected int currentLine = 0; - public PunchedCardUserInteractor(Path path) throws UserInteractorException { - try { - List lines = Files.readAllLines(path); - this.lines = lines.toArray(new String[lines.size()]); - this.currentLine = 0; - } catch (IOException ex) { - throw new UserInteractorException("Can't load punched card."); + public PunchedCardUserInteractor() throws UserInteractorException { + List lines = new ArrayList<>(); + + while(true){ + UserReadCommandInteractor receiver = new UserReadCommandInteractor(); + String command = receiver.readCommand(); + if(command.equals("exit")) { + break; + } else { + lines.add(command); + } + } + + this.lines = lines.toArray(new String[lines.size()]); + 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); + + @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; } - return command; - } - @Override - public void print(String output) throws UserInteractorWriteException { - System.out.println(">> " + output); - } + @Override + public void print(String output) throws UserInteractorWriteException { + System.out.println(">> " + output); + } } diff --git a/NoteBook/src/info/istamendil/notebook/utils/UserReadCommandInteractor.java b/NoteBook/src/info/istamendil/notebook/utils/UserReadCommandInteractor.java new file mode 100644 index 0000000..2cb9d78 --- /dev/null +++ b/NoteBook/src/info/istamendil/notebook/utils/UserReadCommandInteractor.java @@ -0,0 +1,18 @@ +package info.istamendil.notebook.utils; +import java.util.Scanner; + +public class UserReadCommandInteractor implements UserInteractor { + + @Override + public String readCommand() throws UserInteractorReadException { + Scanner sc = new Scanner(System.in); + System.out.println("Input next command: "); + String command = sc.nextLine(); + return command; + } + + @Override + public void print(String output) throws UserInteractorWriteException { + System.out.println(">> " + output); + } +} From 23308c5d6b4b9fd1e4e257d9ad47725f1f09c306 Mon Sep 17 00:00:00 2001 From: Yasha Ivanov Date: Tue, 7 Dec 2021 18:07:36 +0300 Subject: [PATCH 2/2] removed unnecessary --- NoteBook/src/info/istamendil/notebook/.idea/.gitignore | 8 -------- NoteBook/src/info/istamendil/notebook/.idea/misc.xml | 6 ------ .../src/info/istamendil/notebook/.idea/modules.xml | 8 -------- .../istamendil/notebook/.idea/runConfigurations.xml | 10 ---------- NoteBook/src/info/istamendil/notebook/.idea/vcs.xml | 6 ------ .../notebook/info/istamendil/notebook/.idea/.gitignore | 8 -------- .../notebook/info/istamendil/notebook/.idea/misc.xml | 6 ------ .../info/istamendil/notebook/.idea/modules.xml | 8 -------- .../istamendil/notebook/.idea/runConfigurations.xml | 10 ---------- .../notebook/info/istamendil/notebook/.idea/vcs.xml | 6 ------ 10 files changed, 76 deletions(-) delete mode 100644 NoteBook/src/info/istamendil/notebook/.idea/.gitignore delete mode 100644 NoteBook/src/info/istamendil/notebook/.idea/misc.xml delete mode 100644 NoteBook/src/info/istamendil/notebook/.idea/modules.xml delete mode 100644 NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml delete mode 100644 NoteBook/src/info/istamendil/notebook/.idea/vcs.xml delete mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore delete mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml delete mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml delete mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml delete mode 100644 NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml diff --git a/NoteBook/src/info/istamendil/notebook/.idea/.gitignore b/NoteBook/src/info/istamendil/notebook/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/NoteBook/src/info/istamendil/notebook/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/NoteBook/src/info/istamendil/notebook/.idea/misc.xml b/NoteBook/src/info/istamendil/notebook/.idea/misc.xml deleted file mode 100644 index 8ed5f34..0000000 --- a/NoteBook/src/info/istamendil/notebook/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/.idea/modules.xml b/NoteBook/src/info/istamendil/notebook/.idea/modules.xml deleted file mode 100644 index ed5542c..0000000 --- a/NoteBook/src/info/istamendil/notebook/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml b/NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/NoteBook/src/info/istamendil/notebook/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/.idea/vcs.xml b/NoteBook/src/info/istamendil/notebook/.idea/vcs.xml deleted file mode 100644 index bc59970..0000000 --- a/NoteBook/src/info/istamendil/notebook/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore deleted file mode 100644 index 73f69e0..0000000 --- a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml -# Editor-based HTTP Client requests -/httpRequests/ diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml deleted file mode 100644 index 8ed5f34..0000000 --- a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml deleted file mode 100644 index ed5542c..0000000 --- a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml deleted file mode 100644 index 797acea..0000000 --- a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/runConfigurations.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml b/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml deleted file mode 100644 index bc59970..0000000 --- a/NoteBook/src/info/istamendil/notebook/out/production/notebook/info/istamendil/notebook/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file