Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 62 additions & 24 deletions app/src/main/java/com/example/intersectionsafe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,86 @@

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity {
TextView carsPerHour,numberOfCarsNow,numberOfCarsTotal,TimeElapsed;
TextView UserNameTitle;
FirebaseAuth auth;
private static final DecimalFormat df = new DecimalFormat("0.00");
private static final DecimalFormat dfInteger = new DecimalFormat("0");


TextView carsPerHour,carsPerHourV,numberOfCarsNow,numberOfCarsNowV,numberOfCarsTotal,numberOfCarsTotalV,optimizedCarsH,optimizedCarsV;

DatabaseReference reference;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

System.out.println("SYSTEM");

carsPerHour = findViewById(R.id.CarPerHourfield);
numberOfCarsNow = findViewById(R.id.numberOfCarsNowfield);
numberOfCarsTotal = findViewById(R.id.numberOfCarsTotalfield);
TimeElapsed = findViewById(R.id.TimeElapsedfiled);
showIntersectionSafeInfo();
optimizedCarsH= findViewById(R.id.optimizedCarsH);
optimizedCarsV= findViewById(R.id.optimizedCarsV);
carsPerHourV = findViewById(R.id.CarPerHourfieldV);
numberOfCarsNowV = findViewById(R.id.numberOfCarsNowfieldV);
numberOfCarsTotalV = findViewById(R.id.numberOfCarsTotalfieldV);
getdata();
}

public void showIntersectionSafeInfo(){
Intent intent =getIntent();
auth = FirebaseAuth.getInstance();
// DatabaseReference reference=FirebaseDatabase.getInstance().getReference().child("py");
reference = FirebaseDatabase.getInstance().getReference().child("py")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("carsPerHour");
// reference.child(name).setValue(helperClass);
String carPH= intent.getStringExtra("carsPerHour");
carsPerHour.setText(carPH);
}
private void getdata() {
reference = FirebaseDatabase.getInstance().getReference().child("py");

reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

//horizantal
float carsPH= snapshot.child("Intersection").child("HorizontalTraffic").child("carsPerHour").getValue(Integer.class);
carsPerHour.setText(dfInteger.format(carsPH));
Integer nocn=snapshot.child("Intersection").child("HorizontalTraffic").child("numOfCarsRN").getValue(Integer.class);
numberOfCarsNow.setText(String.valueOf(nocn));
Integer noct=snapshot.child("Intersection").child("HorizontalTraffic").child("numOfCarsTotal").getValue(Integer.class);
numberOfCarsTotal.setText(String.valueOf(noct));
//Vertical
float carsPHV=snapshot.child("Intersection").child("VerticalTraffic").child("carsPerHour").getValue(Integer.class);
carsPerHourV.setText(dfInteger.format(carsPHV));
Integer nocnV=snapshot.child("Intersection").child("VerticalTraffic").child("numOfCarsRN").getValue(Integer.class);
numberOfCarsNowV.setText(String.valueOf(nocnV));
Integer noctV=snapshot.child("Intersection").child("VerticalTraffic").child("numOfCarsTotal").getValue(Integer.class);
numberOfCarsTotalV.setText(String.valueOf(noctV));
//optimization
// private static final DecimalFormat df = new DecimalFormat("0.00");
double optimizedH= (10+(45*(carsPH/(carsPH+carsPHV))));
double optimizedV= (10+(45*(carsPHV/(carsPHV+carsPH))));
// double optimizedH=carsPH/(carsPH+carsPHV);
optimizedCarsH.setText(df.format(optimizedH)+" seconds");
optimizedCarsV.setText(df.format(optimizedV)+" seconds");



}

@Override
public void onCancelled(@NonNull DatabaseError error) {
// calling on cancelled method when we receive
// any error or we are not able to get the data.
Toast.makeText(MainActivity.this, "Fail to get data.", Toast.LENGTH_SHORT).show();
}
});
}
}
Loading