I've implemented in-app-billing v3 and I'd like a way to keep some sort of table which contains 3 columns
(String) Name of product, (String) sku, (boolean) true/false
What is the best way to do this? The best way would (in my opinion) allow me to keep a static easily readable list of values where the first two were strings, the third a boolean. At the start all the values for the boolean column were false, but as I query purchases I am able to easily reset the value.
Just to add,
I'm against SQLite databases for the reason that they're just too easy to hack. I'd rather create it in code and thus I'm debating between an ArrayList within an arraylist or matrices or something like the following:
private static class CatalogEntry {
public String sku;
public String name;
public CatalogEntry(String sku, String name) {
this.sku = sku;
this.name = name;
}
}
private static final CatalogEntry[] CATALOG = new CatalogEntry[] {
new CatalogEntry("android.test.purchased", "Item1"),
new CatalogEntry("android.test.canceled", "Item2"),
new CatalogEntry("android.test.refunded", "Item3"),
new CatalogEntry("android.test.item_unavailable", "Item4")
};
Is there a way to reset values in the CATALOG array if I add a third column, otherwise I'm considering using both that and standard arraylists.
Use sqlite with FOUR columns:
sku
name
purchaseDate (or null if not purchased)
md5Hash
When a purchase is made, you just concatenate the sku, name, purchaseDate, device-id, and a PSK, make an md5Hash and store it along with the other data in the row. When you check the contents of the table, you compare the md5 stored against the data, and you can verify whether the purchase was valid, or the table has been tampered with. You can even let the user back up the purchase table to sdcard (or use Android backup).
Related
I have a small java project which tries to match a given customer record with one in a database based on closest match.
A given customer has a unique combination of String variables-
Name, Address1, Address2
If some data is missing then the default for a given variable is "ALL".
The code retrieves all the database records and creates a map of customer records where customerKey is a unique combination of name, address1, address2.
CustomerKey is a class of these string variables with their own getters/setters and hashcode (from apache HashCodeBuilder).
CustomerData is just a dump of all the data in the record for that customer in the database
The code works as normal so far
String name = "Harry";
String address1 = "hello street";
String address2 = "hello town";
map<customerKey, CustomerData> lookupMap = getDataFromDB();
CustomerData data = lookupMap.get(new CustomerKey(name, address1, address2));
if (data==null){
data = lookupMap.get(new CustomerKey("ALL", address1, address2));
}
if (data==null){
data = lookupMap.get(new CustomerKey("ALL", "ALL", address2));
}
if (data==null){
data = lookupMap.get(new CustomerKey("ALL", "ALL", "ALL"));
}
return data
I want to be able to add wildcards to the matching process where it is applied to the names. I can find out the regex for what i want e.g. .*arry and add that to the name of a particular record.
And i know that if i want to use wild card comparisons of strings then i need to use .matches rather than .equals (e.g. "harry".matches(".*arry").
But i'm having some difficulty moving forward from there.
Does anyone have some advice?
I have these class on my package:
1. Class Goods with attributes (name, price, id) //Goods id is unique, but the name can be the same
2. Class Storage with attributes (Goods gArray[3])
3. Class Store with attributes (name, ArrayList<Storage>) //Store name is unique
4. Class StoreSystem with attributes (ArrayList<Store>)
I want to insert Goods into Storage which belong to certain Store. I already succeed in inserting the Store to ArrayList, but haven't found the way to insert the Goods.
Here's the code for adding the store:
public String addStore(String storeName) {
String output = "";
if(storeCheck(storeName)) { //storeCheck used to check whether the store name exist/not.
output = "store already exist!";
}
else {
Store s1 = new Store();
Storage st1 = new Storage();
s1.setStoreName(storeName);
s1.setStorageList(null);
st1.setGArray(null);
listOfStore.add(s1);
listOfStorage.add(st1);
output = "Store added";
}
return output;
}
You could create a method in the Storage class that:
either takes an index of the array and Goods as arguments and places the Goods in the array at that index (if you're interested in full control of where everything goes)
or just Goods as argument and decides where to put them internally (if you do not care which index in the array the goods go to)
Does that make sense?
I believe is just this, using list as intermediate:
public void addGoods(Goods g) {
List<Goods> storageList = Arrays.asList(this.getGoods());
storageList.add(g);
this.setGoods(storageList.toArray());
}
get and set as usual, and you will need to control the size.
Make a POJO called Storage with the field Goods gArray[3]. Then have a method to add Storage in Store class (it should be as simple as using storeArrayList.add(new Storage()); and pass 3 parameters of type Goods to it). And there you go. You have a Storage and Goods in Storage.
Now properly looking up for a particular Storage in a Store without assigning an index to a particular Storage will be a bit of a bother. Ideally, every Storage object should have an id field.
The code will look something like:
The Store add method:
<return-type> addToStore(Goods[] goods) {
storageArray.add(new Storage(goods));
}
And then the Storage class will look something like:
class Storage {
private final Goods gArray[3];
public Storage(final Goods gArray) {
this.gArray = gArray;
}
// getters
}
I want to write a generic function that accepts two objects of same entity class and compares the fields that are different and returns List of all the changes made to particular fields along with time.
One among the many entity classes would be say Member as follows
public class Member {
String firstName;
String lastName;
String driverLicenseNumber;
Integer age;
LocalDateTime timestamp;
}
In the DB, I have a table called member_audit that gets populated with old data whenever there is a change in member table using triggers (Similarly for other entities).
The List of resource for each of the entity I would be returning is something like
public class MemberAuditsResource {
private String field;
private LocalDateTime on;
private String changeType;
private String oldValue;
private String newValue;
}
I can only think of writing a function for each entity separately like this
private List<MembeAuditsResource> memberCompare(Member obj1, Member obj2) {
//Compare every field in both the objects using if else and populate the resource.
}
And then calling the above function to compare every pair of record in the entity_audit table.
The code would be very large to compare every field and multiplied by different entities.
Is there a better and efficient way?
If you extend the ideas to compare the object graph , it is not a trivial problem. So, the efficient way is not to re-inventing the wheel but use an existing library such as JaVers :
Member oldMember = new Member("foo" ,"chan" ,"AB12" , 21 ,LocalDateTime.now());
Member newMember = new Member("bar" ,"chan" ,"AB12" , 22 ,LocalDateTime.now());
Diff diff = javers.compare(oldMember, newMember);
for(Change change: diff.getChanges()) {
System.out.println(change);
}
Then , you can get something like:
ValueChange{ 'firstName' changed from 'foo' to 'bar' }
ValueChange{ 'age' changed from '21' to '22' }
Convert both object to a Map using JSON objectMapper.convertValue method. Then you can easily compare the keys/values of the two maps and create a list of differences.
Hi this is my first time using this because I am confused as to how I should go about this problem.
I have a spreadsheet which has multiple columns such as "house owner names", "Address", "price" etc. All of the columns have 12 values in them relating to 12 individuals each with their address and other such details regarding their property.
I need to create a program where if I enter a certain price range, the program sorts through the spreadsheet and only displays the results that fall within the price range that the user enters.
I first thought of using multiple one-dimensional arrays in parallel, but I am not sure if this is the correct way to do such a thing, also I do not know if it is possible to to search through arrays for specific ranges and then have it display to the console.
In this instance I would say your spreadsheet is acting as your database.
Rather than arrays, I would read the spread sheet into instances (objects) of a class, such that each class represents a row in your spreadsheet, and then put these instances into an ArrayList
You should then be able to "search" the ArrayList.
The object oriented way to handle this is to create a Java object with the 12 column names. Since you only gave us 3 column names, I created this Java objecvt with the 3 names.
public class HomeOwner {
private final String name;
private final String address;
private final double price;
public HomeOwner(String name, String address, double price) {
this.name = name;
this.address = address;
this.price = price;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public double getPrice() {
return price;
}
}
You can now create a List of HomeOwner instances, and search the List by price or any of the other column names.
So i am trying to sort through a large amount of data in an CSV. file. The file includes a set amount of information for companies, but there are 1000s of companies. For example, I might need to go through 1000 companies, be able to acquire their annual earnings, current stock value, CEO, ect.. each company will have the same information provided (same number of commas but different char lengths), but as the file is a CSV. the company name and information is all separated by commas.
currently i am splitting the csv file into an array via the commas between information. But i want to be able to keep the information together with companies and be able to specify, call and, sort by the given information and company names. But because i have already separated the information via the commas its all listed out already in a listarray.
So is it possible to specify, on a mass scale, that every 15 commas (or splits in the listarray) should be joined back together?? This way each part of the listarray is a separate company. Or is there another way to separate the data so that the information doesnt get split up?
note: there is no similarities in the csv file that would allow me to split information so that it splits after each companies information.
here is a sample of what one of the csv files may look like.
"Tiffany & Co. Com",964270,"+0.81","1/14/2014",88.97,93.64,"87.795 - 88.97""Asia Pacific Fund",20700,"+0.04","1/14/2014",10.23,11.37,"10.19 - 10.23""Anadarko Petroleu",4236380,"+2.47","1/14/2014",80.99,98.47,"78.40 - 80.99""Proto Labs, Inc. ",451984,"-0.18","1/14/2014",73.83,89.97,"71.00 - 73.83""Zuoan Fashion Lim",201560,"-0.02","1/14/2014",1.79,3.62,"1.71 - 1.79"
I would agree with converting each row of CSV into Java object.
But traditional parsing mechanism is too verbose for me and I might need to handle too many conditions like comma in between quotes, new line character in a column with multiline description. So I suggest you use an existing awesome solution like supercsv.
I also have written a wrapper around it to make developer life easy.
QuickOCM will let you do this way.
Create a Company class
public class Company {
/* this specifies that
* it is a mandatory field in the csv,
* of header name "Company Name" and
* of type string.
* Header is the first line of the csv.
*/
#ImportField(mandatory = true, name = "Company Name", type = "String")
public String name;
#ImportField(mandatory = true, name = "Name of the CEO", type = "String")
public String ceoName;
}
You need public getter-setter or public fields, anything works
Create csv parser, a handler to handle each row, probably add to a list to sort, and then call parse
final List<Company> companies = new ArrayList<Company>();
csvParser.process(inputStream, Company.class, new RecordHandler<Company>() {
#Override
public void execute(Company imported, int rowNumber, Map supplementaryInfo) {
companies.add(imported);
}
});
Now you can sort the list with by using a sorted list implementation or use a comparator for the same.
For detailed info, you can look into QuickOCM page.
public class Read{
String original = "";
String company = "";
String otherValue = "";
public Read(String read){
//here Split the original string into the values
}
//public void getters and setters
}
Then make an array of Read Objects and sort them as you want
One idea would be to parse the CSV into objects and then sort those objects. The object would "know" how many fields it was made up of in the CSV and how to parse each field. Using the StringTokenizer to parse and a TreeMap to sort would look something like:
...
BufferedReader reader = new BufferedReader(new FileReader("somedata.csv"));
TreeMap<String, MyObject> map = new TreeMap<>();
String line = reader.readLine();
StringTokenizer tokens = new StringTokenizer(line,",");
while(tokens.hasMoreTokens()) {
MyObject obj = new MyObject(tokens);
//add the objects to the sorted map, where field1 is what we sort on
map.put(obj.field1, obj);
}
...
}
static class MyObject {
//would need the same number of fields as you want to group
String field1;
String field2;
//... so with 2 fields, input is field1,field2,field1,field2,...
MyObject (StringTokenizer input) {
this.field1 = input.nextToken();
this.field2 = input.nextToken();
}
}