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
6 changes: 4 additions & 2 deletions NoteBook/src/info/istamendil/notebook/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
25 changes: 25 additions & 0 deletions NoteBook/src/info/istamendil/notebook/data/RAMDb.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Scanner;

/**
*
Expand Down
45 changes: 45 additions & 0 deletions NoteBook/src/info/istamendil/notebook/utils/RAMInteractor.java
Original file line number Diff line number Diff line change
@@ -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);
}
}