-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerson.java
125 lines (112 loc) · 2.89 KB
/
Person.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class Person {
/** just for fun */
String name;
/** array of match preferences with the most preferred first */
int[] spousalPicks;
/** responses integer array holds collected proposal responses
* used by the proposer type to manage workflow
* key: 0 unasked, 1 asked, 2 no, 3 maybe */
int[] responses;
/** proposals list used by the proposees to manage their propositions */
List<Integer> proposals;
public Person(String name, int numberOfPairs) {
this.name = name;
this.responses = new int[numberOfPairs];
this.proposals = new ArrayList<Integer>();
setPreferredMatches(numberOfPairs);
}
/** generate 'random' match preferences */
public void setPreferredMatches(int numberOfPairs) {
spousalPicks = new int[numberOfPairs];
for ( int x = 0; x< numberOfPairs;x++ ) { // create a sequential array
spousalPicks[x]=x;
}
shuffleArray(spousalPicks); // shuffle
}
/** proposee functions */
public void addProposal(int suitor) {
proposals.add(suitor);
}
/** propser functions */
public void proposalAccepted() {
for (int i=0;i<responses.length;i++) {
if (responses[i]==1) {
responses[i]=3;
return;
}
}
}
public void proposalDeclined() { // so sad.
for (int i=0;i<responses.length;i++) {
if (responses[i]==1) {
responses[i]=2;
return;
}
}
}
public int getMatch() {
for (int i=0; i<responses.length;i++) {
if (responses[i]==3) { // an acceptance!
return spousalPicks[i];
}
}
return -1; //no match found
}
public boolean hasMatch() {
for (int i=0; i<responses.length;i++) {
if (responses[i]==3) { // an acceptance!
return true;
}
}
return false;
}
public int setNextProposalTarget() {
for ( int p=0;p<responses.length;p++ ) {
if (responses[p] ==0) {
// propose to this lucky person
responses[p]=1;
return p;
}
}
//System.out.println("Proposer is out of potential mates");
throw new IllegalArgumentException("Something is wrong -we ran out of matches for someone.");
//return -1; // no proposals to make!
}
// setters
void setName(String name) {
this.name = name;
}
// toString
@Override
public String toString() {
String returnString = this.name+" preference order ";
for (int i=0;i<this.spousalPicks.length;i++) {
returnString = returnString+ " "+this.spousalPicks[i];
}
returnString+="\n";
return returnString;
}
public String getName() {
return this.name;
}
private void shuffleArray(int[] array)
{
int index;
Random random = new Random();
for (int i = array.length - 1; i > 0; i--)
{
index = random.nextInt(i + 1);
if (index != i)
{
array[index] ^= array[i];
array[i] ^= array[index];
array[index] ^= array[i];
}
}
}
}