30 Ağustos 2017 Çarşamba

JAVA - SETS



package datastructures;

import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class Sets {

 public static void main(String[] args) {
  // hashset is random order
  Set animals = new HashSet();
  animals.add("dog");
  animals.add("pig");
  animals.add("hog");
  animals.add("cow");
  
  System.out.println(animals);
  
  animals.add("hog");
  animals.add("cow");
  animals.add("groose");
  // same elements does not add again
  System.out.println(animals);

  // linked hash set is same order in added
  Set animals1 = new LinkedHashSet();
  animals1.add("chicken");
  animals1.add("pig");
  animals1.add("snake");
  animals1.add("cow");
  
  System.out.println(animals1);
  
  // Difference of animals from animals1
  Set differenceSet = new HashSet(animals);
  differenceSet.removeAll(animals1);
  System.out.println(differenceSet);
  
  // Union of animals from animals1
  Set unionSet = new HashSet(animals);
  unionSet.addAll(animals1);
  System.out.println(unionSet);
    
  
  // tree set is in alphabetical order
  Set animals2 = new TreeSet();
  animals2.add("dog");
  animals2.add("pig");
  animals2.add("hog");
  animals2.add("cow");
  
  System.out.println(animals2);
 }

}


Here is the output:

[hog, cow, dog, pig]
[groose, hog, cow, dog, pig]
[chicken, pig, snake, cow]
[groose, hog, dog]
[groose, hog, chicken, snake, cow, dog, pig]
[cow, dog, hog, pig]

26 Ağustos 2017 Cumartesi

Java Arrays - For and ForEach Loop - Single And Multi Dimensional Arrays


public class Arrays {

 public static void main(String[] args) {
  String[] alphabet = {"a", "b", "c", "d", "e", "f", "g"};
  
  // With for loop
  
  System.out.println("Old fashioned way");
  int size = alphabet.length;
  
  for(int i=0; i< size; i++){
   System.out.print(alphabet[i] + " ");
  }

  // With for-each loop
  System.out.println("\n\nWith For-Each Loop");
  
  for(String letter : alphabet){
   System.out.print(letter + " ");
  }
 }

}


Here is the output:

Old fashioned way
a b c d e f g 

With For-Each Loop
a b c d e f g 

Here with multi dimensional array example



public class Arrays {
      public static void main(String[] args) {
       // Double Arrays
  String[][] users = {
    {"Jon", "Snow", "js@test.com","78965412"},
    {"Dany", "Targeryan", "dt@test.com", "45632101"},
    {"Arya", "Stark", "as@test.com", "12304569"}
   };
  
  int numOfUsers = users.length;
  int numOfFields = users[0].length;
  
  // With old fashioned way
  System.out.println("\n\nWith old Fashoned Way");
  for(int i=0; i< numOfUsers; i++){
   for(int j=0; j< numOfFields; j++){
    System.out.print(users[i][j] + " ");
   }
   System.out.println();
  }
  
  // Little More Intelligent Way
  System.out.println("\n\nLittle More Intelligent Way");
  for(int i=0; i< numOfUsers; i++){
   String name= users[i][0];
   String lastname = users[i][1];
   String mail = users[i][2];
   String phone = users[i][3];
   System.out.println(name+ " " + lastname+ " " + mail + " " + phone);
  }
  
  // With ForEach Loop
  System.out.println("\n\nWith ForEach Loop");
  for(String[] user: users){
   for(String us: user){
    System.out.print(us + " ");
   }
   System.out.println();
  }
 }

}


Here is the output:

With old Fashoned Way
Jon Snow js@test.com 78965412 
Dany Targeryan dt@test.com 45632101 
Arya Stark as@test.com 12304569 


Little More Intelligent Way
Jon Snow js@test.com 78965412
Dany Targeryan dt@test.com 45632101
Arya Stark as@test.com 12304569


With ForEach Loop
Jon Snow js@test.com 78965412 
Dany Targeryan dt@test.com 45632101 
Arya Stark as@test.com 12304569 

Java - Dosyadan Okunan Sayi Telefon Numarasi mi Kontrol Etme

Dosyada yazili olan sayiyi okuyacagiz. Ve bu sayi bir telefon numarasi mi anlamaya calisacagiz.
Okunan sayinin telefon numarasi olmasi belli kritirler var.
Valid phone numbers:

  •  10 digits long 
  • Area code cannot start in 0 or 9 
  • There cannot be 911 in phone number
Bu kosullara uymayanlar icin User defined exception olusturacagiz.


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class PhoneNumberApp {
 public static void main(String[] args) {
  String filename = "C:\\Users\\Gamze\\Java Udemy\\phoneNumber.txt";
  String phoneNum = null;
  
  File file = new File(filename);
  
  try {
   BufferedReader bf = new BufferedReader(new FileReader(file));
   phoneNum = bf.readLine();
   System.out.println(phoneNum);
   bf.close();
   
  } catch (FileNotFoundException e) {
   System.out.println("ERROR: File is not found "+ filename);
   e.printStackTrace();
  } catch (IOException e) {
   System.out.println("ERROR: Data could not read " +filename);
   e.printStackTrace();
  }
  
  // Valid phone numbers:
  // 10 digits long
  // Area code cannot start in 0 or 9
  // There cannot be 911 in phone number
  
  try{
  if(phoneNum.length() != 10) {
   throw new TenDigitsException(phoneNum);
  } if(phoneNum.substring(0, 1).equals("0") || phoneNum.substring(0,1).equals("9")){
   throw new AreaCodeException(phoneNum);
  } if(phoneNum.contains("911")){
   throw new EmergencyCodeException(phoneNum);
  }
  } catch(TenDigitsException ex){
   System.out.println("ERROR: Phone number is not 10 digits");
  } catch(AreaCodeException ex){
   System.out.println("ERROR: Phone number cannot start with 0 or 9");
  } catch(EmergencyCodeException ex){
   System.out.println("ERROR: Phone number cannot contain emergency code");
  }
 }
}

class TenDigitsException extends Exception{
 String num;
 TenDigitsException(String num) {
  this.num = num;
 }
 public String toString(){
  return ("Ten Digits Exception "+num);
 }
}

class AreaCodeException extends Exception{
 String num;
 AreaCodeException(String num){
  this.num = num;
 }
 public String toString(){
  return ("AreaCodeException "+num);
 }
}
class EmergencyCodeException extends Exception{
 String num;
 EmergencyCodeException(String num){
  this.num = num;
 }
 public String toString(){
  return ("EmergencyCodeException "+ num);
 }
}


Java - Dosya Okuma / Yazma - Error Handling

Okuma Islemi

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileOperations1 {

 public static void main(String[] args) {
  // 1. Define the path that we read the file
  // \ younunu ya / yap ya da \\ kullan. 
  // java bu sekilde anliyor.
  String filename = "C:\\Users\\Gamze\\Java Udemy\\FileToRead.txt";
  String text = null;
  
  // 2. Create the file in Java
  File file = new File(filename);
  
  // 3. Open the file
  BufferedReader br;
  
  // alt satirda exception handling lazim.
  //cunku o isimde bir dosya bulunmayabilir.
  try {
   br = new BufferedReader(new FileReader(file));

   
   // 4. Read the file
   // dosya okunmasi sirasinda txt olamyabilir.
   // ppt veya image olabilir. hata handling lazim
   text = br.readLine();
   
   // 5.Close the resources
   br.close();
   
  } catch (FileNotFoundException e) {
   System.out.println("ERROR:File is not found "+ filename);
   e.printStackTrace();
  } catch (IOException e) {
   System.out.println("ERROR:Could not read the data "+ filename);
   e.printStackTrace();
  } finally {
   System.out.println("Finished reading file");
  }
  System.out.println(text);
  
  
 }

}


Here is the output:
Finished reading file
This is the file for reading purpose.

Yazma Islemi

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class FileOperations1_Write {

 public static void main(String[] args) {
  // 1. Define the path
  String filename = "C:\\Users\\Gamze\\Java Udemy\\FileToWrite.txt";
  String text = "I am writing to this file.";
  
  // 2. Create the file in java
  File file = new File(filename);
  
  // 3. Open file writer
  FileWriter fw;
  try {
   fw = new FileWriter(file);
   // 4. Write to file
   fw.write(text);
   
   // 5. Close the file
   fw.close();
   
  } catch (IOException e) {
   System.out.println("ERROR: Could not read the file " + filename);
   e.printStackTrace();
  } finally {
   System.out.println("Closing the file writer.");
  }
  

 }

}


Bu isimde bir dosya olusturulup icine mesaji yazar.

18 Ağustos 2017 Cuma

Php sonuclarini html de gosterme

Mysql ile olusturdugum veritabanindaki kayitlari php cekiyorum. Bunlari android de kullanacagim. Hem bu verileri debug etmek hem de denemek amaciyla sonuclari html de gostermek istedim.

Wamp serverimin www klasorunde loginform01 diye bir klasor olusturdum ve gerekli dosyalari oraya koydum. Simdi ise bana gerekli olan iki dosyayi gosterecegim. ilki init.php. burada veritabani baglantisini olusturuyoruz. Digeri ise xx.php burda da veritabanindaki kayitlari sirasiyla cekiyorum.

init.php

<?php
$host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "example_test";

$con  = mysqli_connect($host, $db_user, $db_password, $db_name);

if($con){
echo "Connection success ...";
}else{
echo "Connection failed ...";
}

?>


xx.php

<?php
require "init.php";
$sql = "select uname from user";
$result = mysqli_query($con, $sql);
    while ($row = mysqli_fetch_array($result)) {
        ?>
            <div>
 
<p><?php echo $row['fcm_token'] ?></p>
            </div>
        <?php
    }
?>


Bu sekilde veritabanindaki kayitlari ekranda paragraf olarak gostermis oluyoruz.

8 Ağustos 2017 Salı

ANDROID - FIREBASE UPDATE OPERATION

I want to update my record in my firebase database table. I will update order cost as 10 which has primary key as: "20170807192734".




Here is the query I should write to update:



final FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference dbRef = firebaseDatabase.getReference("orders");
String primaryKey = "20170807192734";
dbRef.child(primaryKey).child("cost").setValue("10");


Firebase veritabanimdaki orders tablosundaki "20170807192734" primary key e sahip olan kayitin costunu 10 olarak guncellemek istersem yazmam gereken kodlar yukaridaki gibi olmalidir.

6 Ağustos 2017 Pazar

ANDROID - FIREBASE KULLANIMI

Android uygulamalarimizda veritabani islemleri icin ve bircok islem icin (ornegin bildirim) Firebase kullanabiliriz.

bu uygulamada firebase i uygulamama ekledim. Veritabanina iki tane tablo ekledim. Users ve siparis olmak uzere 2 tablo. bu iki tabloya kayit ekleme ve user tablosundaki kayitlari cekerek list view de gosterme isini yaptim. kodlar github hesabimda yer almaktadir.