Guest User

Untitled

a guest
Oct 1st, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.50 KB | None | 0 0
  1. // Database Version
  2. public static final int DATABASE_VERSION = 1;
  3. // Database Name
  4. public static final String DATABASE_NAME = "traineeInfo";
  5. // Contacts table name
  6. public static final String TABLE_NAME = "trainee";
  7. // Trainee Table Columns names
  8. public static final String COL_ID = "ID";
  9. public static final String COL_USERNAME = "USERNAME";
  10. public static final String COL_NAME = "NAME";
  11. public static final String COL_PASS = "PASSWORD";
  12. public static final String COL_EMAIL = "EMAIL";
  13. SQLiteDatabase db;
  14.  
  15. //DataBase Helper
  16. public DatabaseHelper(Context context) {
  17. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  18. }
  19.  
  20. //onCreat
  21. @Override
  22. public void onCreate(SQLiteDatabase db) {
  23. String CREATE_CONTACTS_TABLE = "create table contacts (id integer primary key not null ," +
  24. " username text not null, name text not null, email text not null,password text not null );";
  25.  
  26. db.execSQL(CREATE_CONTACTS_TABLE);
  27. this.db = db;
  28. }
  29.  
  30. //onUpgrade
  31. @Override
  32. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  33. // Drop older table if existed
  34. db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
  35. // Creating tables again
  36. this.onCreate(db);
  37. }
  38.  
  39. //Adding new trainee
  40. public void addTrainee(Trainee trainee) {
  41. SQLiteDatabase db = this.getWritableDatabase();
  42.  
  43. ContentValues values = new ContentValues();
  44.  
  45. int count = getShopsCount();
  46.  
  47. values.put(COL_ID, count);
  48. values.put(COL_USERNAME, trainee.getUsername());
  49. values.put(COL_NAME, trainee.getName());
  50. values.put(COL_PASS, trainee.getPassword());
  51. values.put(COL_EMAIL, trainee.getEmail());
  52.  
  53. // Inserting Row
  54. db.insert(TABLE_NAME, null, values);
  55. db.close();// Closing database connection
  56. }
  57.  
  58.  
  59. //Check the match beetwen user data and database
  60. public String searchPassword(String username) {
  61.  
  62. //Read data from dataBase
  63. db = this.getReadableDatabase();
  64.  
  65.  
  66.  
  67. // Getting shops Count
  68. public int getShopsCount() {
  69. String countQuery = "SELECT * FROM " + TABLE_NAME;
  70. SQLiteDatabase db = this.getReadableDatabase();
  71. Cursor cursor = db.rawQuery(countQuery, null);
  72. cursor.close();
  73. // return count
  74. return cursor.getCount();
  75. }
  76.  
  77. private static final Pattern PASSWORD_PATTERN =
  78. Pattern.compile("^" +
  79. "(?=.*[a-zA-Z])" + //any letter
  80. "(?=\S+$)" + //no white spaces
  81. ".{4,}" + //at least 4 characters
  82. "$");
  83.  
  84. //The database helper.
  85. DatabaseHelper myDb;
  86.  
  87.  
  88. private TextInputLayout textInputUsername;
  89. private TextInputLayout textInputEmail;
  90. private TextInputLayout textInputName;
  91. private TextInputLayout textInputPassword;
  92. private TextInputLayout textInputConfirmPassword;
  93.  
  94.  
  95. @Override
  96. protected void onCreate(Bundle savedInstanceState) {
  97. super.onCreate(savedInstanceState);
  98. setContentView(R.layout.activity_join_us);
  99.  
  100. //Creat the databas.
  101. myDb = new DatabaseHelper(this);
  102.  
  103. textInputUsername = findViewById(R.id.etUserName);
  104. textInputName = findViewById(R.id.etName);
  105. textInputEmail = findViewById(R.id.etEmail);
  106. textInputPassword = findViewById(R.id.etPassword);
  107. textInputConfirmPassword = findViewById(R.id.etConfirmPassword);
  108.  
  109. TextView tvLogin = (TextView) findViewById(R.id.tvLogin);
  110.  
  111. //onClick on text view juin us for register activity.
  112. tvLogin.setOnClickListener(new View.OnClickListener() {
  113. @Override
  114. public void onClick(View view) {
  115. Intent intent = new Intent(JoinUs.this,Login.class);
  116. startActivity(intent);
  117. finish();
  118. }
  119. });
  120. }
  121.  
  122.  
  123. public void confirminput(View v) {
  124. //If one of the validate retuen false the validation faild.
  125. if ( !validateEmail() || !validateUsername() || !validateName() || !validatePassword()) {
  126. Toast.makeText(getApplicationContext(), " Validation NOT OK", Toast.LENGTH_LONG).show();
  127. return;
  128. }
  129.  
  130. //Inserting Trainee.
  131. Trainee trainee = new Trainee();
  132. trainee.setUsername( textInputUsername.getEditText().getText().toString().trim());
  133. trainee.setName(textInputName.getEditText().getText().toString().trim());
  134. trainee.setEmail(textInputEmail.getEditText().getText().toString().trim());
  135. trainee.setPassword(textInputPassword.getEditText().getText().toString().trim());
  136.  
  137. //Insert Method data .
  138. myDb.addTrainee(trainee);
  139.  
  140.  
  141. }
  142.  
  143.  
  144. //Validate User name.
  145. private boolean validateUsername() {
  146. String usernameInput = textInputUsername.getEditText().getText().toString().trim();
  147.  
  148. if (usernameInput.isEmpty()) {
  149. textInputUsername.setError("Field can't be empty");
  150. return false;
  151. } else if (usernameInput.length() > 15) {
  152. textInputUsername.setError("Username too long");
  153. return false;
  154. } else {
  155. textInputUsername.setError(null);
  156. return true;
  157. }
  158. }
  159.  
  160. //Validate Email
  161. private boolean validateEmail() {
  162. String emailInput = textInputEmail.getEditText().getText().toString().trim();
  163.  
  164. /*Check if email already exist
  165. if (checkIfExists(emailInput)) {
  166. textInputEmail.setError("Email already exist");
  167. return false;
  168. }else*/
  169. if (emailInput.isEmpty()) {
  170. textInputEmail.setError("Field can't be empty");
  171. return false;
  172. } else if (!Patterns.EMAIL_ADDRESS.matcher(emailInput).matches()) {
  173. textInputEmail.setError("Please enter a valid email address");
  174. return false;
  175. } else {
  176. textInputEmail.setError(null);
  177. return true;
  178. }
  179. }
  180.  
  181. //Validate Name
  182. private boolean validateName() {
  183. String firstnameInput = textInputName.getEditText().getText().toString().trim();
  184.  
  185. if (firstnameInput.isEmpty()) {
  186. textInputName.setError("Field can't be empty");
  187. return false;
  188. } else if (firstnameInput.length() > 15) {
  189. textInputName.setError("Username too long");
  190. return false;
  191. } else {
  192. textInputName.setError(null);
  193. return true;
  194. }
  195. }
  196.  
  197. //Validate Password
  198. private boolean validatePassword() {
  199. String passwordInput = textInputPassword.getEditText().getText().toString().trim();
  200. String confirmPasswordInput = textInputConfirmPassword.getEditText().getText().toString().trim();
  201.  
  202. //Check if password & confirm password match
  203. if (passwordInput.equals(confirmPasswordInput)) {
  204.  
  205. if (passwordInput.length() < 4) {
  206. textInputPassword.setError("Password must contain 4 characters");
  207. return false;
  208. }else if (passwordInput.contains(" ")) {
  209. textInputPassword.setError("No Spaces Allowed");
  210. return false;
  211. }else if (!PASSWORD_PATTERN.matcher(passwordInput).matches()) {
  212. textInputPassword.setError("Password must contain any letter");
  213. return false;
  214. }else if (confirmPasswordInput.length() < 4) {
  215. textInputConfirmPassword.setError("Password must contain 4 characters");
  216. return false;
  217. }else if (confirmPasswordInput.contains(" ")) {
  218. textInputConfirmPassword.setError("No Spaces Allowed");
  219. return false;
  220. }else if (confirmPasswordInput.isEmpty()) {
  221. textInputConfirmPassword.setError("Field can't be empty");
  222. return false;
  223. }else if (!PASSWORD_PATTERN.matcher(confirmPasswordInput).matches()) {
  224. textInputConfirmPassword.setError("Password must contain any letter");
  225. return false;
  226. }else {
  227. textInputConfirmPassword.setError(null);
  228. textInputPassword.setError(null);
  229. return true;
  230. }
  231. }else {
  232. textInputConfirmPassword.setError("Password don't match!");
  233. return false;
  234. }
  235.  
  236. }
Add Comment
Please, Sign In to add comment