forked from dkomanov/fizteh-java-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
10,546 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ jars | |
|
||
.classpath | ||
.project | ||
workbench.xmi | ||
*.swp | ||
|
||
.settings | ||
.checkstyle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import util.*; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Scanner; | ||
|
||
public final class FileMap { | ||
|
||
public static void main(final String[] args) { | ||
File fl = new File("db.dat"/*System.getProperty("db.file")*/); | ||
HashMap<String, String> hm; | ||
try { | ||
hm = Data.load(fl); | ||
} catch (FileNotFoundException e) { | ||
System.out.println("File not found"); | ||
return; | ||
} catch (IOException e) { | ||
System.out.println("Error while reading file"); | ||
return; | ||
} | ||
Launcher lnch = new Launcher(hm); | ||
if (args.length != 0) { | ||
try { | ||
lnch.run(args); | ||
} catch (Exception e) { | ||
System.out.println(e.getMessage()); | ||
} | ||
} else { | ||
Scanner sc = new Scanner(System.in); | ||
while (true) { | ||
try { | ||
System.out.print("$ "); | ||
String input = sc.nextLine().trim(); | ||
lnch.run(input.split(" ")); | ||
} catch (IndexOutOfBoundsException e) { | ||
System.out.println(e.getMessage()); | ||
} catch (Exception e) { | ||
sc.close(); | ||
System.out.println(e.getMessage()); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
try { | ||
Data.save(fl, hm); | ||
} catch (FileNotFoundException e) { | ||
System.out.println("File not found"); | ||
return; | ||
} catch (IOException e) { | ||
System.out.println("Error while reading file"); | ||
return; | ||
} | ||
} | ||
} |
Empty file.
47 changes: 47 additions & 0 deletions
47
src/ru/fizteh/fivt/students/mrshoco/filemap/util/Data.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package util; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class Data { | ||
public static HashMap<String, String> load(File fl) throws | ||
IOException, FileNotFoundException { | ||
HashMap<String, String> hm = new HashMap<String, String>(); | ||
DataInputStream dis = new DataInputStream(new FileInputStream(fl)); | ||
|
||
while (dis.available() > 0) { | ||
int keyLen = dis.readInt(); | ||
String key = ""; | ||
for (int i = 0; i < keyLen; i++) { | ||
key += dis.readChar(); | ||
} | ||
int valLen = dis.readInt(); | ||
String val = ""; | ||
for (int i = 0; i < valLen; i++) { | ||
val += dis.readChar(); | ||
} | ||
hm.put(key, val); | ||
} | ||
dis.close(); | ||
return hm; | ||
} | ||
|
||
public static void save(File fl, HashMap<String, String> hm) throws | ||
IOException, FileNotFoundException { | ||
DataOutputStream dos = new DataOutputStream(new FileOutputStream(fl)); | ||
for (Map.Entry<String, String> element : hm.entrySet()) { | ||
dos.writeInt(element.getKey().length()); | ||
dos.writeChars(element.getKey()); | ||
dos.writeInt(element.getValue().length()); | ||
dos.writeChars(element.getValue()); | ||
} | ||
dos.close(); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/ru/fizteh/fivt/students/mrshoco/filemap/util/Launcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package util; | ||
|
||
import java.util.HashMap; | ||
|
||
public class Launcher { | ||
HashMap<String, String> hashMap; | ||
|
||
public Launcher(HashMap<String, String> hm) { | ||
hashMap = hm; | ||
} | ||
|
||
public void run(String[] cmd) throws Exception { | ||
switch (cmd[0]) { | ||
case "put": | ||
if (cmd.length != 3) { | ||
throw new IndexOutOfBoundsException("Wrong number of arguments"); | ||
} | ||
put(cmd[1], cmd[2]); | ||
break; | ||
case "get": | ||
if (cmd.length != 2) { | ||
throw new IndexOutOfBoundsException("Wrong number of arguments"); | ||
} | ||
get(cmd[1]); | ||
break; | ||
case "remove": | ||
if (cmd.length != 2) { | ||
throw new IndexOutOfBoundsException("Wrong number of arguments"); | ||
} | ||
remove(cmd[1]); | ||
break; | ||
case "list": | ||
if (cmd.length != 1) { | ||
throw new IndexOutOfBoundsException("Wrong number of arguments"); | ||
} | ||
list(); | ||
break; | ||
case "exit": | ||
if (cmd.length != 1) { | ||
throw new IndexOutOfBoundsException("Wrong number of arguments"); | ||
} | ||
throw new Exception("Exit"); | ||
default: | ||
System.out.println("Bad command"); | ||
} | ||
} | ||
|
||
private void put(String key, String value) { | ||
if (hashMap.containsKey(key)) { | ||
System.out.println("overwrite\nold value"); | ||
} else { | ||
System.out.println("new"); | ||
} | ||
hashMap.put(key, value); | ||
} | ||
|
||
private void get(String key) { | ||
if (hashMap.containsKey(key)) { | ||
System.out.println("found\n" + hashMap.get(key)); | ||
} else { | ||
System.out.println("not found"); | ||
} | ||
} | ||
|
||
private void remove(String key) { | ||
if (hashMap.containsKey(key)) { | ||
System.out.println("removed"); | ||
hashMap.remove(key); | ||
} else { | ||
System.out.println("not found"); | ||
} | ||
} | ||
|
||
private void list() { | ||
for (String key : hashMap.keySet()) { | ||
System.out.println(key + " "); | ||
} | ||
} | ||
} |
Oops, something went wrong.