-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewUser.java
233 lines (199 loc) · 8.26 KB
/
NewUser.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* This activity allows the user to enter in their name, username, password, section and year.
* It checks to make sure that none of these fields are blank and that passwords are valid.
* admins can edit users points in a different activity.
* Programming Assignment #8
* @version v1.0
*/
package com.example.vhl2.bandapp3;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
public class NewUser extends AppCompatActivity {
final String TAG = "NewUser: ";
private final String NEW_USER_CODE = "newMember";
private BandMember user;
private Spinner classes;
private Spinner section;
/**
* This function initializes an arrayadapter as well as the 2 spinners. It also sets the text
* Views views if the user has to renter a user name or rotated the phone.
* @param savedInstanceState a band member object
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_user);
section = (Spinner) findViewById(R.id.section);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.sections,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
section.setAdapter(adapter);
classes = (Spinner) findViewById(R.id.classes);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.classes,
android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
classes.setAdapter(adapter2);
Intent intent = getIntent();
if (savedInstanceState != null) {
user = (BandMember) savedInstanceState.getSerializable("newMember");
setInformation();
} else if (intent.getExtras().getBoolean("redo")) {
user = (BandMember) intent.getSerializableExtra("newMember");
setInformation();
} else {
user = new BandMember();
}
}
/**
* This function is called by on create to set the contents of the text views and spinners
*/
private void setInformation(){
EditText userNameText = (EditText) findViewById(R.id.userName);
EditText nameText = (EditText) findViewById(R.id.name);
EditText passwordText = (EditText) findViewById(R.id.password);
Spinner year = (Spinner) findViewById(R.id.classes);
Spinner instrument = (Spinner) findViewById(R.id.section);
EditText passwordText2 = (EditText) findViewById(R.id.passwordCheck);
nameText.setText(user.getName());
userNameText.setText(user.getUserName());
passwordText.setText(user.getPassword());
passwordText2.setText(user.getPassword());
String userSection = user.getInstrument();
String userYear = user.getYear();
switch(userSection){
case "saxophone" :
instrument.setSelection(0);
break;
case "trumpet" :
instrument.setSelection(1);
break;
case "percussion" :
instrument.setSelection(2);
break;
case "flute" :
instrument.setSelection(3);
break;
case "clarinet" :
instrument.setSelection(4);
break;
case "trombone" :
instrument.setSelection(5);
break;
case "bass" :
instrument.setSelection(6);
break;
case "sousaphone" :
instrument.setSelection(7);
break;
default:
Log.e(TAG, "addMember: invalid entry");
break;
}
switch(userYear){
case "freshman":
year.setSelection(0);
break;
case "sophomore":
year.setSelection(1);
break;
case "junior":
year.setSelection(2);
break;
case "senior":
year.setSelection(3);
break;
case "director":
year.setSelection(4);
break;
default:
year.setSelection(0);
break;
}
}
/**
* saves the instance state in case app is closed for any reason prematurely.
* @param outState instanceState for when app restarts
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(NEW_USER_CODE, user);
}
/**
* creates the options menu
* @param menu a menu reasource file;
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.new_user_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* sets behavior for the options menu which allows it to submit the contents of the text Views
* and spinners into a bandmember object back to main activity.
* @param item the button that was clicked
* @return whether the action was successful.
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int menuId = item.getItemId();
EditText userNameText = (EditText) findViewById(R.id.userName);
EditText nameText = (EditText) findViewById(R.id.name);
EditText passwordText = (EditText) findViewById(R.id.password);
EditText passwordText2 = (EditText) findViewById(R.id.passwordCheck);
Spinner year = (Spinner) findViewById(R.id.classes);
String name = nameText.getText().toString();
String userName = userNameText.getText().toString();
String password = passwordText.getText().toString();
switch (menuId) {
case R.id.doneButton:
if((!userName.equals("") && (!password.equals("")))) {
if(password.equals(passwordText2.getText().toString())) {
Intent intent = new Intent();
user.setName(name);
user.setPassword(password);
user.setUserName(userName);
user.setYear(classes.getSelectedItem().toString());
user.setInstrument(section.getSelectedItem().toString());
Log.d(TAG, "onOptionsItemSelected: " + user.toString());
intent.putExtra(NEW_USER_CODE, user);
setResult(Activity.RESULT_OK, intent);
NewUser.this.finish();
return true;
}else{
Toast.makeText(this, "the passwords you entered do not match", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(this, "your user name and password cannot be blank", Toast.LENGTH_SHORT).show();
}
return true;
case android.R.id.home:
Intent dud = new Intent();
dud.putExtra(NEW_USER_CODE, user); //test
setResult(0, dud);
NewUser.this.finish();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}