forked from hellokaton/write-readable-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample3.java
More file actions
90 lines (73 loc) · 2.57 KB
/
Copy pathExample3.java
File metadata and controls
90 lines (73 loc) · 2.57 KB
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
package chapter_13;
import java.util.ArrayList;
import java.util.List;
/**
* 用自然语言描述解决方案
*
* @author biezhi
* @date 2018/9/1
*/
public class Example3 {
class Movie {
Integer id;
String title;
String coverImage;
String intro;
}
class CurrentIndex {
int title = 0;
int cover = 0;
int intro = 0;
}
private List<Movie> findAll(String sql) {
return new ArrayList<>();
}
public void printMoive() {
List<Movie> titles = findAll("SELECT id, title FROM movie1 ORDER BY id ASC");
List<Movie> coverImages = findAll("SELECT id, cover_image FROM movie2 ORDER BY id ASC");
List<Movie> intros = findAll("SELECT id, intro FROM movie3 ORDER BY id ASC");
CurrentIndex currentIndex = new CurrentIndex();
for (Movie movie : titles) {
boolean matched = advanceToMatchIndex(currentIndex, titles, coverImages, intros);
if(!matched){
break;
}
System.out.print("id: " + titles.get(currentIndex.title).id);
System.out.print(", title: " + titles.get(currentIndex.title).title);
System.out.print(", cover: " + coverImages.get(currentIndex.cover).coverImage);
System.out.print(", intro: " + intros.get(currentIndex.intro).intro);
System.out.print("\n");
currentIndex.title++;
currentIndex.cover++;
currentIndex.intro++;
}
}
private boolean advanceToMatchIndex(CurrentIndex currentIndex,
List<Movie> titles,
List<Movie> coverImages,
List<Movie> intros){
for (Movie title : titles) {
int titleId = titles.get(currentIndex.title).id;
int coverId = coverImages.get(currentIndex.cover).id;
int introId = intros.get(currentIndex.intro).id;
if(titleId == coverId && titleId == introId){
return true;
}
int maxId = getMaxId(titleId, coverId, introId);
if(titleId < maxId){
currentIndex.title++;
}
if(coverId < maxId){
currentIndex.cover++;
}
if(introId < maxId){
currentIndex.intro++;
}
}
return false;
}
private Integer getMaxId(Integer...ids){
return ids[0] > ids[1] ?
Math.max(ids[0], ids[2]) : Math.max(ids[1], ids[2]);
}
}