-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.java
More file actions
104 lines (89 loc) · 3.61 KB
/
MainActivity.java
File metadata and controls
104 lines (89 loc) · 3.61 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
package com.example.myinventoryapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// UI components
private EditText usernameEditText, passwordEditText;
private Button loginButton, createAccountButton;
// Database helper
private DatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize database helper
dbHelper = new DatabaseHelper(this);
// Initialize UI components
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
createAccountButton = findViewById(R.id.createAccountButton);
// Set up login button click listener
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
loginUser();
}
});
// Set up create account button click listener
createAccountButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createAccount();
}
});
}
/**
* Attempts to login the user with the provided credentials
*/
private void loginUser() {
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
// Validate input
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please enter username and password", Toast.LENGTH_SHORT).show();
return;
}
// Check credentials against database
if (dbHelper.checkUser(username, password)) {
// Login successful, proceed to inventory screen
Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, InventoryActivity.class);
startActivity(intent);
finish(); // Close login activity
} else {
// Login failed
Toast.makeText(this, "Invalid username or password", Toast.LENGTH_SHORT).show();
}
}
/**
* Creates a new user account with the provided credentials
*/
private void createAccount() {
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
// Validate input
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Please enter username and password", Toast.LENGTH_SHORT).show();
return;
}
// Add user to database
long userId = dbHelper.addUser(username, password);
if (userId != -1) {
// Account creation successful
Toast.makeText(this, "Account created successfully", Toast.LENGTH_SHORT).show();
// Proceed to inventory screen
Intent intent = new Intent(MainActivity.this, InventoryActivity.class);
startActivity(intent);
finish(); // Close login activity
} else {
// Account creation failed
Toast.makeText(this, "Failed to create account. Username may already exist.", Toast.LENGTH_SHORT).show();
}
}
}