From 447ff625037892378020163b853e74b4285da0b0 Mon Sep 17 00:00:00 2001 From: git-user-cpp Date: Mon, 12 Jun 2023 11:05:29 +0300 Subject: [PATCH] Added new main menu option - in file console_menu.rs function print_license() was implemented --- .github/workflows/CI_Ubuntu.yml | 10 +++---- .github/workflows/CI_Windows.yml | 12 ++++---- .github/workflows/CI_macOS.yml | 10 +++---- src/console/console_menu.rs | 27 +++++++++++++++-- src/main.rs | 51 +++++--------------------------- src/options/general_options.rs | 50 +++++++++++++++++++++++++++++-- src/options/menu_options.rs | 22 +++++++------- 7 files changed, 106 insertions(+), 76 deletions(-) diff --git a/.github/workflows/CI_Ubuntu.yml b/.github/workflows/CI_Ubuntu.yml index 617802d..f4d7f3e 100644 --- a/.github/workflows/CI_Ubuntu.yml +++ b/.github/workflows/CI_Ubuntu.yml @@ -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 diff --git a/.github/workflows/CI_Windows.yml b/.github/workflows/CI_Windows.yml index b7360f4..c7220bb 100644 --- a/.github/workflows/CI_Windows.yml +++ b/.github/workflows/CI_Windows.yml @@ -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 diff --git a/.github/workflows/CI_macOS.yml b/.github/workflows/CI_macOS.yml index 4ccf80e..f0fac90 100644 --- a/.github/workflows/CI_macOS.yml +++ b/.github/workflows/CI_macOS.yml @@ -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 diff --git a/src/console/console_menu.rs b/src/console/console_menu.rs index 6f997ba..96964a7 100644 --- a/src/console/console_menu.rs +++ b/src/console/console_menu.rs @@ -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(), @@ -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(), @@ -85,7 +87,7 @@ pub fn print_third_option() { ///Function for printing the list of products -pub fn print_list(products: &Vec) { +pub fn print_list(products: &Vec) { for element in products { println!(" {}\n Product: {} Price: {} {}", "-----------------------------------------".red(), @@ -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" + ); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 3078d30..4c78df2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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 = Vec::new(); - percentage(&products_list, total_sum); - } - } + launch_main_console_menu(products_list); print_stop_message(); } diff --git a/src/options/general_options.rs b/src/options/general_options.rs index 38089a1..b7c0430 100644 --- a/src/options/general_options.rs +++ b/src/options/general_options.rs @@ -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) { + 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"); diff --git a/src/options/menu_options.rs b/src/options/menu_options.rs index 2e4102a..be8ef7e 100644 --- a/src/options/menu_options.rs +++ b/src/options/menu_options.rs @@ -18,16 +18,16 @@ 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) { +pub fn run_first_option(products: &mut Vec) { 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, @@ -35,12 +35,12 @@ pub fn run_first_option(products: &mut Vec) { 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); } @@ -51,12 +51,12 @@ pub fn run_first_option(products: &mut Vec) { ///Function for running the second option -pub fn run_second_option(products: &Vec) { - options::general_options::display_list(products); +pub fn run_second_option(products: &Vec) { + display_list(products); } ///Function for running the second menu option -pub fn run_third_option(products: &Vec, sum: &mut f64) { - options::general_options::count_total_sum(products, sum); +pub fn run_third_option(products: &Vec, sum: &mut f64) { + count_total_sum(products, sum); } \ No newline at end of file