kilam_deelaw

generic and file handling

May 30th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. package employee;
  2. import java.util.*;
  3. import java.io.*;
  4. public class Employee <U,V> implements Comparable <Employee> {
  5. private U a;
  6. private V b;
  7.  
  8. public Employee(U a, V b) {
  9. this.a = a;
  10. this.b = b;
  11. }
  12.  
  13. public U getA() {
  14. return a;
  15. }
  16.  
  17. public void setA(U a) {
  18. this.a = a;
  19. }
  20.  
  21. public V getB() {
  22. return b;
  23. }
  24.  
  25. public void setB(V b) {
  26. this.b = b;
  27. }
  28.  
  29.  
  30. public static void main(String[] args) {
  31. ArrayList <Employee> arr = new ArrayList<Employee>();
  32. arr.add (new Employee ("Ali",1));
  33. arr.add (new Employee ("Ahmad",2));
  34. arr.add (new Employee ("Xhmad",5));
  35. arr.add(new Employee (2,"Saim"));
  36. arr.add(new Employee (1,"Xaim"));
  37. arr.add(new Employee (9,"Gaim"));
  38. Collections.sort(arr);
  39. for(Employee x:arr)
  40. System.out.println(x.getA()+" "+x.getB());
  41. }
  42.  
  43. @Override
  44. public int compareTo(Employee o) {
  45. String one="",two="";
  46. int a=0,b=0;
  47. if (o.getA() instanceof String && this.getA() instanceof String){
  48. one = (String) o.getA();
  49. two = (String) this.getA();
  50. return two.compareTo(one);
  51. }
  52. /* else
  53. if (o.getB() instanceof String && this.getB() instanceof String){
  54. one = (String) o.getB();
  55. two = (String) this.getB();
  56. return one.compareTo(two);
  57. }*/
  58. /* else
  59. if (o.getA() instanceof String && this.getB() instanceof String){
  60. one = (String) o.getA();
  61. two = (String) this.getB();
  62. return one.compareTo(two);
  63. }*/
  64. if(o.getA() instanceof Integer && this.getA() instanceof Integer)
  65. {
  66. a=(int) o.getA();
  67. //System.out.println(this.getA());
  68. b=Integer.parseInt((String.valueOf(this.getA())));
  69. return b-a;
  70. }
  71. return 0;
  72. }
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
  79. Upper to Lower, Lower to Upper
  80. package st2;
  81. public class St2 {
  82.  
  83. public static void main(String[] args) {
  84.  
  85. String s="HelLo woRld @ 2017";
  86. int i;
  87. char[]c=s.toCharArray();
  88. char[] k=new char[100];
  89. int a=c.length;
  90. for(i=0;i<a;i++)
  91. {
  92. if(Character.isUpperCase(c[i]))
  93. {
  94. c[i]=Character.toLowerCase(c[i]);
  95. }
  96. else if(Character.isLowerCase(c[i]))
  97. {
  98. c[i]= Character.toUpperCase(c[i]);
  99. }
  100. }
  101. for(i=0;i<a;i++)
  102. {
  103. System.out.print(c[i]);
  104. }
  105. }
  106. }
  107.  
  108.  
  109.  
  110.  
  111. import java.io.*;
  112. import java.util.ArrayList;
  113. import java.util.Arrays;
  114. import java.util.Collections;
  115. import java.util.StringTokenizer;
  116. /**
  117. * Created by test on 3/14/2017.
  118. */
  119. public class Main{
  120. public static void main(String[] args) throws Exception {
  121. File country = new File("out\\country.txt");
  122. File capital = new File("out\\capital.txt");
  123. File combine = new File("out\\combine.txt");
  124. FileReader f1 = new FileReader(country);
  125. BufferedReader bf1 = new BufferedReader(f1);
  126. String str = new String();
  127. ArrayList<String> list = new ArrayList<String>();
  128. int i = 0;
  129. while((str = bf1.readLine()) != null) {
  130. list.add(str);
  131. i++;
  132. }
  133. Collections.sort(list);
  134. bf1.close();
  135. f1.close();
  136. FileWriter wcountry= new FileWriter(country);
  137. BufferedWriter bwcountry = new BufferedWriter(wcountry);
  138. for (String ss: list) {
  139. bwcountry.write(ss + "\n");
  140. }
  141. bwcountry.close();
  142. wcountry.close();
  143. FileWriter w1 = new FileWriter(capital);
  144. BufferedWriter bw1 = new BufferedWriter(w1);
  145. String delimiter = " ";
  146. String comb = new String();
  147. for (int k = 0; k < i; k++) {
  148. int j =0;
  149. FileReader f2 = new FileReader(combine);
  150. BufferedReader bf2 = new BufferedReader(f2);
  151. while((comb = bf2.readLine()) != null) {
  152. StringTokenizer st = new StringTokenizer(comb, delimiter);
  153. String name = st.nextToken();
  154. String name2 = null;
  155. if(name.equals(list.get(k))) {
  156. name2 = st.nextToken();
  157. bw1.write(name2 + "\n");
  158. }
  159. j++;
  160. }
  161. bf2.close();
  162. f2.close();
  163. }
  164. bw1.close();
  165. w1.close();
  166. }
Add Comment
Please, Sign In to add comment