Balance - Demo program

by shian ⌂, Sunday, September 06, 2020, 13:28 (23 days ago) @ shian

[image]You can download the Balance demo program from the Demo source programs for learning Euphoria 3. (Delete your browser's history to update the home page if needed...).


The Balance demo program demonstrates how to retrieve records from a simple colon-separated-values file, which is actually a CSV (Comma Separated Values) file that is using colon ':' instead of comma ',' to separate between fields.

The CSV database looks like this:

20120930:1100:deposit:2500
20121001:1100:rent pmt:-1400
...


The function split() from string.e in Lib2 1.41, is doing the work easily:

-- demo: BALANCE CALCULATOR, using Euphoria 3.1.1 + Lib2 v1.41
-- Version 1.10, 06/Sep/2020, shian.
 
-- demonstrates how to retrieve records from a simple colon-
-- separated-values file, ':', (text database).
 
-- In version 1.10: instead of loading *entire* database file into
-- memory we retrieve the records *one by one*, for efficiency.
 
-- Type in terminal with: edu balance.ex
-- Then run the program from the menu with: Esc-e
 
 
 
include graphics.e  -- colors
include string.e    -- Lib2: split(), val()
include machine2.e  -- Lib2: TRUE, EMPTY, pause(), iif()
 
constant DATABASE_FILE = "balance.db"
 
constant SCREEN = 1, EOF = -1
 
 
-- set screen colors
constant COLOR_NORMAL = 1,
           COLOR_PLUS = 2,
          COLOR_MINUS = 3
procedure set_color(integer color)
    bk_color(BLACK)
 
    if color = COLOR_NORMAL then
        text_color(WHITE)
    elsif color = COLOR_PLUS then
        text_color(BRIGHT_CYAN)
    elsif color = COLOR_MINUS then
        text_color(YELLOW)
    end if
end procedure
 
 
integer database_fn
database_fn = EOF -- initialize
 
-- return the *next* line from a database text file
function get_text_line(sequence file_name)
    object next_line
 
    -- open text file for reading only once on the beginning
    if database_fn = EOF then
        database_fn = open(file_name, "r")
 
        -- error?
        if database_fn = EOF then
            puts(SCREEN, "\nCannot find or open the file '" &
                            file_name & "' for reading!\n")
            pause()
            abort(1)
        end if
    end if
 
    -- get and return the next line in file
    next_line = gets(database_fn)
    if atom(next_line) then -- End of file? (EOF)
        close(database_fn)  -- close the file
        database_fn = EOF   -- set back flag that file is closed
    end if
 
    return next_line
end function
 
 
-- pause display after few lines
function press_any_key(integer line_counter)
    line_counter += 1
 
    if remainder(line_counter, 10) = 0 then
        puts(SCREEN, "Press any key to continue...")
        pause()
        puts(SCREEN, '\r') -- return cursor to column 1 in current line
    end if
 
    return line_counter
end function
 
 
-- some constants for table and formating line
constant T_WIDTH = 54,
          H_LINE = '+' & repeat('-', T_WIDTH - 2) & '+' & '\n',
     COMMON_LINE = "| %-20s | %+12.3f | %+12.3f |\n"
 
constant DELIMIT = ":\r\n"  -- colon ':', or end-of-line control characters
 
 
-- print horizontal line in table
procedure horizontal_line()
    set_color(COLOR_NORMAL)
    puts(SCREEN, H_LINE)
end procedure
 
 
-- display the balance table on the screen
procedure print_balance()
    sequence min_val, max_val, avg_val
    atom number, balance
    integer line_counter
    object line -- string-sequence or atom EOF
 
    -- initialize variables
    min_val = {0, 0}
    max_val = {0, 0}
    avg_val = {0, 0}
    balance = 0
    line_counter = 0
 
    -- print table captions
    set_color(COLOR_NORMAL)
    puts(SCREEN, mset("*** BALANCE CALCULATOR ***", T_WIDTH, ' ') & '\n')
    horizontal_line()
    printf(SCREEN, "| %-20s | %-12s | %-12s |\n",
        {mset("Use", 20, ' '), mset("Value", 12, ' '), mset("Sub Total", 12, ' ')}
    )
    horizontal_line()
 
    -- print the balance table (retrieve lines from database one by one)
    while TRUE do
        line = get_text_line(DATABASE_FILE)
        if atom(line) then
            exit    -- line is EOF (end of file)
        end if
 
        -- split line into fields, i.e. {"field1", field2", "field3", "field4"}
        line = split(line, DELIMIT)
 
        -- make sure line is valid (don't crash)
        if length(line) >= 4 then
            number = val(line[4])
            balance += number
 
            -- print line
            set_color(iif(balance < 0, COLOR_MINUS, COLOR_PLUS))
            printf(SCREEN, COMMON_LINE, {line[3], number, balance})
 
            -- update statistics
            if number  < min_val[1] then min_val[1] = number    end if
            if balance < min_val[2] then min_val[2] = balance   end if
            if number  > max_val[1] then max_val[1] = number    end if
            if balance > max_val[2] then max_val[2] = balance   end if
            avg_val[1] += number
            avg_val[2] += balance
 
            line_counter = press_any_key(line_counter)
        end if
    end while
 
    -- print table statistics & total
    horizontal_line()
    set_color(COLOR_NORMAL)
    printf(SCREEN, COMMON_LINE, {"MINIMUM:"} & min_val)
    printf(SCREEN, COMMON_LINE, {"MAXIMUM:"} & max_val)
    printf(SCREEN, COMMON_LINE, {"AVERAGE:"} & (avg_val / line_counter))
 
    horizontal_line()
    set_color(iif(balance < 0, COLOR_MINUS, COLOR_PLUS))
    printf(SCREEN, "| %-20s | %+12.3f | %12s |\n", {"TOTAL:", balance, ""})
 
    horizontal_line()
end procedure
 
 
-- start the program here
 
set_color(COLOR_NORMAL)
clear_screen()
 
print_balance()
pause()
 
 
-- end.
 

Tags:
demo


Complete thread:

 RSS Feed of thread

powered by my little forum