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:
- Groceries
- Restaurants
- Other expenses.
- 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;