0

I am working on a budget app for practice. And I am using an array of objects and want to be able to iterate through an array and only print out an element of the same name once, to create a menu for a user to choose from, without hard coding it in.

I want my output to look like this:

  1. Groceries
  2. Restaurants
  3. Other expenses.
  4. Not listed.

So I want to avoid having the "expense type" being listed more than once. Make sense? Here is the code that I have so far:

package homebudget;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;

/**
*
* @author Derek
*/
public class HomeBudget 
{
//Features to add:
//Reminder 2 days before an auto deduction




public static void main(String[] args) throws Exception
{
    // TODO code application logic here

List<Bills> deductions = new ArrayList();
String billName, deductDate, resp;
double amount, totalAmount;
int cmd, year, month, date;
totalAmount = 0;

List<Spending> expenses = new ArrayList();
String type;

List<Income> deposits = new ArrayList();
String incomeType;

String fname = JOptionPane.showInputDialog("Enter the name of the budget file, none if no file");
    if (fname.compareTo("none") !=0)
    {
        FileInputStream ist = new FileInputStream(fname);
        ObjectInputStream ifile = new ObjectInputStream(ist);
        deductions = (ArrayList<Bills>) ifile.readObject();

    }
    boolean done = false;
    while(!done)
    {
        resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1:Add a new deduction\n"  //think its done
                + "\t2:Add a new expense\n"  //this is done, but could be made better wit
                + "\t3:Add a deposit\n"  //This is done
                + "\t4:Deduction options\n"  
                + "\t5:Expense Options\n"  
                + "\t6:Total balances in bank\n"
                + "\t7:quit");
        cmd = Integer.parseInt(resp);
        switch(cmd)
        {
            case 1:

            billName = JOptionPane.showInputDialog("Enter the name of the bill:");
            deductDate = JOptionPane.showInputDialog("Enter the deduct date:");
            resp = JOptionPane.showInputDialog("Enter the deduct amount");
            amount = Double.parseDouble(resp);

            Bills d = new Bills(billName, deductDate, amount);
            deductions.add(d);
            break;

            case 2:
            //Give the option to add new spending occurence.
            //Give option to choose from array of spending types.
            resp = JOptionPane.showInputDialog("Enter a command from: \n" 
                + "\t1: Create a new expense\n"  //done
                + "\t2: Choose from expense list\n"
                + "\t3:quit");
            int cmd2 = Integer.parseInt(resp);
            switch (cmd2){
                case 1:

                 type = JOptionPane.showInputDialog("Enter the type of the expense:"); 

                    resp = JOptionPane.showInputDialog("Enter the amount of the expense:");   
                    amount = Double.parseDouble(resp);
                    resp = JOptionPane.showInputDialog("Enter the year of the expense:");
                    year = Integer.parseInt(resp);
                    resp = JOptionPane.showInputDialog("Enter the month of the expense:");
                    month =  Integer.parseInt(resp);
                    resp = JOptionPane.showInputDialog("Enter the date of the expense:");
                    date =  Integer.parseInt(resp);
                    Spending s = new Spending(amount, type, year, month, date);
                    expenses.add(s);    


                case 2:


                Iterator<Spending> spendIter = expenses.iterator();

                boolean found = false;
                while(!found && spendIter.hasNext())
                {
                  s = spendIter.next();
                  if(s.getType().compareTo(type) == 0)
                  {
                    JOptionPane.showInputDialog("Choose from list of expenses below:" + s.getType());

                    found = true;
                  }
                }
                  if(!found)
                  {
                      System.out.println("No expenses exist.");
                  }
                    //This is the part that I have no idea how to code.
                    //I want a list of expenses to be shown where the user can select a     number and that 
                    //expense is selected.   


            break;
JP Hochbaum
  • 637
  • 4
  • 15
  • 28
  • See [In Java, how can I test if an Array contains a certain value?](http://stackoverflow.com/questions/1128723/in-java-how-can-i-test-if-an-array-contains-a-certain-value) – Romojr50 Aug 11 '14 at 20:04
  • 1
    Use a Set to keep track of what elements you've already seen. – Hot Licks Aug 11 '14 at 20:09

2 Answers2

0

Put the elements of your array into a hashtable and then iterate over its elements. The hashtable makes sure that the values corresponding to duplicate keys are put in the same place, thus in essence removing duplicates implicitly. Following is a simple demonstration of it.

int[] a = {1, 1, 2, 1, 2, 3, 1, 2, 3, 4};
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < a.length; i++) {
    map.put(a, a);
}
for (Integer x : map.keySet()) {
    System.out.println(map.get(x));
}
Debasis
  • 3,680
  • 1
  • 20
  • 23
0

You could maintain the set of the expense types, which gives you the surety of taking only the new types, not the one it already contains.

Dheeraj Arora
  • 608
  • 4
  • 13
  • And so I can also use numerous values for a set? For expenses I was planning on having "amount", and "date" for values associated with expenses. – JP Hochbaum Aug 11 '14 at 20:55
  • I didnt get your question exactly. Numerous values of set means different sets for "amount" and "date" as well? – Dheeraj Arora Aug 11 '14 at 21:19
  • Basically the user is going to select an "expense type" and also put in an amount and a date for the expense. So there will be two values for each "expense type", the amount and the date. – JP Hochbaum Aug 12 '14 at 13:25
  • Okay looks like my answer should be this: http://stackoverflow.com/questions/8229473/hashmap-one-key-multiple-values – JP Hochbaum Aug 12 '14 at 13:30