Skip to content

Commit 863f1a4

Browse files
authored
Add files via upload
1 parent 557e27d commit 863f1a4

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "dibz"
3+
version = "0.1.0"
4+
authors = ["limisi"]
5+
edition = "2018"
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
text_io = "0.1.8"
10+
pickledb = "0.4.1"
11+
dirs = "3.0.1"

src/analysis.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub mod analysis {
2+
3+
use pickledb::PickleDb;
4+
pub fn mean(mydb: &PickleDb, total: u16) -> f64 {
5+
let wosh = total as f64;
6+
let mash = mydb.total_keys() as f64;
7+
wosh / mash
8+
}
9+
10+
pub fn sum(mydb: &PickleDb) -> u16 {
11+
let mut val: u16;
12+
let mut total: u16 = 0;
13+
for kv in mydb.iter() {
14+
val = kv.get_value().unwrap();
15+
total = val + total;
16+
}
17+
total
18+
}
19+
20+
pub fn median(mosh: Vec<u16>) {
21+
if mosh.len() % 2 != 0 {
22+
println!("Median is = {}", mosh[mosh.len() / 2 - 1]);
23+
} else if mosh.len() % 2 == 0 {
24+
let mido = (mosh[mosh.len() / 2] + mosh[mosh.len() / 2 - 1]) / 2;
25+
println!("Median is = {}", mido);
26+
}
27+
}
28+
29+
pub fn fill_in(mydb: &PickleDb) -> Vec<u16> {
30+
let mut mosh: Vec<u16> = Vec::new();
31+
for kv in mydb.iter() {
32+
mosh.push(kv.get_value().unwrap());
33+
}
34+
mosh.sort();
35+
mosh
36+
}
37+
38+
pub fn extreme_vals(mosh: Vec<u16>) {
39+
println!("Biggest amount is = {}", mosh.iter().max().unwrap());
40+
println!();
41+
println!("Smallest amount is = {}", mosh.iter().min().unwrap());
42+
}
43+
}

src/internals.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
pub mod internals {
2+
3+
use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod};
4+
use std::path::Path;
5+
use text_io::read;
6+
7+
pub fn lookup(name: &str, mydb: &PickleDb) {
8+
match mydb.get::<u16>(name) {
9+
Some(v) => {
10+
println!("{}'s amount is {}", name, v)
11+
}
12+
None => {
13+
println!("No such entry")
14+
}
15+
};
16+
}
17+
18+
pub fn remove(name: &str, mydb: &mut PickleDb) {
19+
match mydb.rem(name) {
20+
Ok(v) => {
21+
println!();
22+
if v {
23+
println!("{} has been successfully removed", name)
24+
} else {
25+
println!("Record does not exist")
26+
}
27+
}
28+
Err(e) => {
29+
println!("Error, failed due to: {}", e)
30+
}
31+
};
32+
}
33+
34+
pub fn update(mydb: &mut PickleDb) {
35+
println!();
36+
println!("Input name followed by amount");
37+
let name: String = read!();
38+
let named = capup(&name);
39+
let amount: u16 = read!();
40+
println!();
41+
if mydb.exists(&named[0..named.len()]) {
42+
mydb.set(&named[0..named.len()], &amount).unwrap();
43+
println!("{} successfully updated", named)
44+
} else {
45+
mydb.set(&named[0..named.len()], &amount).unwrap();
46+
println!("{} successfully added", named)
47+
}
48+
}
49+
50+
fn iterate(mydb: &PickleDb) {
51+
for kv in mydb.iter() {
52+
match kv.get_key() {
53+
_ => print!("{}", kv.get_key()),
54+
}
55+
print!(" ");
56+
}
57+
println!();
58+
println!();
59+
}
60+
61+
pub fn bc_handler(val: &str, mydb: &PickleDb) {
62+
println!();
63+
println!("These are the available records: ");
64+
iterate(mydb);
65+
println!("What name do you want to {}?", val);
66+
}
67+
68+
pub fn initdb() -> PickleDb {
69+
let mydb: PickleDb;
70+
let root = dirs::config_dir().unwrap();
71+
let the_way = root.to_str().unwrap();
72+
let true_way = the_way.to_owned();
73+
let the_path = true_way + "/.mydb";
74+
if !Path::new(&the_path[0..the_path.len()]).is_file() {
75+
mydb = PickleDb::new(
76+
the_path,
77+
PickleDbDumpPolicy::AutoDump,
78+
SerializationMethod::Cbor,
79+
)
80+
} else {
81+
mydb = PickleDb::load(
82+
the_path,
83+
PickleDbDumpPolicy::AutoDump,
84+
SerializationMethod::Cbor,
85+
)
86+
.unwrap()
87+
}
88+
mydb
89+
}
90+
91+
pub fn capup(name: &String) -> String {
92+
let (first, suffix) = name.split_at(1);
93+
let cap = first.to_uppercase();
94+
let downfix = suffix.to_lowercase();
95+
let captup: String = cap + &downfix;
96+
97+
captup
98+
}
99+
}

src/main.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use text_io::read;
2+
mod analysis;
3+
use analysis::analysis::{extreme_vals, fill_in, mean, median, sum};
4+
mod internals;
5+
use internals::internals::{bc_handler, capup, initdb, lookup, remove, update};
6+
7+
fn main() {
8+
println!("What would you love to do today?");
9+
prompt();
10+
loop {
11+
choices();
12+
println!();
13+
println!("More operations? (y/n)");
14+
let ans: String = read!();
15+
if ans == "n" {
16+
println!();
17+
println!("Goodbye, see you later i hope :)");
18+
break;
19+
} else if ans == "y" {
20+
println!();
21+
prompt();
22+
continue;
23+
} else {
24+
println!();
25+
println!("Back to the menu we go :)");
26+
println!();
27+
prompt();
28+
}
29+
}
30+
}
31+
fn prompt() {
32+
println!("(a)input/update records (b)look up a record");
33+
println!(" (c)delete records (d)analysis");
34+
}
35+
36+
fn choices() {
37+
let mut mydb = initdb();
38+
let choice: String = read!();
39+
let name: String;
40+
if choice == "a" {
41+
update(&mut mydb);
42+
} else if choice == "b" {
43+
bc_handler("look up", &mydb);
44+
name = read!();
45+
println!();
46+
let named = capup(&name);
47+
lookup(&named[0..named.len()], &mydb);
48+
} else if choice == "c" {
49+
bc_handler("remove", &mydb);
50+
name = read!();
51+
let named = capup(&name);
52+
remove(&named[0..named.len()], &mut mydb);
53+
println!("Current number of records left: {}", mydb.total_keys());
54+
} else if choice == "d" && mydb.total_keys() > 1 {
55+
let total = sum(&mydb);
56+
println!();
57+
println!("Total number of records is {}", mydb.total_keys());
58+
println!();
59+
println!("The total amount is = {}", total);
60+
println!();
61+
println!("The mean is = {}", mean(&mydb, total));
62+
println!();
63+
median(fill_in(&mydb));
64+
println!();
65+
extreme_vals(fill_in(&mydb));
66+
} else if choice == "d" && mydb.total_keys() < 2 {
67+
println!(":( Not enough records");
68+
} else {
69+
println!("Invalid input!!!");
70+
println!();
71+
println!("heh, heh, back to prompt :)");
72+
println!();
73+
}
74+
}

0 commit comments

Comments
 (0)