Skip to content

Commit 5c7720f

Browse files
committed
feat: view all txs
1 parent 2494c44 commit 5c7720f

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

Diff for: src/main.rs

+35-28
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,71 @@ use split::*;
55

66
fn main() {
77
let mut all_users: Vec<User> = Vec::new();
8-
let mut all_transaction = Transactions::new();
8+
let mut all_transactions = Transactions::new();
99

1010
loop {
11-
let options: Vec<&str> = vec![
11+
let options = vec![
1212
"1. Create Group",
1313
"2. Add Expense",
1414
"3. Split Bill",
15-
"4. Exit",
15+
"4. View all Transactions",
16+
"5. Exit",
1617
];
1718

1819
let selected_prompt: Result<&str, InquireError> =
19-
Select::new("Please Select one Option", options).prompt();
20+
Select::new("Please select one option", options).prompt();
2021

2122
match selected_prompt {
22-
Ok(choice) => {
23-
handle_choice(choice, &mut all_users, &mut all_transaction);
24-
}
23+
Ok(choice) => handle_choice(choice, &mut all_users, &mut all_transactions),
2524
Err(_) => println!("There was an error, please try again"),
2625
}
26+
println!();
2727
}
2828
}
2929

30-
fn handle_choice(choice: &str, all_users: &mut Vec<User>, all_transaction: &mut Transactions) {
30+
fn handle_choice(choice: &str, all_users: &mut Vec<User>, all_transactions: &mut Transactions) {
3131
match choice {
3232
"1. Create Group" => create_group(all_users),
33-
"2. Add Expense" => add_expense(all_users, all_transaction),
34-
"3. Split Bill" => split_bill(all_transaction),
35-
"4. Exit" => handle_exit(),
33+
"2. Add Expense" => add_expense(all_users, all_transactions),
34+
"3. Split Bill" => split_bill(all_transactions),
35+
"4. View all Transactions" => view_all_expenses(all_transactions),
36+
"5. Exit" => handle_exit(),
3637
_ => println!("Invalid choice!"),
3738
}
3839
}
3940

4041
fn create_group(all_users: &mut Vec<User>) {
41-
let no_of_people = Text::new("Enter the number of people ?").prompt();
42+
let no_of_people = Text::new("Enter the number of people:").prompt();
4243
match no_of_people {
4344
Ok(no_of_people) => {
4445
let mut no_of_people = no_of_people.parse::<u8>().unwrap();
4546
if no_of_people > 0 {
4647
while no_of_people > 0 {
47-
let username = Text::new("Enter the User Name ").prompt();
48+
let username = Text::new("Enter the user name:").prompt();
4849
match username {
4950
Ok(username) => {
5051
let user = User::create_user(&username);
5152
all_users.push(user);
5253
}
53-
Err(_) => println!("Error in create user"),
54+
Err(_) => println!("Error in creating user"),
5455
}
5556
no_of_people -= 1;
5657
}
5758
}
5859
}
59-
Err(_) => println!("Error occured while taking no of peopel"),
60+
Err(_) => println!("Error occurred while taking number of people"),
6061
}
6162
println!("------------------Group Created----------------------");
6263
}
6364

64-
fn add_expense(all_users: &mut Vec<User>, all_transaction: &mut Transactions) {
65+
fn add_expense(all_users: &mut Vec<User>, all_transactions: &mut Transactions) {
6566
let mut payer_list: Vec<&str> = Vec::new();
6667
for user in all_users.iter() {
6768
payer_list.push(&user.name);
6869
}
6970

7071
let selected_user: Result<&str, InquireError> =
71-
Select::new("Please select the payer of Expense ?", payer_list.clone()).prompt();
72+
Select::new("Please select the payer of expense:", payer_list.clone()).prompt();
7273

7374
match selected_user {
7475
Ok(selected_user) => {
@@ -78,36 +79,42 @@ fn add_expense(all_users: &mut Vec<User>, all_transaction: &mut Transactions) {
7879
.collect();
7980

8081
let giver_name: Result<&str, InquireError> =
81-
Select::new("Please select the givers of Expense", giver_list.clone()).prompt();
82+
Select::new("Please select the givers of expense:", giver_list.clone()).prompt();
83+
8284
match giver_name {
8385
Ok(giver_name) => {
84-
let amount = Text::new("Enter the Amount of Expense").prompt();
86+
let amount = Text::new("Enter the amount of expense:").prompt();
8587
match amount {
8688
Ok(amount) => {
8789
let amount = amount.parse::<u64>().unwrap();
8890
let from = User::create_user(selected_user);
8991
let to = User::create_user(giver_name);
9092

9193
let tx = Transaction::new(from, to, amount);
92-
all_transaction.add(tx);
94+
all_transactions.add(tx);
9395
}
94-
Err(_) => {}
96+
Err(_) => println!("Error in entering amount"),
9597
}
9698
}
97-
Err(_) => {}
99+
Err(_) => println!("Error in selecting giver"),
98100
}
99101
}
100-
Err(_) => {}
102+
Err(_) => println!("Error in selecting payer"),
101103
}
102104
println!("Added Expense...");
103105
}
104106

105-
fn split_bill(all_transaction: &mut Transactions) {
106-
println!("Splitting Bill...");
107-
let seletted_tx = all_transaction.split_bill();
108-
seletted_tx.display();
107+
fn split_bill(all_transactions: &mut Transactions) {
108+
println!();
109+
println!("Split Wise Bill ");
110+
let selected_tx = all_transactions.split_bill();
111+
selected_tx.display();
109112
}
113+
fn view_all_expenses(all_transactions: &mut Transactions) {
114+
all_transactions.display();
115+
}
116+
110117
fn handle_exit() {
111-
println!("Exiting CLI SUI");
118+
println!("Exiting CLI...");
112119
exit(0);
113120
}

Diff for: src/split.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ impl Transactions {
4646
}
4747

4848
pub fn display(&self) {
49+
println!("{0: <10} | {1: <10} | {2: <10}", "From", "To", "Amount");
4950
for i in &self.transactions {
5051
println!(
51-
"------> {} pays : {} amount to {}",
52+
"{0: <10} | {1: <10} | {2: <10}",
5253
i.from.name, i.amount, i.to.name
5354
);
5455
}

0 commit comments

Comments
 (0)