Skip to content

Commit

Permalink
Merge pull request #20 from git-user-cpp/development
Browse files Browse the repository at this point in the history
Optimization
- a hash table in the file main.rs was replaced by vector
- added struct Product in file main.rs for using vector
- added some visual delimiters in file menu_first.rs
- added pushing the 'prod' element into the vector in file menu_first.rs
- changed parameters in function run_second_option in file menu_second.rs
- functions 'show_list' and 'count_total_sum' were re-implemented for using vector in file options.rs
  • Loading branch information
git-user-cpp authored Feb 15, 2023
2 parents 37f94e3 + bbb1b55 commit 4d584ca
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
15 changes: 9 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,21 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

use std::collections::HashMap;

mod menu_main;
mod menu_first;
mod menu_second;
mod menu_third;
mod options;

fn main() {
pub struct Product {
name: String,
price: f64,
}

fn main() {

//hashmap for holding products data
let mut products = HashMap::<String, String>::new();
let mut products_list: Vec<Product> = Vec::new();

//loop for main menu
loop {
Expand All @@ -53,11 +56,11 @@ fn main() {
if choise == 1 {
//running the first option
menu_first::show_first_option();
menu_first::run_first_option(&mut products);
menu_first::run_first_option(&mut products_list);
}else if choise == 2 {
//running the second option
menu_second::show_second_option();
menu_second::run_second_option(&products);
menu_second::run_second_option(&products_list);
}else if choise == 3 {
//running the third option
menu_third::show_third_option();
Expand Down
16 changes: 12 additions & 4 deletions src/menu_first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

use std::collections::HashMap;
use crate::Product;
use colored::*;

//function for showing the first menu option
Expand All @@ -27,7 +26,7 @@ pub fn show_first_option() {
}

//function for running the first option
pub fn run_first_option(products: &mut HashMap<String, String>) {
pub fn run_first_option(products: &mut Vec<Product>) {
loop {
println!("{} {}", ">".red(), "Please input amount of your products:".green());

Expand All @@ -43,12 +42,21 @@ pub fn run_first_option(products: &mut HashMap<String, String>) {
let mut name = String::new();
let mut price = String::new();

println!(" {}","-----------------------".red());
println!("{} {} {} {}{}", "> Input".red(), i+1, "product".red(), "name".yellow(), ":".red());
let new_name = crate::options::read_product(&mut name);
println!("{} {} {} {}{}", "> Input".red(), i+1, "product".red(), "price".yellow(), ":".red());
let new_price = crate::options::read_product(&mut price);
println!(" {}","-----------------------".red());

let new_price: f64 = new_price.trim().parse().expect("Failed to convert");

let prod: Product = Product {
name: new_name,
price: new_price,
};

products.insert(new_name, new_price);
products.push(prod);
}

break;
Expand Down
4 changes: 2 additions & 2 deletions src/menu_second.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

use std::collections::HashMap;
use crate::Product;
use colored::*;

//function for showing the second menu option
Expand All @@ -27,7 +27,7 @@ pub fn show_second_option() {
}

//function for running the second menu option
pub fn run_second_option(products: &HashMap<String, String>) {
pub fn run_second_option(products: &Vec<Product>) {
crate::options::show_list(products);
crate::options::count_total_sum(products);
}
1 change: 0 additions & 1 deletion src/menu_third.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

use std::collections::HashMap;
use colored::*;

//function for showing the third menu option
Expand Down
15 changes: 7 additions & 8 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ SOFTWARE.
*/

use std::io;
use std::collections::HashMap;
use colored::*;
use crate::Product;

//function for choosing an option
pub fn make_choise() -> String {
Expand All @@ -43,21 +43,20 @@ pub fn read_product(tmp: &mut String) -> String {
}

//function for showing the list of products
pub fn show_list(products: &HashMap<String, String>) {
for (name, price) in products {
println!(" {}\n {} {} {}","-----------------------".red(), name, price, "-----------------------".red());
pub fn show_list(products: &Vec<Product>) {
for element in products {
println!(" {}\n {} {} {}","-----------------------".red(), element.name, element.price, "\n -----------------------".red());
}
}

//function for counting total sum
pub fn count_total_sum(products: &HashMap<String, String>) {
pub fn count_total_sum(products: &Vec<Product>) {
//variable for total sum
let mut sum: f64 = 0.0;

//counting total sum
for (name, price) in products {
let price: f64 = price.trim().parse().expect("Enter a number");
sum += price;
for element in products {
sum += element.price;
}

println!(" {} {}\n {}", "Total sum =".yellow(), sum, "-----------------------".red());
Expand Down

0 comments on commit 4d584ca

Please sign in to comment.