-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.pde
163 lines (158 loc) · 4.31 KB
/
Database.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
public class Database //encapsulates a database (file or mysql) to ease the use.
{
//***PARAMETERS***//
private int option; //file = 0, mysql = 1
private int loadSplit;
private ArrayList<Product>p;
private String filename;
private String splitter;
//MYSQL
private MySQL mysql;
private String user = "root";
private String pass = "root";
private String database = "productdb";
private String table = "db";
//LOCAL VARIABLES
ArrayList <Product> lastSearch;
public Database()
{
p = new ArrayList<Product>();
option = 0;
}
public Database(PApplet app, String filename, String splitter, int loadSplit, int option)
{
this();
this.filename = "data/" + filename;
this.splitter = splitter;
this.loadSplit = loadSplit;
this.option = option;
if(option == 1) {mysql = new MySQL( app, "localhost", database, user, pass );}
loadDatabase();
}
private void loadDatabase()
{
switch(option)
{
case 0 : //local
try{
String lines[] = loadStrings(filename);
if (lines != null)
{
for (int i = 0; i < lines.length; i++)
{
String line[] = split(lines[i], splitter);
println(line);
if (line.length == loadSplit) this.add(new Product(line[0], line[1], line[2], line[3], line[4], line[5]));
}
}
}catch(NullPointerException e){println("File not found");}
break;
case 1 : //mysql
if(mysql.connect())
{
mysql.query("SELECT * FROM " + table);
while(mysql.next())
{
this.add(new Product(mysql.getString("name"),mysql.getString("info"), mysql.getString("url"), mysql.getInt("quantity"), mysql.getInt("price"), mysql.getString("image")));
}
}
else{println("could not connect");}
break;
}
}
private void saveDatabase()
{
switch(option)
{
case 0 : //local
String[] stringsToSave = new String[p.size()];
for(int i = 0; i < p.size(); i++)
{
stringsToSave[i] = p.get(i).getSplitForm(splitter);
}
saveStrings(filename,stringsToSave);
break;
case 1 : //mysql
if(mysql.connect())
{
for(Product product : p)
{
mysql.execute("REPLACE INTO " + table + " (`name`, `info`, `url`, `quantity`, `price`, `image`) VALUES ('" + product.getName() + "', '" + product.getInfo() + "', '" + product.getUrl() + "', '" + product.getQuantity() + "', '" + product.getPrice() + "', '" + product.getImageUrl() + "')");
}
}
else{println("could not connect");}
break;
}
}
public Database add(Product product)
{
p.add(product);
println(product);
saveDatabase();
return this;
}
public Product get(String name)
{
for(Product product : lastSearch)
{
if(product.getName().equals(name))
{
return product;
}
}
return null;
}
public Product get(int index)
{
println("lastSearch: " + lastSearch.size());
if(lastSearch.size() > index)
{
return lastSearch.get(index);
}
return null;
}
public Database delete(Product product)
{
p.remove(product);
saveDatabase();
return this;
}
public ArrayList find(String tip)//TBC
{
String[] tips = split(tip, " ");
lastSearch = new ArrayList<Product>();
for (Product product : p)
{
String[] ks = split(product.getName(), " ");
for(String kpart : ks)
{
for(String tippart : tips)
{
if(stringsMatch(kpart,tippart))
{
if(!lastSearch.contains(product)){ lastSearch.add(product);}
}
}
}
}
return lastSearch;
}
private boolean stringsMatch(String a, String b)
{
String as = a.toLowerCase(); String bs = b.toLowerCase(); if(as != "" && bs != "" && as.contains(bs)) return true; else return false;
}
@Override
public String toString()
{
String s = "";
try{
String[] lines = loadStrings(filename);
for (int i = 0; i < lines.length; i++)
{
String[] line = split(lines[i], splitter);
s += ""+ join(line, " | ") + "\n";
}
}catch(NullPointerException e){println("File not found");}
return s;
}
}