Simple database: Difference between revisions
Content deleted Content added
m →{{header|Rebol}}: Changed "Rebol" to "REBOL" |
→{{header|Java}}: added Java |
||
Line 2,041: | Line 2,041: | ||
'2012-02-08:23:45:06.539';'many more!' |
'2012-02-08:23:45:06.539';'many more!' |
||
$ </lang> |
$ </lang> |
||
=={{header|Java}}== |
|||
{{trans|D}} |
|||
{{works with|Java 7}} |
|||
<lang java>import java.io.*; |
|||
import java.text.*; |
|||
import java.util.*; |
|||
public class SimpleDatabase { |
|||
final static String filename = "simdb.csv"; |
|||
public static void main(String[] args) throws ParseException { |
|||
if (args.length < 1 || args.length > 3) { |
|||
printUsage(); |
|||
return; |
|||
} |
|||
switch (args[0].toLowerCase()) { |
|||
case "add": |
|||
addItem(args); |
|||
break; |
|||
case "latest": |
|||
printLatest(args); |
|||
break; |
|||
case "all": |
|||
printAll(); |
|||
break; |
|||
default: |
|||
printUsage(); |
|||
break; |
|||
} |
|||
} |
|||
private static class Item implements Comparable<Item>{ |
|||
final String name; |
|||
final String date; |
|||
final String category; |
|||
Item(String n, String d, String c) { |
|||
name = n; |
|||
date = d; |
|||
category = c; |
|||
} |
|||
@Override |
|||
public int compareTo(Item item){ |
|||
return item.date.compareTo(date); |
|||
} |
|||
@Override |
|||
public String toString() { |
|||
return String.format("%s,%s,%s%n", name, date, category); |
|||
} |
|||
} |
|||
private static void addItem(String[] input) { |
|||
if (input.length < 2) { |
|||
printUsage(); |
|||
return; |
|||
} |
|||
List<Item> db = load(); |
|||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|||
String date = sdf.format(new Date()); |
|||
String cat = (input.length == 3) ? input[2] : "none"; |
|||
db.add(new Item(input[1], date, cat)); |
|||
store(db); |
|||
} |
|||
private static void printLatest(String[] a) { |
|||
List<Item> db = load(); |
|||
if (db.isEmpty()) { |
|||
System.out.println("No entries in database."); |
|||
return; |
|||
} |
|||
Collections.sort(db); |
|||
if (a.length == 2) { |
|||
for (Item item : db) |
|||
if (item.category.equals(a[1])) |
|||
System.out.println(item); |
|||
} else { |
|||
System.out.println(db.get(0)); |
|||
} |
|||
} |
|||
private static void printAll() { |
|||
List<Item> db = load(); |
|||
if (db.isEmpty()) { |
|||
System.out.println("No entries in database."); |
|||
return; |
|||
} |
|||
Collections.sort(db); |
|||
for (Item item : db) |
|||
System.out.println(item); |
|||
} |
|||
private static List<Item> load() { |
|||
List<Item> db = new ArrayList<>(); |
|||
try (BufferedReader br = new BufferedReader(new FileReader(filename))) { |
|||
String line; |
|||
while ((line = br.readLine()) != null) { |
|||
String[] item = line.split(","); |
|||
db.add(new Item(item[0], item[1], item[2])); |
|||
} |
|||
} catch (IOException e) { |
|||
System.out.println(e); |
|||
} |
|||
return db; |
|||
} |
|||
private static void store(List<Item> db) { |
|||
try (FileWriter fw = new FileWriter(filename)) { |
|||
for (Item item : db) |
|||
fw.write(item.toString()); |
|||
} catch (IOException e) { |
|||
System.out.println(e); |
|||
} |
|||
} |
|||
private static void printUsage() { |
|||
System.out.println("Usage:"); |
|||
System.out.println(" simdb cmd [categoryName]"); |
|||
System.out.println(" add add item, followed by optional category"); |
|||
System.out.println(" latest print last added item(s), followed by " |
|||
+ "optional category"); |
|||
System.out.println(" all print all"); |
|||
System.out.println(" For instance: add \"some item name\" " |
|||
+ "\"some category name\""); |
|||
} |
|||
}</lang> |
|||
Output: |
|||
<pre> |
|||
C:\temp>java -jar SimpleDatabase.jar add item1 |
|||
C:\temp>java -jar SimpleDatabase.jar add item2 |
|||
C:\temp>java -jar SimpleDatabase.jar add item3 cat3 |
|||
C:\temp>java -jar SimpleDatabase.jar add item4 cat3 |
|||
C:\temp>java -jar SimpleDatabase.jar add item5 cat3 |
|||
C:\temp>java -jar SimpleDatabase.jar latest |
|||
item1,2014-06-03 19:30:05,none |
|||
C:\temp>java -jar SimpleDatabase.jar latest cat3 |
|||
item5,2014-06-03 19:30:23,cat3 |
|||
item4,2014-06-03 19:30:20,cat3 |
|||
item3,2014-06-03 19:30:14,cat3 |
|||
C:\temp>java -jar SimpleDatabase.jar all |
|||
item5,2014-06-03 19:30:23,cat3 |
|||
item4,2014-06-03 19:30:20,cat3 |
|||
item3,2014-06-03 19:30:14,cat3 |
|||
item2,2014-06-03 19:30:08,none |
|||
item1,2014-06-03 19:30:05,none</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |