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/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); + } +}