-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardCollection.java
More file actions
58 lines (54 loc) · 1.54 KB
/
CardCollection.java
File metadata and controls
58 lines (54 loc) · 1.54 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
import java.util.List;
import java.util.ArrayList;
public class CardCollection {
private String owner;
private List<Card> myCollection;
public CardCollection(String owner){
this.owner = owner;
myCollection = new ArrayList<Card>();
}
public boolean addCard(Card c){
int i = 0;
while(i < myCollection.size()){
if(myCollection.get(i).compareTo(c) == 0){
return false;
}
if(myCollection.get(i).compareTo(c) > 0){
myCollection.add(i, c);
return true;
}
i++;
}
myCollection.add(c);
return true;
}
public void removeCard(int index){
myCollection.remove(index);
}
public int getSize(){
return myCollection.size();
}
public List<Card> mergeCollections(CardCollection cc){
List<Card> duplicates = new ArrayList<Card>();
while (cc.getSize() > 0){
Card c = cc.myCollection.remove(0);
if(this.addCard(c) == false){
duplicates.add(c);
} else{
myCollection.add(c);
}
}
return duplicates;
}
public String toString(){
String x = "";
for(int i = 0; i<owner.length(); i++){
x = x + "-";
}
String y = "";
for(int j= 0; j < this.myCollection.size(); j++){
y = y + ((Card) this.myCollection.get(j)).toString() + "\n";
}
return owner + "\n" + x + "\n" + y ;
}
}