0

The project that I am working on is an income and expense system where there will be three options: Add income and expense of a month Search income and expense by month Display the report of a year For the first one, I asked the user to input the income and expenditures of different categories and write to a text file. For the second one, I am unable to search according to the month in the text file. Would you please give me some suggestions? How do I store them in array?

import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    public class IncomeExpenseSystem {
        public static void main (String[]args) throws IOException{
            
            File summaryTxt = new File("summary.txt");
            
            
            // To create the menu
            Scanner  input = new Scanner(System.in);
            
            int choice = 0;
            
            while(choice!= -1) {
            System.out.println("Please choose an choice you would like to proceed with from the list below(type -1 to quit;");
            System.out.println("1. Add income and expense of a month");
            System.out.println("2. Search income and expense by month");
            System.out.println("3. Display the report of a year");      
            
            
            choice = input.nextInt();
            
          
    
            if (choice == 1) {          
                
                //ask month
                 System.out.println(" Add the month:");
                 String month = input.next();
                 input.nextLine();
                 
                 // input the income
                 System.out.println("Enter the Freelance Salary:");
                 double freelance_salary = input.nextDouble();
                 
                 System.out.println("Enter the Monthly Job Salary:");
                 double monthly_salary = input.nextDouble();
                 
                 System.out.println("Enter the Business Income:");
                 double business_salary = input.nextDouble();
                 
                 System.out.println("Enter the Rental Income:");
                 double rental_salary = input.nextDouble();
                 
                 double total_income = freelance_salary + monthly_salary + business_salary + rental_salary;
                 
                 //input the expenses
                 
                 System.out.println("Enter the expenses on food:");
                 double food_expense = input.nextDouble();
                 
                 System.out.println("Enter the expenses on transportation:");
                 double trans_expense = input.nextDouble();
                 
                 System.out.println("Enter the expenses on Entertainment:");
                 double enter_expense = input.nextDouble();
                 
                 System.out.println("Enter the expenses on Rent:");
                 double rent_expense = input.nextDouble();
                 
                 System.out.println("Enter the expenses on Tax:");
                 double tax_expense = input.nextDouble();
                 
                 System.out.println("Enter the expenses on Miscellaneous:");
                 double miscell_expense = input.nextDouble();
                 
                 double total_expenses = food_expense + trans_expense + enter_expense + rent_expense + tax_expense+ miscell_expense;
                 
                 double balance1 = total_income - total_expenses;
                 double balance =Math.abs(balance1);   //to remove negative sign
                 
                 //To check whether the expenditure is above or below limit with extra expense and saving respectively
                 if(balance <0) {
                     System.out.println("The expenditure is above limit with extra expense of " + balance);
                 }
                 else {
                     System.out.println("The expenditure is below limit with saving of " + balance);
                 }
                
                 createData( month,  freelance_salary,  monthly_salary,  business_salary,  rental_salary, total_income, food_expense, trans_expense, enter_expense, rent_expense, tax_expense, miscell_expense, total_expenses, balance, summaryTxt);
                }
            
            if (choice == 2) { 
                
                //Search the income and expenditure by month
                System.out.println("Enter the month that you want to search:");
                String search_month = input.next();
                
      
        
                 FileReader fr=new FileReader(summaryTxt);   //Creation of File Reader object
                 BufferedReader br=new BufferedReader(fr);  //Creation of BufferedReader object
                 String s=null;          
                 while((s=br.readLine())!=null)       //Reading the content line by line
                 {
    
                    System.out.println(s);
                       
                 }
                    fr.close();
                
                
                
                
            }
                 
                 
            }
            }
        
        
            
            static void  createData(String month, double freelance_salary, double monthly_salary, double business_salary,  double rental_salary, double total_income,double food_expense,double trans_expense,double enter_expense,double rent_expense,double tax_expense,double miscell_expense,double total_expenses,double balance, File summaryTxt)
            {
                try {
                    PrintStream ps = new PrintStream(new FileOutputStream(summaryTxt,true));
                    ps.print(month + " ");
                    ps.print(freelance_salary + " ");
                    ps.print( monthly_salary+ " ");
                    ps.print( business_salary+ " ");
                    ps.print( rental_salary+ " ");
                    ps.print(total_income + " ");
                    ps.print(food_expense + " ");
                    ps.print(trans_expense + " ");
                    ps.print(enter_expense + " ");
                    ps.print(rent_expense+ " ");
                    ps.print(tax_expense + " ");
                    ps.print(miscell_expense + " ");
                    ps.print(total_expenses + " ");
                    ps.print(balance + " ");            
                    ps.print(" \n");
                    ps.close();
                } catch (FileNotFoundException e) {
                    System.err.println(e);
                }
            }
        }
  • This is not an answer, but consider making your createData() method a class. A method should have no more than 3-4 parameters. This tip will make your life easier. Also, keep your methods as short as possible (4-5 lines max, maybe 10 if it is a very heavy method). – Ivan Dimitrov Oct 03 '20 at 16:06
  • Thank you @IvanDimitrov, I am just a beginner and I am confused about how to do it. I have not studied OOP. Would you please give me more tips? – louis drama Oct 03 '20 at 16:09
  • Get comfortable with using and creating classes, interfaces and enums by reading the book Effective Java by Joshua Bloch. That's 400 pages of tips there. Try to understand what you are reading before moving forward. Go through the Java tutorials (https://docs.oracle.com/javase/tutorial/) if you want a good OOP introduction. – Ivan Dimitrov Oct 03 '20 at 16:13
  • Thank youu @IvanDimitrov. I will consider your suggestion:)) – louis drama Oct 03 '20 at 17:20

0 Answers0