-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer.java
More file actions
155 lines (120 loc) · 4.24 KB
/
Copy pathCustomer.java
File metadata and controls
155 lines (120 loc) · 4.24 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
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
import java.util.Scanner;
public class Customer implements Comparable<Customer> {
private String name;
private int customerID;
private CreditHistory history;
public Customer(){
name = "";
customerID = 0;
}
public Customer(Customer accountHolder){
setName(accountHolder.getName());
setCustomerID(accountHolder.getID());
history = accountHolder.getCreditHistory();
}
public Customer(String name, int customerID){
setName(name);
setCustomerID(customerID);
}
public Customer(String name, int customerID, CreditHistory aHistory){
setName(name);
setCustomerID(customerID);
history = aHistory;
}
public int compareTo(Customer aCustomer){
int compareNum = 0;
String compareName = aCustomer.getName();
int compareID = aCustomer.getID();
char[] compareNameChar = compareName.toCharArray();
char[] nameChar = name.toCharArray();
// figure out which name is shorter for dictionary sorting
int compareNameLength = compareName.length();
int nameLength = name.length();
int shorterName = 0;
int shorterNameLength;
if (compareNameLength == nameLength){
shorterNameLength = nameLength;
}
else if (compareNameLength < nameLength){
shorterNameLength = compareNameLength;
}
else {
shorterNameLength = nameLength;
}
// if the two names are equal resort to customer ID
if (name.equals(compareName)){
if (compareID == customerID){
compareNum = 0;
}
else if (compareID > customerID){
compareNum = -1;
}
else {
compareNum = +1;
}
}
// if the two names are not equal
else {
// search through all the characters in the string and compare their
// values on the ASCII table
for (int i = 0; i < shorterNameLength; i++){
// if the characters are equal
if (compareNameChar[i] == nameChar[i]){
// then we continue, but check to see if we are at the
// end of the string, then we choose if its shorter
if (i == shorterNameLength - 1){
// the name is longer than compareName goes before
if (compareNameLength < nameLength){
compareNum = +1;
}
// the name is shorter than compareName goes after
else {
compareNum = -1;
}
}
}
// otherwise we can make the comparison
else {
// if the character is lower on the ascii table it is before
if (compareNameChar[i] < nameChar[i]){
compareNum = +1;
}
// if not less than or equal then its bigger, so goes after
else {
compareNum = -1;
}
}
// end of for
}
// end of else
}
return compareNum;
// end of compareTo
}
public void setCreditHistory(CreditHistory aHistory){
history = aHistory;
}
public CreditHistory getCreditHistory(){
// CreditHistory copyHistory = new CreditHistory(history);
return history;
}
public String getName(){
String copyName = name;
return copyName;
}
public void setName(String newName){
name = newName;
}
public void setCustomerID(int newID){
customerID = newID;
}
public int getID(){
int copyID = customerID;
return copyID;
}
public String toString(){
String customerIDstring = Integer.toString(customerID);
String customerString = name+" "+customerIDstring;
return customerString;
}
}