How to resolve the algorithm Simple database step by step in the D programming language
How to resolve the algorithm Simple database step by step in the D programming language
Table of Contents
Problem Statement
Write a simple tool to track a small set of data. The tool should have a command-line interface to enter at least two different values. The entered data should be stored in a structured format and saved to disk. It does not matter what kind of data is being tracked. It could be a collection (CDs, coins, baseball cards, books), a diary, an electronic organizer (birthdays/anniversaries/phone numbers/addresses), etc.
You should track the following details:
The command should support the following Command-line arguments to run:
The category may be realized as a tag or as structure (by making all entries in that category subitems) The file format on disk should be human readable, but it need not be standardized. A natively available format that doesn't need an external library is preferred. Avoid developing your own format if you can use an already existing one. If there is no existing format available, pick one of:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Simple database step by step in the D programming language
Source code in the d programming language
import std.stdio, std.algorithm, std.string, std.conv, std.array,
std.file, std.csv, std.datetime;
private {
immutable filename = "simdb.csv";
struct Item {
string name, date, category;
}
void addItem(in string[] item) {
if (item.length < 3)
return printUsage();
auto db = load();
const date = (cast(DateTime)Clock.currTime).toISOExtString;
const cat = (item.length == 4) ? item[3] : "none";
db ~= Item(item[2], date, cat);
store(db);
}
void printLatest(in string[] a) {
auto db = load();
if (db.empty)
return writeln("No entries in database.");
db.sort!q{ a.date > b.date };
if (a.length == 3) {
foreach (item; db)
if (item.category == a[2])
writefln("%s, %s, %s", item.tupleof);
} else {
writefln("%s, %s, %s", db[0].tupleof);
}
}
void printAll() {
auto db = load();
if (db.empty)
return writeln("No entries in database.");
db.sort!q{ a.date < b.date };
foreach (item; db)
writefln("%s, %s, %s", item.tupleof);
}
Item[] load() {
Item[] db;
if (filename.exists && filename.isFile) {
try {
const text = filename.readText;
if (!text.empty)
db = csvReader!Item(text).array;
} catch (CSVException e) {
writeln(e.msg);
}
}
return db;
}
void store(in Item[] db) {
auto f = File(filename, "w+");
foreach (item; db)
f.writefln("%s,%s,%s", item.tupleof);
}
void printUsage() {
writeln(
`Usage:
simdb cmd [categoryName]
add add item, followed by optional category
latest print last added item(s), followed by optional category
all print all
For instance: add "some item name" "some category name"`);
}
}
void main(in string[] args) {
if (args.length < 2 || args.length > 4)
return printUsage();
switch (args[1].toLower) {
case "add": addItem(args); break;
case "latest": printLatest(args); break;
case "all": printAll(); break;
default: printUsage(); break;
}
}
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm File size step by step in the Clean programming language
You may also check:How to resolve the algorithm Constrained random points on a circle step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Haskell programming language
You may also check:How to resolve the algorithm Box the compass step by step in the REXX programming language