-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecipe.java
146 lines (135 loc) · 2.88 KB
/
Recipe.java
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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Recipe
{
private static ArrayList <String> tags = new ArrayList<String>();
private static ArrayList <Integer> similarities = new ArrayList<Integer>();
private static ArrayList <String> titles = new ArrayList<String>();
private static ArrayList <String> links = new ArrayList<String>();
private String data;
private int b = 1;
private static String link;
String choice;
File myObj = new File("recipes.txt");
int length = 0;
public Recipe()
{
}
public void readFile()
{
try {
Scanner myReader = new Scanner(myObj);
int a = 0;
getLength();
while (a<b)
{
data = myReader.nextLine();
a++;
}
b++;
checkRecipe(data);
myReader.close();
} catch (FileNotFoundException e)
{
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public static void checkRecipe(String data)
{
String recipe = data;
String comp = null;
int start = 1;
for(int i =recipe.length(); i>0; i--)
{
if(recipe.charAt(i-1)=='|')
{
links.add(recipe.substring(i+1,recipe.length()));
break;
}
}
for(int i = 0; i<recipe.length(); i++)
{
if(recipe.charAt(i)==';')
{
titles.add(recipe.substring(0,i-1));
comp = recipe.substring(i+1,recipe.length());
}
}
for(int i = 0; i<comp.length(); i++)
{
if(comp.charAt(i) == ',')
{
tags.add(comp.substring(start,i));
start = i+2;
}
}
}
public void compareRecipe(ArrayList<String> qualities)
{
int similarity = 0;
if (tags.size()<qualities.size())
{
for(int i =0; i<qualities.size();i++)
{
for(int l = 0; l<tags.size(); l++)
{
if(qualities.get(i).equals(tags.get(l)))
similarity++;
}
}
}
else
for(int i =0; i<tags.size();i++)
{
for(int l = 0; l<qualities.size(); l++)
{
if(tags.get(i).equals(qualities.get(l)))
similarity++;
}
}
similarities.add(similarity);
tags.clear();
}
public void findMatch()
{
int max = similarities.get(0);
for(int i = 1; i<similarities.size(); i++)
{
if(similarities.get(i)>max)
max = similarities.get(i);
}
System.out.println("Similarities: " +max);
choice = titles.get(similarities.indexOf(max));
link = links.get(similarities.indexOf(max));
System.out.println(links);
System.out.println("Choice: " +choice);
System.out.println("Link: " + link);
}
public int getLength()
{
Scanner secondReader;
try {
secondReader = new Scanner(myObj);
while(secondReader.hasNextLine())
{
length++;
secondReader.nextLine();
}
secondReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return length;
}
public String setChoice()
{
return choice;
}
public String getLink()
{
return link;
}
}