Skip to content

Instantly share code, notes, and snippets.

@NemchinovSergey
Forked from yegor256/quiz.java
Created April 16, 2021 09:41
Show Gist options
  • Save NemchinovSergey/0796839b918a45ba368efc8053f73def to your computer and use it in GitHub Desktop.
Save NemchinovSergey/0796839b918a45ba368efc8053f73def to your computer and use it in GitHub Desktop.
quiz.java
/**
* Please review the class below and suggest improvements. How would
* you refactor this class if it would be in a real-life project?
* There are many problems here, both high-level design mistakes,
* and low-level implementation bugs. We're interested to see high-level
* problems first, since they are most critical. The more mistakes
* you can spot, the better programmer you are.
*/
/**
* This class is thread safe.
*/
public class Parser {
private File file;
public synchronized void setFile(File f) {
file = f;
}
public synchronized File getFile() {
return file;
}
public String getContent() throws IOException {
InputStream i = new FileInputStream(file);
String output = "";
int data;
while ((data = i.read()) > 0) {
output += (char) data;
}
return output;
}
public String getContentWithoutUnicode() throws IOException {
InputStream i = new FileInputStream(file);
String output = "";
int data;
while ((data = i.read()) > 0) {
if (data < 0x80) {
output += (char) data;
}
}
return output;
}
public void saveContent(String content) throws IOException {
OutputStream o = new FileOutputStream(file);
for (int i = 0; i < content.length(); i += 1) {
o.write(content.charAt(i));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment