Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 55 additions & 56 deletions NoteBook/src/info/istamendil/notebook/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,71 @@
*
* Note Book. UserInteractor and DB modules.
* @author Alexander Ferenets (Istamendil) <ist.kazan@gmail.com>
*
*
* 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);
}
}


}
22 changes: 17 additions & 5 deletions NoteBook/src/info/istamendil/notebook/data/FileDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

/**
*
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -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<String> 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<String> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}