use grade_calculator::{
Syllabus,
GradeCategory,
};
use std::{
process::Command,
thread::sleep,
time::Duration,
};
use text_io::read;
use colored::Colorize;
fn main() {
clear_screen();
let syllabus: Syllabus = Syllabus::new();
let num_selections: u8 = syllabus.num_categories() + 2;
let mut selection: u8 = 0;
while selection != num_selections {
println!("\n------ MENU ------");
for category in syllabus.categories() {
println!("{}: {}", category.0, category.1.name());
}
println!("{}: Display final grade", num_selections - 1);
println!("{}: Exit", num_selections);
print!("\nWhich category would you like to add a grade to? ");
let input: String = read!();
selection = match input.parse() {
Ok(num) => num,
Err(_) => 0,
};
if selection == 0 || selection > num_selections {
clear_screen();
println!("\n{}: Invalid option! Choose a number between 1-{}",
"Error".red().bold(), num_selections);
}
else {
if selection >= 1 && selection <= syllabus.num_categories() {
let category: &GradeCategory = syllabus.categories()
.get(&usize::from(selection))
.unwrap();
if !category.scores().borrow().contains(&-1.0) {
clear_screen();
println!("\n{}: Cannot add anymore grades to this category!",
"Error".red().bold());
println!(" Edit {} if you wish to add more grades.",
syllabus.filename().cyan());
}
else {
print!("\nEnter new grade for {}: ", category.name());
let grade: String = read!();
match grade.parse::<f32>() {
Ok(grade) => if grade < 0.0 || grade > 120.0 {
println!("\n{}: Grade value is outside valid range.",
"Error".red().bold());
sleep(Duration::from_secs(2));
}
else {
category.add_grade(grade);
category.export();
},
Err(msg) => {
println!("\n{}: {}", "Error".red().bold(), msg);
sleep(Duration::from_secs(2));
},
}
clear_screen();
}
}
else if selection == num_selections - 1 {
let mut acc: f32 = 0.0;
for (_, category) in syllabus.categories() {
acc += category.total();
}
acc *= 100.0;
let letter_grade: String =
if acc >= 90.0 { format!("{}", "A".purple().bold()) }
else if acc >= 80.0 { format!("{}", "B".green().bold()) }
else if acc >= 70.0 { format!("{}", "C".green().bold()) }
else if acc >= 60.0 { format!("{}", "D".yellow().bold()) }
else { format!("{}", "F".red().bold()) };
clear_screen();
println!("\nCurrent course grade: {} -> {}", acc, letter_grade);
}
}
}
println!();
}
fn clear_screen () {
if cfg!(target_os = "windows") {
Command::new("cls").status().unwrap();
}
else if cfg!(target_os = "macos") {
let esc: char = 27 as char;
print!("{}c{}[3J", esc, esc);
}
else {
Command::new("clear").status().unwrap();
}
}