Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.4 kB
1
Indexable
Never
package edu.ewubd.cse489120221;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.text.CollationElementIterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class creatEventActivity extends AppCompatActivity {


    TextView errorTextView = findViewById(R.id.error);

    Button buttonCancel = findViewById(R.id.btnCancel);
    Button buttonShare = findViewById(R.id.btnShare);
    Button buttonSave = findViewById(R.id.btnSave);
    EditText nameField = findViewById(R.id.etName);
    EditText placeField = findViewById(R.id.etPlace);
    EditText dateField = findViewById(R.id.etDateTime);
    EditText capacityField = findViewById(R.id.etCapacity);
    EditText budgetField = findViewById(R.id.etBudget);
    EditText emailField = findViewById(R.id.etEmail);
    EditText phoneField = findViewById(R.id.etPhone);
    EditText descField = findViewById(R.id.etDescription);
    RadioGroup typeField = findViewById(R.id.etType);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event_information_layout);


        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
    });
        buttonShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(creatEventActivity.this, "Share Button Pressed", Toast.LENGTH_SHORT).show();
            }
        });

        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = nameField.getText().toString().trim();
                String place = placeField.getText().toString().trim();
                int typeId = typeField.getCheckedRadioButtonId();
                String date = dateField.getText().toString().trim();
                String capacityText = capacityField.getText().toString();
                String budgetText = budgetField.getText().toString();
                String email = emailField.getText().toString().trim();
                String phone = phoneField.getText().toString().trim();
                String desc = descField.getText().toString().trim();

                if (name.length() == 0) {
                    setError("Name cannot be empty");
                    return;
                }

                if (place.length() == 0) {
                    setError("Place cannot be empty");
                    return;
                }

                if (date.length() == 0) {
                    setError("Date cannot be empty");
                    return;
                }

                //MString to integer
                int capacity = Integer.parseInt(capacityText);
                if (capacityText.length() == 0) {
                    setError("Capacity cannot be empty");
                    return;
                }

                if (capacity <= 0){
                    setError("Capacity must be greater than 0");
                    return;
                    }


                double budget = Double.parseDouble(budgetText);

                if (budget == 0){
                    setError("Budget cannot be empty");
                    return;
                }

                if (budget <= 0) {
                        setError("Budget must be greater than 0");
                        return;
                    }


                String emailPattern = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$";
                Matcher emailMatcher = Pattern.compile(emailPattern, Pattern.CASE_INSENSITIVE).matcher(email);
                if (!emailMatcher.matches()) {
                    setError("Email must be a valid email address");
                    return;
                }

                if (phone.length() != 11 || !phone.startsWith("01")) {
                    setError("Phone must be a valid phone number");
                    return;
                }

                setError("");

                RadioButton selectedRadioButton = findViewById(typeId);
                String type = selectedRadioButton.getText().toString();

                System.out.println("Name: " + name);
                System.out.println("Place: " + place);
                System.out.println("Type: " + type);
                System.out.println("Date: " + date);
                System.out.println("Capacity: " + capacity);
                System.out.println("Budget: " + budget);
                System.out.println("Email: " + email);
                System.out.println("Phone: " + phone);

                if (desc.length() > 0) {
                    System.out.println("Description: " + desc);
                }
            }
        });



}
    private void setError(String message) {
        errorTextView.setText(message);
        System.err.println("Error: " + message);
    }

}