Skip to content

Commit

Permalink
ledger: requested changes updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Falilah committed Dec 22, 2024
1 parent 821dfbd commit 3f8ca26
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 108 deletions.
59 changes: 25 additions & 34 deletions exercises/practice/ledger/.meta/example.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,15 @@ pub enum Locale {
nl_NL,
}

#[derive(Debug, PartialEq, Drop)]
pub struct Entry {
pub date: ByteArray,
pub description: ByteArray,
pub amount_in_cents: i32,
}

pub fn format_entries(
currency: Currency, locale: Locale, transactions: Array<(ByteArray, ByteArray, ByteArray)>
currency: Currency, locale: Locale, entries: Array<Entry>
) -> Array<ByteArray> {
let mut ledger: Array<ByteArray> = array![];

Expand All @@ -24,30 +31,21 @@ pub fn format_entries(
Locale::nl_NL => "Datum | Omschrijving | Verandering ",
};
ledger.append(header);

// Step 2: Process transactions
for (
date, transaction, change
) in transactions {
let formatted_date = format_date(@date, @locale);

// Format the change based on the currency and locale
let formatted_change = format_change(@change, @currency, @locale);

// Format the transaction row
let mut row = formatted_date;
row += " | ";
row += format_transaction(transaction);
row += " | ";
row += formatted_change;

// Append the row to the ledger
for entry in entries {
let row = format_row(@currency, @locale, entry);
ledger.append(row);
};

ledger
}

fn format_row(currency: @Currency, locale: @Locale, entry: Entry) -> ByteArray {
let date = format_date(@entry.date, locale);
let amount_in_cents = format_amount(@entry.amount_in_cents, currency, locale);
format!("{} | {} | {}", date, format_description(entry.description), amount_in_cents)
}

// format date based on the locale handled in 1 function
fn format_date(date: @ByteArray, locale: @Locale) -> ByteArray {
let (mut year, mut month, mut day) = split_date(date);
Expand Down Expand Up @@ -89,18 +87,20 @@ fn split_date(date: @ByteArray) -> (ByteArray, ByteArray, ByteArray) {
(year, month, day)
}

fn format_change(change: @ByteArray, currency: @Currency, locale: @Locale) -> ByteArray {
fn format_amount(amount_in_cents: @i32, currency: @Currency, locale: @Locale) -> ByteArray {
let amount_in_cents = format!("{amount_in_cents}");
let mut int_value: u32 = 0;
let mut negative = false;
let mut i = 0;

if change[i] == '-' {
if amount_in_cents[i] == '-' {
negative = true;
i += 1;
}

while i < change.len() {
if let Option::Some(digit) = char_to_digit(change[i]) {
while i < amount_in_cents.len() {
let zero_ascii = '0';
if let Option::Some(digit) = Option::Some(amount_in_cents[i] - zero_ascii) {
int_value = int_value * 10 + digit.into();
}
i += 1;
Expand Down Expand Up @@ -152,9 +152,9 @@ fn format_number(value: u32, negative: bool, currency: @Currency, locale: @Local

if fraction < 10 {
result.append_byte('0');
@fraction.append_formatted_to_byte_array(ref result, 10);
fraction.append_formatted_to_byte_array(ref result, 10);
} else {
@fraction.append_formatted_to_byte_array(ref result, 10);
fraction.append_formatted_to_byte_array(ref result, 10);
}

if negative && locale == @Locale::en_US {
Expand Down Expand Up @@ -194,7 +194,7 @@ fn add_sep(whole: u32, locale: @Locale) -> ByteArray {
result
}

fn format_transaction(transaction: ByteArray) -> ByteArray {
fn format_description(transaction: ByteArray) -> ByteArray {
let mut formatted = "";

if transaction.len() > 22 {
Expand All @@ -213,12 +213,3 @@ fn format_transaction(transaction: ByteArray) -> ByteArray {

formatted
}


fn char_to_digit(c: u8) -> Option<u8> {
if c >= '0' && c <= '9' {
Option::Some(c - '0')
} else {
Option::None
}
}
110 changes: 54 additions & 56 deletions exercises/practice/ledger/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use core::to_byte_array::{AppendFormattedToByteArray};

#[derive(Debug, PartialEq, Drop)]
pub enum Currency {
USD,
Expand All @@ -13,8 +11,15 @@ pub enum Locale {
nl_NL,
}

#[derive(Debug, PartialEq, Drop)]
pub struct Entry {
pub date: ByteArray,
pub description: ByteArray,
pub amount_in_cents: i32,
}

pub fn format_entries(
currency: Currency, locale: Locale, transactions: Array<(ByteArray, ByteArray, ByteArray)>
currency: Currency, locale: Locale, entries: Array<Entry>
) -> Array<ByteArray> {
let mut ledger: Array<ByteArray> = ArrayTrait::new();
let mut header = "";
Expand All @@ -23,11 +28,9 @@ pub fn format_entries(
} else if locale == Locale::nl_NL {
header = "Datum | Omschrijving | Verandering ";
}

// date, transaction, change
ledger.append(header);
for (
date, transaction, change
) in transactions {
for entry in entries {
let mut formatted_date = "";
if locale == Locale::en_US {
let mut year = "/";
Expand All @@ -37,15 +40,15 @@ pub fn format_entries(
let mut i = 0;
let mut sep = 0;

while i < date.len() {
if sep == 0 && i < 4 && date[i] != '-' {
year.append_byte(date[i]);
} else if date[i] == '-' {
while i < entry.date.len() {
if sep == 0 && i < 4 && entry.date[i] != '-' {
year.append_byte(entry.date[i]);
} else if entry.date[i] == '-' {
sep += 1;
} else if sep == 1 && i < 7 && date[i] != '-' {
month.append_byte(date[i]);
} else if sep == 1 && i < 7 && entry.date[i] != '-' {
month.append_byte(entry.date[i]);
} else {
day.append_byte(date[i]);
day.append_byte(entry.date[i]);
}
i += 1;
};
Expand All @@ -59,15 +62,15 @@ pub fn format_entries(
let mut i = 0;
let mut sep = 0;

while i < date.len() {
if sep == 0 && i < 4 && date[i] != '-' {
year.append_byte(date[i]);
} else if date[i] == '-' {
while i < entry.date.len() {
if sep == 0 && i < 4 && entry.date[i] != '-' {
year.append_byte(entry.date[i]);
} else if entry.date[i] == '-' {
sep += 1;
} else if sep == 1 && i < 7 && date[i] != '-' {
month.append_byte(date[i]);
} else if sep == 1 && i < 7 && entry.date[i] != '-' {
month.append_byte(entry.date[i]);
} else {
day.append_byte(date[i]);
day.append_byte(entry.date[i]);
}
i += 1;
};
Expand All @@ -76,6 +79,8 @@ pub fn format_entries(
}

let mut formatted_change = "";
let amount_in_cents = entry.amount_in_cents;
let change = format!("{amount_in_cents}");
if currency == Currency::USD {
if locale == Locale::en_US {
let mut result = "$";
Expand All @@ -92,7 +97,8 @@ pub fn format_entries(
}
let mut int: u32 = 0;
while i < change.len() {
let c = char_to_digit(change[i]);
let zero_ascii = '0';
let c = Option::Some(change[i] - zero_ascii);

match c {
Option::Some(v) => { int = int * 10 + v.into(); },
Expand All @@ -102,8 +108,7 @@ pub fn format_entries(
};

let val = int / 100;
let mut temp = "";
@val.append_formatted_to_byte_array(ref temp, 10);
let mut temp = format!("{val}");
if temp.len() > 3 {
result.append_byte(temp[0]);
let mut i = 1;
Expand All @@ -127,7 +132,8 @@ pub fn format_entries(
result += "0";
}

@rem.append_formatted_to_byte_array(ref result, 10);
let rem = format!("{rem}");
result += rem;
if result[result.len() - 2] == '.' {
result += "0";
}
Expand Down Expand Up @@ -159,7 +165,8 @@ pub fn format_entries(

let mut int: u32 = 0;
while i < change.len() {
let c = char_to_digit(change[i]);
let zero_ascii = '0';
let c = Option::Some(change[i] - zero_ascii);

match c {
Option::Some(v) => { int = int * 10 + v.into(); },
Expand All @@ -169,8 +176,7 @@ pub fn format_entries(
};

let val = int / 100;
let mut temp = "";
@val.append_formatted_to_byte_array(ref temp, 10);
let mut temp = format!("{val}");
if temp.len() > 3 {
result.append_byte(temp[0]);
let mut i = 1;
Expand All @@ -194,7 +200,8 @@ pub fn format_entries(
result += "0";
}

@rem.append_formatted_to_byte_array(ref result, 10);
let rem = format!("{rem}");
result += rem;
if result[result.len() - 2] == ',' {
result += "0";
}
Expand Down Expand Up @@ -231,7 +238,8 @@ pub fn format_entries(
}
let mut int: u32 = 0;
while i < change.len() {
let c = char_to_digit(change[i]);
let zero_ascii = '0';
let c = Option::Some(change[i] - zero_ascii);

match c {
Option::Some(v) => { int = int * 10 + v.into(); },
Expand All @@ -241,17 +249,18 @@ pub fn format_entries(
};

let val = int / 100;
@val.append_formatted_to_byte_array(ref result, 10);
let val = format!("{val}");
result += val;
result += ".";
let rem = int % 100;
if int < 10 {
result += "0";
}
@rem.append_formatted_to_byte_array(ref result, 10);

if result[result.len() - 2] == '.' {
result += "0";
let mut rem = format!("{rem}");
if rem.len() < 2 {
rem += "0";
}
result += rem;

result = ByteArrayTrait::concat(@ByteArrayTrait::concat(@op, @result), @cl);

Expand Down Expand Up @@ -283,7 +292,8 @@ pub fn format_entries(
}
let mut int: u32 = 0;
while i < change.len() {
let c = char_to_digit(change[i]);
let zero_ascii = '0';
let c = Option::Some(change[i] - zero_ascii);

match c {
Option::Some(v) => { int = int * 10 + v.into(); },
Expand All @@ -292,8 +302,7 @@ pub fn format_entries(
i += 1;
};
let val = int / 100;
let mut temp = "";
@val.append_formatted_to_byte_array(ref temp, 10);
let mut temp = format!("{val}");
if temp.len() > 3 {
result.append_byte(temp[0]);
let mut i = 1;
Expand All @@ -315,11 +324,11 @@ pub fn format_entries(
if int < 10 {
result += "0";
}
@rem.append_formatted_to_byte_array(ref result, 10);

if result[result.len() - 2] == ',' {
result += "0";
let mut rem = format!("{rem}");
if rem.len() < 2 && int >= 10 {
rem += "0";
}
result += rem;

result = ByteArrayTrait::concat(@ByteArrayTrait::concat(@op, @result), @cl);
let mut extra = "";
Expand All @@ -338,15 +347,15 @@ pub fn format_entries(

let mut row = formatted_date;
row += " | ";
if transaction.len() > 22 {
if entry.description.len() > 22 {
let mut i = 0;
while i < 22 {
row.append_byte(transaction[i]);
row.append_byte(entry.description[i]);
i += 1;
};
row += "...";
} else {
row += transaction;
row += entry.description;
}

if row.len() < 38 {
Expand All @@ -365,14 +374,3 @@ pub fn format_entries(

ledger
}

fn char_to_digit(c: u8) -> Option<u8> {
let zero_ascii = '0';
let nine_ascii = '9';

if c >= zero_ascii && c <= nine_ascii {
Option::Some(c - zero_ascii)
} else {
Option::None // Return None for invalid characters
}
}
Loading

0 comments on commit 3f8ca26

Please sign in to comment.