user1337
user1337

Reputation: 21

Java ArrayLists - involves comparing

This concept I am about to explain seems little hard to discuss, but I will try to give as much details as possible.

So there 2 classes. One of them is for setting up the attributes for setter and getter and the other file is my main one where I am doing all of the comparisons.

The comparison I want to do is within the array, but there are too many attributes to account for. This is what it looks like currently:

[[Year, Month, type1, type2, name, $], [Year, Month, type1, type2, name, $], [Year, Month, type1, type2, name, $]]

Only two that is an integer is $ and year, rest are strings. Now that you have an idea on what kind of attributes I am playing with, here is a small snippet which I am using to gather those information from an Excel.

ArrayList<customType> workList = new ArrayList<customType>();
 for (int i = start; i <= end; i++) {
     Row r = sheet.getRow(i);
    ArrayList tmp = new ArrayList();
      tmp.setYear((int) row.getCell(0).getNumericCellValue());
      tmp.setMonth(row.getCell(1).getStringCellValue());
      tmp.setType1(row.getCell(2).getStringCellValue());
      tmp.setType2(row.getCell(3).getStringCellValue());
      tmp.setName(row.getCell(4).getStringCellValue());
      tmp.setPrice((int) row.getCell(5).getNumericCellValue());

    workList.add(temp);
 }

Ok, so I have a list of maybe 500 rows in the arraylist. They are sorted by Year and Month from the excel.

Ex:

So for the HaloReach game, you can see that it is missing a field in March and February. I want to back in to the list and add the year, month, type1, type2, name, but for the value put $0 Also, looking at Monopoly game, it didn't exist future or present, I want to copy an paste the stuff with $0 but for each month that I have listed.

Ex:

Could I maybe use a hash set for this? I've been trying this, but I can't get it done. I have like 500 lines of commented code lol.

The main reason I am doing this is so I can go create another arrayList that will hold a temporary arrayList of $$ for previous months. Example: This arraylist will only show the current Month:

  2011 Mar Game Racing Forza [60, 60, 0, 0] <-- from latest to oldest
  2011 Mar Game Shooter HaloReach [45, 0, 45, 45]
  etc.. 

If the name didn't show up in the list I want to add 0 for price..but I want to look at the current month. Is there a better approach to this? I can try to provide better explanation if needed.

Edit: I got it done without mysql database. It took me 6+ hours. thanks guys for the advice. Maybe next time I will try mysql approach.

Upvotes: 0

Views: 145

Answers (2)

jeff
jeff

Reputation: 4333

If I were you, I would start by breaking up my objects a little better. When you break your system into more logical components - they can really start doing the heavy lifting for you. Consider the following:

public class Game
{
  private String name;
  private String category;
  private Map<String, Double> pricesByDate;

  // Constructors, getters, setters

  public void addPrice(String monthYear, Double price)
  {
     pricesByDate.put(monthYear, price);
  }

  public Double getPrice(String monthYear)
  {
    // here, let the object do the work for you
    Double price = pricesByDate.get(monthYear);
    return price != null ? price : 0.0;
  }
}

So rather than worrying about filling in 0's everywhere, just make the object smart enough to report a 0 when no data is found.

Now change your parser to construct a list of Games.

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81674

An approach that would make vastly more sense would be to use a real database, like MySQL, which you can query, sort, and organize easily using SQL. This would lead to simple solutions for whatever you want to do with this data. The path you've chosen so far leads only to madness and despair.

Upvotes: 2

Related Questions