View the step-by-step solution to:

Hello, I am in a 100 Java course and I am having issues with this project I am working on.

Hello, I am in a 100 Java course and I am having issues with this project I am working on.I have the code that I have been making at the bottom,but It might be very wrong because I am getting a lot of errors and I am very confused with what I am doing. I hope this is enough information for you to help me, if you need anything else I am not sure how this whole tutoring thing works, first time trying it, but I bet there is a chat system we can go through and talk easily to each other.


I am required to construct a Cost Split Application in java.

Overview of the project

The program allows the user to create expenses for a group. a group includes a number of members that share the cost of a group activity (e.g. road trip, household, vacation, etc.).

There is just one user who can use the app and add expenses. When the user adds an expense, the user should provide the id of a person who paid for the cost, and the amount paid.

The expenses should be saved in a 2-dimensional array, where rows and columns represent group members. For example the expenses for a group of 4 people, will be maintained in a 4 * 4 array of type double. when and expense is added, the array should be updated. For example, if person 2 paid $100 for the group of 4 people, person 0, 1, and 3 should owe $25 each to person 1. Hence, if the expenses array is called expenses, expenses[0][1] expenses[2][1] and expenses[3][1] should be all incremented by 25. in a general term, expenses[i][j] represents the amount that person i owes person j.

The user should have the options to do the following tasks at each run of the program:

  • Load expenses from a previously saved text file.
  • Add an expense (paid by a person and split equally among group members)
  • Add an expense (paid by a person and split equally between the person and another member)
  • Add an expense (paid by a person and split by percentage between the person and another member)
  • settle expense (between any two members)
  • Get total cost owed to a person
  • Get the members list of the group (with id and name)
  • Get the payment due report
  • Save expenses into the existing file or a new file.

To get more familiar on how a Cost Split App works see: http://www.costsplitapp.com/

 (Links to an external site.)


Links to an external site.

 

Implementation

Required components:

  • a class called Group that stores the data about the group expenses, and provides methods that can add expenses or generate reports.
  • a class called Person that stores data about a group member (a person) and provides methods for storing data or returning data about the person.
  • a main class that performs the main tasks of the project including
  • reading and writing the expenses from and into a text file.
  • providing menu options for the user to choose a task to perform
  • construct a Group object
  • create member objects (Person) and add them to the Group object
  • call appropriate Group object methods to add expenses to the group
  • call appropriate Group object method to settle an expense between any two group members
  • call appropriate Group object methods to generate payments due report
  • save group members and expenses to a text file

Description of Group and Person Classes:

Person
- id: int
- name: String
+ Person(_name: String)
+ setID(_id: int): void
+ getName(): String
+ getID(): int



Group
- MAX_SIZE: int
- count: int
- members: Person[]
- expenses: double[][]
+ Group()
+ addMember(p: Person): void
+ addExpense(PersonID: int, amount: double): void
+ addExpense(Person1ID: int, Person2ID: int, amount: double): void
+ addExpense(Person1ID: int, Person2ID: int, amount: double, percentage: int): void
+ settle(Person1ID: int, Person2ID: int, amount: double): void
+ getPayableTo(PersonID: int): double
+ getMembers():Person[]
+ getCount(): int
+ getExpenses():double[][]
+ setExpenses(exp: double[][]): void
+ generatePaymentsDue(): String

Deliverables

You will complete the project in two phases:

Phase I:

  • The two classes (Person and Group) should be partially implemented. The implementation should be completed for the methods highlighted in green in the UML diagrams above. Other method implementations can be left blank but the method headers and brackets can be present.
  • A main (test) class should be implemented with the following features:
  • a method called loadExpenses that reads the expenses array from a text file and and copy them into the Group expenses array. following is a suggested header:
  • public static void loadExpenses(Group g, String expensesFileName) throws IOException
  • a method called saveExpenses that writes the Group expenses into the text file. following is a suggested header:
  • public static void saveExpenses(Group g, String expensesFileName) throws IOException
  • a method called menu that displays the menu options to the user and reads from input an integer and returns it as one of the 7 menu options. Following is the suggested options:

Select an option:

1. add expense (paid by a person and split equally among group members)

2. add expense (paid by a person and split equally between the person and another member)

3. add expense (paid by a person and split by percentage between the person and another member)

4. settle expense (between any two members)

5. get total cost payable to a person

6. print members

7. get the payment due report

8. save expenses

any other number to quit

  • For this phase, the menu options 6 and 8 should be handled, which means if the user enters one of these numbers, the program should perform the appropriate task. Other options should be implemented in the final phase of the project, but the user should be able to choose those options regardless (it just won't work)
  • Note: in the addMember method of class Group, when a person is added to the group, a new id should be created (last id + 1), and then should be assigned to the person's id.

followings is a sample run of the first phase of the project:

Please enter the file name to load: roadtrip.txt
Load expenses from input file? (y/n) y

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
6
0: John
1: Alex
2: Matt
3: Jane

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
8
Save expenses in the same file? (y/n) y
expenses saved to roadtrip.txt

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
9
Bye!

The above test is based on an input text file provided here:  roadtrip.txt

I will test your program based on this file. If everything works well, your program should produce the exact same output as above. 

The text file starts with the number of group members (in this case 4), followed by the member names (4 names), and an n * n (in this case 4 * 4) matrix of expenses.

Your program first asks the user for the file name to load, then asks if the expenses should also be loaded from the file. If n is selected, the expense matrix would not be loaded and set to the group expenses. (the expenses matrix will include all 0s).

When the user decides to save the updated expenses (perhaps to load in the next run), the program asks if the user wants to override the current file. If y is selected, (in this case, I chose to override the same file); then the members count, members list, and updated expenses matrix (2-dimensional array) is saved in the same structure in the same file (the old data is replaced by new data).

Phase II (final):

The rest of the methods should be implemented in this phase. The corresponding menu options should also be working. 

following is a sample run of the program:

Please enter the file name to load: roadtrip.txt
Load expenses from input file? (y/n) y

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
6
0: John
1: Alex
2: Matt
3: Jane

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
1
Enter the id of the person: 3
Enter the amount spent: 215.48
215.48 by Jane added to the group expenses!

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
3
Enter the id of the lending person: 2
Enter the id of the borrowing person: 0
Enter the amount spent: 80
Enter the percentage of your share: %25
60.00 by Matt lent to John added to the expenses!

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
5
Enter the id of the person: 3
group owes 761.61 to Jane

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
7

John to Alex $175.0
John to Matt $60.0
John to Jane $328.87
Alex to Jane $153.87
Matt to Jane $278.87

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
8
Save expenses in the same file? (y/n) n
Enter the new file name: roadtrip_updated.txt 
expenses saved to roadtrip_updated.txt

Select an option: 
1. add expense (paid by a person and split equally among group members)
2. add expense (paid by a person and split equally between the person and another member)
3. add expense (paid by a person and split by percentage between the person and another member)
4. settle expense (between any two members)
5. get total cost payable to a person
6. print members
7. get the payment due report
8. save expenses
any other number to quit
9
Bye!


The above test is based on an input text file provided here:  roadtrip.txt

 4

John

Alex

Matt

Jane

0.0

175.0

0.0

275.0

0.0

0.0

0.0

100.0

0.0

0.0

0.0

0.0


I will test your program based on this file. If everything works well, your program should produce the exact same output as above. 

The text file starts with the number of group members (in this case 4), followed by the member names (4 names), and an n * n (in this case 4 * 4) matrix of expenses.

Your program first asks the user for the file name to load, then asks if the expenses should also be loaded from the file. If n is selected, the expense matrix would not be loaded and set to the group expenses. (the expenses matrix will include all 0s).

When the user decides to save the updated expenses (perhaps to load in the next run), the program asks if the user wants to override the current file. If n is selected, the program asks for a new file name (in this case, I entered roadtrip_updated.txt); then the members count, members list, and updated expenses matrix (2-dimensional array) is saved in the same structure in the new file.

here's the content of the updated file: (optional: I made the program save the payments due report at the end of the text file)


Note: sometimes for example, John and Jane may both owe money to each other. In this case, if John owes Jane $200 and Jane owes John $150, simplify the payment due in the report and just add one entry as: John to Jane: $50

put all the files (including .java and .txt files) in a folder


This is roadtrip.txt the file that was given to me :

4

John

Alex

Matt

Jane

0.0

175.0

0.0

275.0

0.0

0.0

0.0

100.0

0.0

0.0

0.0

0.0





This is what I have for Testp1.java which is what I called the java file that is running everything:

import java.util.Scanner;

import java.io.*;

import java.util.ArrayList;

public class Testp1

{

  public static void main(String[] args) throws Exception

  {

     

    Group g = new Group(); 

 

     

    int ans;

    int i; 

    System.out.printf("Please enter the file name to load: ");

    Scanner keyboard = new Scanner(System.in);

    String filename = keyboard.nextLine();

    Scanner input = new Scanner(System.in);

    System.out.printf("load info from input file? yes or no:n");

    String under = input.next(); //asks the user if they want to load exspenses from the file they inputed

     if(under.equals("yes"))

    {

     // Testp1.loadExpenses();

    }

    System.out.printf("Select an option:n");

    

    Testp1.displayMenu(); //displays the menu

     

    for(i = 1; i > 0;i++){ //for loop doesn't help here. use while loop and repeat it until a quite option (a number out of 1-8 range) is selected by the user

     //in every iteration of this loop, display the menu again (by calling menu() function) 

    ans = 0;  

    ans = input.nextInt(); //asks the user what he wants to answer from the menu   

    //use switch/case structure instead of if/if else

    if(ans == 1) //if the user says he wants to answer 1 on the menu it uses this if statement

    {

   /*   System.out.printf("Enter the id of the persont");

      System.out.printf("Enter the amount spentt");

      Testp1.displayMenu(); //will display the menu again

      System.out.printf("___ by ____ added to the group expenses!" 

      System.out.printf("Select an option");

  */  

  }

     

    else if(ans == 2) //if the user says he wants to answer 2 on the menu it uses this if statement

    {

    

    }

     

    else if(ans == 3) //if the user says he wants to answer 3 on the menu it uses this if statement

    {

    

    }

    

   else if(ans == 4) //if the user says he wants to answer 4 on the menu it uses this if statement

    {

   

    }

    

   else if(ans == 5) //if the user says he wants to answer 5 on the menu it uses this if statement

    {

   

    }

     

    else if(ans == 6) 

    {

    Scanner s = new Scanner(new File(filename)); //you wanna run this only once (creating an array of members)

    //because a usre may select this option several times, you don't wanna add the members to the array again

    ArrayList<String> list = new ArrayList<String>();

      while (s.hasNext())

      {

      list.add(s.next());

      }

    s.close(); //when the array is created, pass it to the Group setMembers method.

     for (int index = 1; index < (5); index++)

      System.out.println(list.get(index));

    //in this part, just call getMembers from the Group object (g) to get the array of members. thern print hte members

    }

    else if(ans == 7) 

    {

   

    }

     

    else if(ans == 8) 

    {

    System.out.printf("save exspenses in the same file? yes or no:n");

    String over = input.next(); //prompts the user to question in the print statement above

     

    if(over.equals("yes"))// if they answer yes it will say that it was saved to the file name they inputed and will overwrite the data on it

      {

      System.out.println("expenses saved to " + filename);

     // Testp1.saveExpenses();

      }

    Testp1.displayMenu(); //will display the menu again

    System.out.printf("Select an option");

    }

     

     

    else //if the user inputs anything other than 1-8 then it will exit the program

    {

      System.out.printf("Program exited!n");

      i = -1;

    }

   } }

   

   public static void displayMenu() //sets up the method to display the menu when prompted

   {

   System.out.printf("1. add expense (paid by a person and split equally among group members)n");

   System.out.printf("2. add expense (paid by a person and split equally between the person and another member)n");

   System.out.printf("3. add expense (paid by a person and split by percentage between the person and another member)n");

   System.out.printf("4. settle expense (between any two members)n");

   System.out.printf("5. get total cost payable to a personn");

   System.out.printf("6. print membersn");

   System.out.printf("7. get the payment due reportn");

   System.out.printf("8. save expensesn");

   System.out.printf("any other number to exitn"); 

   }

    public static void loadExpenses(Group g, String expensesFileName) throws IOException //will load the exspences from the file roadtrip.txt

   {

   Scanner s = new Scanner(new File("roadtrip.txt"));

    ArrayList<String> list = new ArrayList<String>();

    while (s.hasNext()){

    list.add(s.next());

    }

    s.close();

     for (int index = 6; index < (10); index++)

     System.out.println(list.get(index));

   }

    public static void saveExpenses(Group g, String expensesFileName) throws IOException  //will save the exspences to the file roadtrip.txt

   { //you must get the expenses by calling getExpenses from th egroup object (in a fom of an array)

    PrintWriter outputFile = new PrintWriter("roadtrip.txt");

     

    //writing into file roadtrip.txt

    outputFile.println("4");

    outputFile.println("John");

    outputFile.println("Alex");

    outputFile.println("Matt");

    outputFile.println("Jane");

     

    //write the expenses you got from the group object into the file

// I tried to run these to input the expenses that are in group and save them back into the roadtrip,txt file, but it does not work

    outputFile.println(g.expense.get(5));

    outputFile.println(g.expense.get(6));

    outputFile.println(g.expense.get(7));

    outputFile.println(g.expense.get(8));

     

    outputFile.println(g.expense.get(9));

    outputFile.println(g.expense.get(10));

    outputFile.println(g.expense.get(11));

    outputFile.println(g.expense.get(12));

     

    outputFile.println(g.expense.get(13));

    outputFile.println(g.expense.get(14));

    outputFile.println(g.expense.get(15));

    outputFile.println(Group.setExpenses.expense.get(16));

    //closing the file

    outputFile.close();

     

    // g.setexpenses(g.expense.get(16));

   }

}

This is what I have for the Group.java file

import java.util.Scanner;

import java.io.*;

import java.util.ArrayList;

public class Group

{

  public static void main(String[] args)throws Exception

  {


  }

   private int MAX_SIZE;

   private int count;

   private Person mem = new Person();

   private Person[] members;

   private double[][] expenses;

    

    

   

    

   public Group()

   {

     MAX_SIZE = 0; 

     

     count = 0; 

     

     members = new Person[4]; 

     

     expenses = new double[4][4]; 

   }


  public Group(int MAX_SIZE,int count,Person[] members,double[][] expenses) 

  {

     

   }

  

  public void addMember(Person p) //this is used to add a member

  {

   members[count] = p;

    

   count++;

  }

   

  public void getMembers()throws Exception //this is used to get the members name

  {

     Scanner s = new Scanner(new File("roadtrip.txt"));

    ArrayList<String> Person = new ArrayList<String>(); //this makes a array list named Person to help store the names

    while (s.hasNext()){

    Person.add(s.next());

    }

    s.close();

     for (int index = 1; index < (5); index++) //this picks the 2nd to the 6th line of the txt file which happen to be the names

     System.out.println(Person.get(index));

   

  }


  public int getCount()

  {

   return count;

  }

   

   public double[][] getExpenses() //this is to get expenses made by the users

  {

   return expenses;

  }

   

   public void double[][] setExpenses()throws Exception //this is used to set the expenses into the text file

  {

   

    Scanner e = new Scanner(new File("roadtrip.txt"));

    ArrayList<String> expense = new ArrayList<String>(); //this makes a array list named expense will get all the expenses in the txt file and set them to a name

    while (e.hasNext()){

    expense.add(e.next());

    }

    e.close();

     for (int index = 5; index < (21); index++) //this picks the 2nd to the 6th line of the txt file which happen to be the names

     System.out.println(expense.get(index));

  }

}


return expense;


/*  expense1 = expense.get(5); this is another idea i had to move the exspenses over but it did not work at all 

     expense2 = expense.get(6); 

     expense3 = expense.get(7);

     expense4 = expense.get(8);

     expense5 = expense.get(9);

     expense6 = expense.get(10);

     expense7 = expense.get(11);

     expense8 = expense.get(12);

     expense9 = expense.get(13);

     expense10 = expense.get(14);

     expense11 = expense.get(15);

     expense12 = expense.get(16);

     expense13 = expense.get(17);

     expense14 = expense.get(18);

     expense15 = expense.get(19);

     expense16 = expense.get(20);

    */ 



This is what I have for the Person.java file

  public class Person

{

  public static void main(String[] args)

  {

  

  }


    private int id;

    private String name;

  

   public Person()

   {

     id = 0; //will set the attribute id to 0 so if no id is given it will still display something

     name = "None"; //will set the attribute name to none so if no name is given it will still display something

   }


  public Person(int iden,String n) 

    {

    name = n;

    id = iden;

    }

   

  public void setID(int iden) //this is used to set the variable ID to the names

  {

    id = iden;

  }

   public String getName() //this is used to get the varaible name from when it was retrieved from the file

  {

   return name;

  }

  public int getID() //this is used to get the varaible ID from when it was set to a name earlier

  {

   return id;

  }

}

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question