Last active
June 23, 2017 21:40
-
-
Save stpettersens/d86655092975c7b411a256fad152ee81 to your computer and use it in GitHub Desktop.
Utility to calculate the volume of paint needed to paint a room of supplied dimensions [in Rust].
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Utility to calculate the volume of paint needed to | |
paint a room of supplied dimensions. | |
Sam Saint-Pettersen, 2017. | |
*/ | |
extern crate clioptions; | |
use clioptions::CliOptions; | |
use std::io::stdin; | |
use std::process::exit; | |
struct Room { | |
width: f64, | |
length: f64, | |
height: f64, | |
} | |
impl Room { | |
pub fn get_width(&self) -> f64 { | |
self.width | |
} | |
pub fn get_length(&self) -> f64 { | |
self.length | |
} | |
pub fn get_height(&self) -> f64 { | |
self.height | |
} | |
pub fn get_fl_square_metres(&self) -> f64 { | |
self.width * self.height | |
} | |
pub fn get_vol_cubic_metres(&self) -> f64 { | |
self.width * self.length * self.height | |
} | |
} | |
fn parse_unit(unit: &str) -> f64 { | |
let n = unit.parse::<f64>().ok(); | |
let unit = match n { | |
Some(unit) => unit as f64, | |
None => 0 as f64, | |
}; | |
unit | |
} | |
fn get_input(prompt: &str) -> String { | |
println!("{}?", prompt); | |
let mut input = String::new(); | |
match stdin().read_line(&mut input) { | |
Ok(_) => {}, | |
Err(error) => { | |
println!("Stdin Error: {}", error); | |
exit(-1); | |
} | |
} | |
input.trim().to_owned() | |
} | |
fn calculate_paint_needed(square_m_per_litre: f64, room: &Room, coats: u64) { | |
let mut required_paint = (room.get_fl_square_metres() * 1.0) / square_m_per_litre; | |
required_paint = required_paint * coats as f64; | |
let mut sopl = String::new(); | |
let mut sopc = " is".to_owned(); | |
if (required_paint > 0.0 && required_paint < 1.0) || required_paint > 1.0 { | |
sopl = "s".to_owned(); | |
} | |
if coats > 1 { | |
sopc = "s are".to_owned(); | |
} | |
println!("\nRoom floor area is {:.1} square metres (w{:.1}m x h{:.1}m) and requires {:.1} litre{} of paint.", | |
room.get_fl_square_metres(), room.get_width(), room.get_height(), required_paint, sopl); | |
println!("The volume of the room is {:.1} cubic metres (w{:.1}m x l{:.1}m x h{:.1}m).", | |
room.get_vol_cubic_metres(), room.get_width(), room.get_length(), room.get_height()); | |
println!("Coverage is {:.1} square metres per litre and {} coat{} applied.", | |
square_m_per_litre, coats, sopc); | |
exit(0); | |
} | |
fn display_usage(program: &str) { | |
println!("Utility to calculate the volume of paint needed to"); | |
println!("paint a room of supplied dimensions."); | |
println!("\nUsage: {} [options]", program); | |
println!("\nOptions are:"); | |
println!("-a | --advanced: Allow advanced input such as paint coverage in square metres per litre."); | |
println!("-h | --help: Display this usage information and exit."); | |
exit(0); | |
} | |
fn main() { | |
let cli = CliOptions::new("paintcalc"); | |
let program = cli.get_program(); | |
let mut square_m_per_litre: f64 = 10.0; | |
let mut coats: u64 = 1; | |
let mut advanced = false; | |
if cli.get_num() > 1 { | |
for a in cli.get_args().iter() { | |
match a.trim() { | |
"-h" | "--help" => display_usage(&program), | |
"-a" | "--advanced" => advanced = true, | |
_ => continue, | |
} | |
} | |
} | |
println!("Calculating paint needed..."); | |
let width = parse_unit(&get_input("Room width (m)")); | |
let length = parse_unit(&get_input("Room length (m)")); | |
let height = parse_unit(&get_input("Room height (m)")); | |
let room = Room { width: width, length: length, height: height }; | |
if advanced { | |
square_m_per_litre = parse_unit(&get_input("Paint coverage in square metres per litre")); | |
coats = parse_unit(&get_input("How many coats")) as u64; | |
} | |
calculate_paint_needed(square_m_per_litre, &room, coats); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment