Skip to content

Commit

Permalink
Merge pull request #49 from git-user-cpp/v2_0_X
Browse files Browse the repository at this point in the history
Added new main menu option
  • Loading branch information
git-user-cpp authored Jun 12, 2023
2 parents 306a67a + 447ff62 commit 49da948
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 76 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/CI_Ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
12 changes: 6 additions & 6 deletions .github/workflows/CI_Windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ env:
jobs:
build:

runs-on: windows-latest
runs-on: windows-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
10 changes: 5 additions & 5 deletions .github/workflows/CI_macOS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
runs-on: macos-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
27 changes: 24 additions & 3 deletions src/console/console_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ Copyright 2023 Andrew Kushyk

use colored::Colorize;

use crate::product;
use crate::product::Product;

///Function for printing main menu
pub fn print_menu() {
println!(" {}\n{} {} {}\n {}\n{} Choose an option: {}\n{} {} {}\n{} {} {}\n{} {} {}\n{} {} {}\n {}\n{}",
println!(" {}\n{} {} {}\n {}\n{} Choose an option: {}\n{}{} {}\n{}{} {}\n{}{} {}\n{}{} {}\n{}{} {}\n {}\n{}",
"-----------------------------------------".blue(),
"|".blue(),
"Finance manager".yellow(),
Expand All @@ -40,6 +39,9 @@ pub fn print_menu() {
"[3] Show total sum & Show percentage ".green(),
"|".blue(),
"|".blue(),
"[4] Info about the program".yellow(),
"|".blue(),
"|".blue(),
"[0] Exit".red(),
"|".blue(),
"-----------------------------------------".blue(),
Expand Down Expand Up @@ -85,7 +87,7 @@ pub fn print_third_option() {

///Function for printing the list of products
pub fn print_list(products: &Vec<product::Product>) {
pub fn print_list(products: &Vec<Product>) {
for element in products {
println!(" {}\n Product: {} Price: {} {}",
"-----------------------------------------".red(),
Expand Down Expand Up @@ -154,4 +156,23 @@ pub fn print_mid_border(i: &u32) {
pub fn print_bot_border() {
println!(" {}", "-----------------------------------------".red());
}

///Function for printing project license
pub fn print_license() {
println!("\nCopyright 2023 Andrew Kushyk\n\
\n\
\tLicensed under the Apache License, Version 2.0 (the \"License\");\n\
\tyou may not use this file except in compliance with the License.\n\
\tYou may obtain a copy of the License at\n\
\n\
\t\thttp://www.apache.org/licenses/LICENSE-2.0\n\
\n\
\tUnless required by applicable law or agreed to in writing, software\n\
\tdistributed under the License is distributed on an \"AS IS\" BASIS,\n\
\tWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n\
\tSee the License for the specific language governing permissions and\n\
\tlimitations under the License.\n"
);
}
51 changes: 7 additions & 44 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,22 @@ Copyright 2023 Andrew Kushyk
limitations under the License.
*/

//Module for using this program via console/terminal mod console; mod console;
//Module for using this program via console/terminal;

mod console;

mod product_structure;
mod options;

use crate::console::console_menu::{print_first_option,
print_menu,
print_second_option,
print_stop_message,
print_third_option,
print_total_sum};
use crate::options::general_options::{make_choise,
percentage};
use crate::options::menu_options::{run_first_option,
run_second_option,
run_third_option};
use crate::options::general_options::launch_main_console_menu;
use crate::console::console_menu::print_stop_message;
use crate::product_structure::product;
use crate::product_structure::product::Product;

fn main() {
let mut products_list: Vec<product::Product> = Vec::new();

loop {
print_menu();

let choice = make_choise();

let choice: u8 = match choice.trim().parse() {
Ok(0) => break,
Ok(1) => 1,
Ok(2) => 2,
Ok(3) => 3,
Err(_) => continue,
Ok(i32::MIN..=-1_i32) | Ok(3_i32..=i32::MAX) => continue,
};

if choice == 1 {
print_first_option();
run_first_option(&mut products_list);
} else if choice == 2 {
print_second_option();
run_second_option(&products_list);
} else if choice == 3 {
let mut total_sum: f64 = 0.0;

print_third_option();
run_third_option(&products_list, &mut total_sum);

print_total_sum(&total_sum);
let products_list: Vec<Product> = Vec::new();

percentage(&products_list, total_sum);
}
}
launch_main_console_menu(products_list);

print_stop_message();
}
50 changes: 48 additions & 2 deletions src/options/general_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,59 @@ Copyright 2023 Andrew Kushyk

use std::io;

use crate::console::console_menu::print_list;
use crate::console::console_menu::{print_first_option,
print_list,
print_menu,
print_second_option,
print_third_option,
print_total_sum,
print_license};
use crate::console::console_menu::print_percentage;
use crate::options::menu_options::{run_first_option, run_second_option, run_third_option};
use crate::product::Product;

///Function for launching main menu in terminal
pub fn launch_main_console_menu(mut products_list: Vec<Product>) {
loop {
print_menu();

let choice = make_choice();

let choice: u8 = match choice.trim().parse() {
Ok(0) => break,
Ok(1) => 1,
Ok(2) => 2,
Ok(3) => 3,
Ok(4) => 4,
Err(_) => continue,
Ok(i32::MIN..=-1_i32) | Ok(3_i32..=i32::MAX) => continue,
};

if choice == 1 {
print_first_option();
run_first_option(&mut products_list);
} else if choice == 2 {
print_second_option();
run_second_option(&products_list);
} else if choice == 3 {
let mut total_sum: f64 = 0.0;

print_third_option();
run_third_option(&products_list, &mut total_sum);

print_total_sum(&total_sum);

percentage(&products_list, total_sum);
} else if choice == 4 {
print_license();
}
}
}

///Function for choosing an option in menus
pub fn make_choise() -> String {
pub fn make_choice() -> String {
let mut choise = String::new();

io::stdin().read_line(&mut choise).expect("Failed to read line");
Expand Down
22 changes: 11 additions & 11 deletions src/options/menu_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ use crate::console::console_menu::{print_bot_border,
print_inp_message,
print_mid_border,
print_top_border};
use crate::options;
use crate::product;
use crate::options::general_options::{count_total_sum, display_list, make_choice, read_product};
use crate::product_structure::product::Product;

///Function for running the first option
pub fn run_first_option(products: &mut Vec<product::Product>) {
pub fn run_first_option(products: &mut Vec<Product>) {
loop {
print_inp_message();

let amount = options::general_options::make_choise();
let amount = make_choice();
let amount: u32 = match amount.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};

for i in 0..amount {
print_top_border(&i);
let name = options::general_options::read_product();
let name = read_product();
print_mid_border(&i);
let price = options::general_options::read_product();
let price = read_product();
print_bot_border();

let prod = product::Product::new(name, price);
let prod = Product::new(name, price);

products.push(prod);
}
Expand All @@ -51,12 +51,12 @@ pub fn run_first_option(products: &mut Vec<product::Product>) {

///Function for running the second option
pub fn run_second_option(products: &Vec<product::Product>) {
options::general_options::display_list(products);
pub fn run_second_option(products: &Vec<Product>) {
display_list(products);
}

///Function for running the second menu option
pub fn run_third_option(products: &Vec<product::Product>, sum: &mut f64) {
options::general_options::count_total_sum(products, sum);
pub fn run_third_option(products: &Vec<Product>, sum: &mut f64) {
count_total_sum(products, sum);
}

0 comments on commit 49da948

Please sign in to comment.