esoe
2 years ago
35 changed files with 1867 additions and 0 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,125 @@
@@ -0,0 +1,125 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package collectionexample; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileReader; |
||||
import java.nio.charset.Charset; |
||||
import java.util.ArrayList; |
||||
import java.util.Comparator; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.TreeSet; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class CollectionExample { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
//collectionInteger();
|
||||
|
||||
collectionPerson(); |
||||
} |
||||
|
||||
public static void collectionPerson(){ |
||||
System.out.println("-----------------ArrayList------------------"); |
||||
List<Person> intArray = new ArrayList<>(); |
||||
intArray.add(new Person("Boby", 11)); |
||||
intArray.add(new Person("Andrey", 4)); |
||||
intArray.add(new Person("Boby", 1)); |
||||
intArray.add(new Person("Boby", 10)); |
||||
intArray.add(new Person("Boby", 5)); |
||||
intArray.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------LinkedList------------------"); |
||||
List<Person> intLink = new LinkedList<>(intArray); |
||||
intLink.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------HashSet------------------"); |
||||
Set<Person> intHashSet = new HashSet<>(intArray); |
||||
intHashSet.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------TreeSet------------------"); |
||||
// Set<Person> intTreeSet = new TreeSet<>(new Comparator<Person>() {
|
||||
// @Override
|
||||
// public int compare(Person o1, Person o2) {
|
||||
// int res = o2.name.compareTo(o1.name);
|
||||
// if(res==0) res = o2.phone.compareTo(o1.phone);
|
||||
// return res;
|
||||
// }
|
||||
// });
|
||||
Set<Person> intTreeSet = new TreeSet<>((o1, o2) -> { |
||||
int res = o2.name.compareTo(o1.name); |
||||
if(res==0) res = o2.phone.compareTo(o1.phone); |
||||
return res; |
||||
}); |
||||
//Set<Person> intTreeSet = new TreeSet<>((o1, o2) -> o2.name.compareTo(o1.name));
|
||||
intTreeSet.addAll(intLink); |
||||
intTreeSet.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------HashMap------------------"); |
||||
Map<Integer, Person> maps = new HashMap<>(); |
||||
maps.put(intArray.get(0).phone, intArray.get(0)); |
||||
maps.put(intArray.get(1).phone, intArray.get(1)); |
||||
maps.put(intArray.get(2).phone, intArray.get(2)); |
||||
maps.put(intArray.get(3).phone, intArray.get(3)); |
||||
maps.put(intArray.get(4).phone, intArray.get(4)); |
||||
maps.forEach((e1, e2) -> {System.out.println(e1 + ": " + e2);}); |
||||
|
||||
} |
||||
|
||||
public static void collectionInteger(){ |
||||
System.out.println("-----------------ArrayList------------------"); |
||||
List<Integer> intArray = new ArrayList<>(); |
||||
intArray.add(50); |
||||
intArray.add(26); |
||||
intArray.add(26); |
||||
intArray.add(50); |
||||
intArray.add(50); |
||||
intArray.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------LinkedList------------------"); |
||||
List<Integer> intLink = new LinkedList<>(intArray); |
||||
intLink.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------HashSet------------------"); |
||||
Set<Integer> intHashSet = new HashSet<>(intArray); |
||||
intHashSet.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------TreeSet------------------"); |
||||
Set<Integer> intTreeSet = new TreeSet<>(intArray); |
||||
intTreeSet.forEach(System.out::println); |
||||
|
||||
System.out.println("-----------------HashMap------------------"); |
||||
Map<String, Integer> maps = new HashMap<>(); |
||||
maps.put("One", intArray.get(0)); |
||||
maps.put("Two", intArray.get(1)); |
||||
maps.put("Three", intArray.get(2)); |
||||
maps.put("Four", intArray.get(3)); |
||||
maps.put("Five", intArray.get(4)); |
||||
maps.forEach((e1, e2) -> {System.out.println(e1 + ": " + e2);}); |
||||
|
||||
System.out.println("-----------------LinkedHashMap------------------"); |
||||
Map<String, Integer> mapsLink = new LinkedHashMap<>(); |
||||
mapsLink.put("One", intArray.get(0)); |
||||
mapsLink.put("Two", intArray.get(1)); |
||||
mapsLink.put("Three", intArray.get(2)); |
||||
mapsLink.put("Four", intArray.get(3)); |
||||
mapsLink.put("Five", intArray.get(4)); |
||||
mapsLink.forEach((e1, e2) -> {System.out.println(e1 + ": " + e2);}); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,131 @@
@@ -0,0 +1,131 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package collectionexample2; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.HashSet; |
||||
import java.util.LinkedHashMap; |
||||
import java.util.LinkedList; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.SortedSet; |
||||
import java.util.TreeSet; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class CollectionExample2 { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
unique(); |
||||
//simple();
|
||||
|
||||
} |
||||
|
||||
private static void unique(){ |
||||
System.out.println("--------------------ArrayList-------------------------"); |
||||
List<User> arrayList = new ArrayList<>(); |
||||
arrayList.add(new User("Andrey", 15)); |
||||
arrayList.add(new User("Andrey", 20)); |
||||
arrayList.add(new User("Andrey", 15)); |
||||
arrayList.add(new User("Andrey", 16)); |
||||
System.out.println("contains=" + arrayList.contains(new User("Andrey", 15))); |
||||
arrayList.forEach(System.out::println); |
||||
|
||||
{ |
||||
System.out.println("--------------------LinkedList-------------------------"); |
||||
List<User> collection = new LinkedList<>(arrayList); |
||||
collection.forEach(System.out::println); |
||||
} |
||||
|
||||
System.out.println("--------------------HashSet-------------------------"); |
||||
{ |
||||
System.out.println("--------------------LinkedList-------------------------"); |
||||
Set<User> collection = new HashSet<>(arrayList); |
||||
collection.forEach(System.out::println); |
||||
} |
||||
|
||||
System.out.println("--------------------TreeSet-------------------------"); |
||||
{ |
||||
System.out.println("--------------------LinkedList-------------------------"); |
||||
SortedSet<User> collection = new TreeSet<>(arrayList); |
||||
collection.forEach(System.out::println); |
||||
} |
||||
|
||||
System.out.println("--------------------HashMap-------------------------"); |
||||
{ |
||||
Map<Integer, User> mapColl = new HashMap<>(); |
||||
mapColl.put(1, new User("Andrey", 15)); |
||||
mapColl.put(10, new User("Andrey", 15)); |
||||
mapColl.put(5, new User("Andrey", 20)); |
||||
mapColl.put(9, new User("Andrey", 16)); |
||||
System.out.println("contains=" + mapColl.containsValue(new User("Andrey", 15))); |
||||
mapColl.entrySet().forEach(e -> System.out.println(e)); |
||||
} |
||||
|
||||
System.out.println("--------------------LinkedHashMap-------------------------"); |
||||
{ |
||||
Map<Integer, User> mapColl = new LinkedHashMap<>(); |
||||
mapColl.put(1, new User("Andrey", 15)); |
||||
mapColl.put(10, new User("Andrey", 15)); |
||||
mapColl.put(5, new User("Andrey", 20)); |
||||
mapColl.put(9, new User("Andrey", 16)); |
||||
System.out.println("contains=" + mapColl.containsValue(new User("Andrey", 15))); |
||||
mapColl.entrySet().forEach(e -> System.out.println(e)); |
||||
} |
||||
} |
||||
|
||||
private static void simple(){ |
||||
System.out.println("--------------------ArrayList-------------------------"); |
||||
List<String> arrayList = new ArrayList<>(); |
||||
arrayList.add("Once"); |
||||
arrayList.add("Two"); |
||||
arrayList.add("Two"); |
||||
arrayList.add("Two"); |
||||
arrayList.add("Three"); |
||||
arrayList.add("Four"); |
||||
arrayList.add("Five"); |
||||
arrayList.add("Five"); |
||||
arrayList.forEach(System.out::println); |
||||
|
||||
System.out.println("--------------------LinkedList-------------------------"); |
||||
List<String> linkedList = new LinkedList<>(arrayList); |
||||
linkedList.forEach(System.out::println); |
||||
|
||||
System.out.println("--------------------HashSet-------------------------"); |
||||
Set<String> hashSet = new HashSet<>(arrayList); |
||||
hashSet.forEach(System.out::println); |
||||
|
||||
System.out.println("--------------------TreeSet-------------------------"); |
||||
SortedSet<String> sorted = new TreeSet<>(arrayList); |
||||
sorted.forEach(System.out::println); |
||||
|
||||
System.out.println("--------------------HashMap-------------------------"); |
||||
Map<Integer, String> mapColl = new HashMap<>(); |
||||
mapColl.put(1, "Once"); |
||||
mapColl.put(10, "Once"); |
||||
mapColl.put(5, "Two"); |
||||
mapColl.put(9, null); |
||||
mapColl.put(0, "Five"); |
||||
mapColl.entrySet().forEach(e -> System.out.println(e)); |
||||
|
||||
System.out.println("--------------------LinkedHashMap-------------------------"); |
||||
Map<Integer, String> hashMap = new LinkedHashMap<>(); |
||||
hashMap.put(1, "Once"); |
||||
hashMap.put(10, "Once"); |
||||
hashMap.put(5, "Two"); |
||||
hashMap.put(9, null); |
||||
hashMap.put(0, "Five"); |
||||
hashMap.entrySet().forEach(e -> System.out.println(e)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package collectionexample; |
||||
|
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Person implements Comparable<Person>{ |
||||
|
||||
public String name; |
||||
public Integer phone; |
||||
|
||||
public Person(String name, Integer phone) { |
||||
this.name = name; |
||||
this.phone = phone; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Person{" + "name=" + name + ", phone=" + phone + '}'; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int hash = 3; |
||||
hash = 37 * hash + Objects.hashCode(this.name); |
||||
hash = 37 * hash + Objects.hashCode(this.phone); |
||||
return hash; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj == null) { |
||||
return false; |
||||
} |
||||
if (getClass() != obj.getClass()) { |
||||
return false; |
||||
} |
||||
final Person other = (Person) obj; |
||||
if (!Objects.equals(this.name, other.name)) { |
||||
return false; |
||||
} |
||||
if (!Objects.equals(this.phone, other.phone)) { |
||||
return false; |
||||
} |
||||
if (!Objects.equals(this.hashCode(), other.hashCode())) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public int compareTo(Person person) { |
||||
int res = this.name.compareTo(person.name); |
||||
if(res==0) res = this.phone.compareTo(person.phone); |
||||
return res; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package collectionexample2; |
||||
|
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class User implements Comparable<User>{ |
||||
String name; |
||||
Integer age; |
||||
|
||||
public User(String name, Integer age) { |
||||
this.name = name; |
||||
this.age = age; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "User{" + "name=" + name + ", age=" + age + '}'; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int hash = 7; |
||||
hash = 37 * hash + Objects.hashCode(this.name); |
||||
hash = 37 * hash + Objects.hashCode(this.age); |
||||
return hash; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj == null) { |
||||
return false; |
||||
} |
||||
if (getClass() != obj.getClass()) { |
||||
return false; |
||||
} |
||||
final User other = (User) obj; |
||||
if (!Objects.equals(this.name, other.name)) { |
||||
return false; |
||||
} |
||||
if (!Objects.equals(this.age, other.age)) { |
||||
return false; |
||||
} |
||||
if (!Objects.equals(this.hashCode(), other.hashCode())) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public int compareTo(User o) { |
||||
int res = this.name.compareTo(o.name); |
||||
if(res == 0) res = this.age.compareTo(o.age); |
||||
return res; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,60 @@
@@ -0,0 +1,60 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package filereaderandwriter; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileReader; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class FileReaderAndWriter { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
File file = new File("Text.txt"); |
||||
if(!file.exists()) try { |
||||
file.createNewFile(); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(FileReaderAndWriter.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
|
||||
if(file.canWrite()){ |
||||
|
||||
String line = "Hello world!"; |
||||
try (FileWriter writer = new FileWriter(file, true)) { |
||||
writer.append("\n" + line); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(FileReaderAndWriter.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
|
||||
} |
||||
|
||||
if(file.canRead()){ |
||||
String line = ""; |
||||
try(FileReader reader = new FileReader(file)){ |
||||
|
||||
char[]buf = new char[5]; |
||||
int c =0; |
||||
while((c=reader.read(buf))!=-1){ |
||||
line += new String(buf, 0, c); |
||||
} |
||||
|
||||
}catch (IOException ex) { |
||||
Logger.getLogger(FileReaderAndWriter.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
System.out.println(line); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,49 @@
@@ -0,0 +1,49 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package filereaderandwriter2; |
||||
|
||||
import java.io.File; |
||||
import java.io.*; |
||||
import java.io.IOException; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class FileReaderAndWriter2 { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
File file = new File("SameFile.txt"); |
||||
if(!file.exists()) try { |
||||
file.createNewFile(); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(FileReaderAndWriter2.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
|
||||
String message = "Hello world"; |
||||
try (FileWriter writer = new FileWriter(file, true)) { |
||||
writer.append("\n" + message); |
||||
} catch (IOException ex) { |
||||
} |
||||
|
||||
try(FileReader reader = new FileReader(file)){ |
||||
StringBuffer buffer = new StringBuffer(); |
||||
char[] cbuf = new char[1024]; |
||||
int count = 0; |
||||
while((count = reader.read(cbuf)) != -1){ |
||||
buffer.append(new String(cbuf, 0, count)); |
||||
} |
||||
System.out.println(buffer.toString()); |
||||
}catch(IOException e) {} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class City extends Country{ |
||||
|
||||
} |
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Country { |
||||
|
||||
} |
@ -0,0 +1,17 @@
@@ -0,0 +1,17 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class District extends City{ |
||||
|
||||
public void methods(){ |
||||
|
||||
} |
||||
} |
@ -0,0 +1,62 @@
@@ -0,0 +1,62 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class RulesPECS { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
RulesPECS rules = new RulesPECS(); |
||||
|
||||
List<Country> countrys = new ArrayList<>(); |
||||
countrys.add(new Country()); |
||||
List<City> citys = new ArrayList<>(); |
||||
citys.add(new City()); |
||||
List<District> districts = new ArrayList<>(); |
||||
districts.add(new District()); |
||||
List<Street> streets = new ArrayList<>(); |
||||
streets.add(new Street()); |
||||
|
||||
rules.producer(countrys); |
||||
rules.producer(citys); |
||||
rules.producer(districts); |
||||
rules.producer(streets); |
||||
|
||||
rules.consumer(countrys); |
||||
rules.consumer(citys); |
||||
rules.consumer(districts); |
||||
rules.consumer(streets); |
||||
} |
||||
|
||||
public void producer (List<? extends District> list){ |
||||
list.add(new Country()); |
||||
list.add(new City()); |
||||
list.add(new District()); |
||||
list.add(new Street()); |
||||
} |
||||
|
||||
public void consumer (List<? super District> list){ |
||||
Object district = list.get(0); |
||||
if(district instanceof Country) System.out.println("Object is Country"); |
||||
if(district instanceof City) System.out.println("Object is City"); |
||||
if(district instanceof District) System.out.println("Object is District"); |
||||
if(district instanceof Street) System.out.println("Object is Street"); |
||||
list.add(new Country()); |
||||
list.add(new City()); |
||||
list.add(new District()); |
||||
list.add(new Street()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Street extends District{ |
||||
|
||||
} |
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class City extends Country{ |
||||
protected String nameCity; |
||||
|
||||
public City(String nameCity, String nameCountry) { |
||||
super(nameCountry); |
||||
this.nameCity = nameCity; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "City{" + "nameCountry=" + nameCountry + "nameCity=" + nameCity + '}'; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Country { |
||||
protected String nameCountry; |
||||
|
||||
public Country(String nameCountry) { |
||||
this.nameCountry = nameCountry; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Country{" + "nameCountry=" + nameCountry + '}'; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class District extends City{ |
||||
|
||||
protected String nameDistrict; |
||||
|
||||
public District(String nameDistrict, String nameCity, String nameCountry) { |
||||
super(nameCity, nameCountry); |
||||
this.nameDistrict = nameDistrict; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "District{" + "nameCountry=" + nameCountry + "nameCity=" + nameCity + "nameDistrict=" + nameDistrict + '}'; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs2; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class RulesPECS2 { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
RulesPECS2 pecs = new RulesPECS2(); |
||||
List<Country> countrys = new ArrayList<>(); |
||||
pecs.consumerCity(countrys); |
||||
pecs.consumerStreet(countrys); |
||||
pecs.consumerDistrict(countrys); |
||||
|
||||
countrys.forEach(System.out::println); |
||||
} |
||||
|
||||
|
||||
public void producer(List<? extends District> list){ |
||||
|
||||
} |
||||
|
||||
public void consumerStreet(List<? super Street> list){ |
||||
list.add(new Street("Ленинский пр.", "Кировский", "Санкт-Петербург", "Россия")); |
||||
list.add(new Street("Московский пр.", "Московский", "Санкт-Петербург", "Россия")); |
||||
list.add(new Street("Стачек пр.", "Кировский", "Санкт-Петербург", "Россия")); |
||||
} |
||||
|
||||
public void consumerDistrict(List<? super District> list){ |
||||
list.add(new District("Невский", "Санкт-Петербург", "Россия")); |
||||
} |
||||
|
||||
public void consumerCity(List<? super City> list){ |
||||
list.add(new City("Санкт-Петербург", "Россия")); |
||||
} |
||||
|
||||
public void method(List<District> list){ |
||||
|
||||
} |
||||
} |
@ -0,0 +1,30 @@
@@ -0,0 +1,30 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package rulespecs2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Street extends District{ |
||||
|
||||
protected String nameStreet; |
||||
|
||||
public Street(String nameStreet, String nameDistrict, String nameCity, String nameCountry) { |
||||
super(nameDistrict, nameCity, nameCountry); |
||||
this.nameStreet = nameStreet; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Street{" + "nameCountry=" + nameCountry + "nameCity=" + nameCity + "nameDistrict=" + nameDistrict + "nameStreet=" + nameStreet + '}'; |
||||
} |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,57 @@
@@ -0,0 +1,57 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesexample; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileReader; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.util.Properties; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class PropertiesExample { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
File fileProp = new File("prop.properties"); |
||||
if(!fileProp.exists()) try { |
||||
fileProp.createNewFile(); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(PropertiesExample.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
|
||||
Properties properties = new Properties(); |
||||
try { |
||||
properties.load(new FileReader(fileProp)); |
||||
} catch (FileNotFoundException ex) { |
||||
Logger.getLogger(PropertiesExample.class.getName()).log(Level.SEVERE, null, ex); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(PropertiesExample.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
|
||||
// properties.put("key", "key value");
|
||||
// properties.put("key", "key value2");
|
||||
// properties.put("key2", "key value2");
|
||||
|
||||
System.out.println(properties.getProperty("nameK", "Key is not exist")); |
||||
|
||||
try { |
||||
properties.store(new FileWriter(fileProp), null); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(PropertiesExample.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
|
||||
} |
||||
|
||||
} |
@ -0,0 +1,99 @@
@@ -0,0 +1,99 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing; |
||||
|
||||
import java.awt.BorderLayout; |
||||
import java.awt.Container; |
||||
import java.awt.Dimension; |
||||
import java.awt.FlowLayout; |
||||
import java.awt.GridBagConstraints; |
||||
import java.awt.GridBagLayout; |
||||
import java.awt.GridLayout; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
import javax.swing.JButton; |
||||
import javax.swing.JFrame; |
||||
import javax.swing.JLabel; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.JPasswordField; |
||||
import javax.swing.JTextField; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class AuthorizationFrame extends JFrame{ |
||||
|
||||
|
||||
|
||||
public AuthorizationFrame(){ |
||||
init(); |
||||
setTitle("Authorization"); |
||||
setLocationRelativeTo(null); |
||||
setSize(400, 250); |
||||
//pack();
|
||||
setResizable(false); |
||||
|
||||
setVisible(true); |
||||
} |
||||
|
||||
private void init(){ |
||||
// Создаём верхнюю метку "Autorization" и размещаем её в панели "northPanel"
|
||||
JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 40, 20)); |
||||
JLabel authLabel = new JLabel("Authorization"); |
||||
northPanel.add(authLabel); |
||||
|
||||
GridBagLayout gridLayout = new GridBagLayout(); |
||||
GridBagConstraints constraints = new GridBagConstraints(); |
||||
JPanel centerPanel = new JPanel(gridLayout); |
||||
JLabel usernameLabel = new JLabel("username"); |
||||
JTextField usernameField = new JTextField(30); |
||||
JLabel passwordLabel = new JLabel("password"); |
||||
JPasswordField passwordField = new JPasswordField(30); |
||||
constraints.gridx = 0; |
||||
constraints.gridy = 0; |
||||
centerPanel.add(usernameLabel, constraints); |
||||
constraints.gridx = 1; |
||||
constraints.gridy = 0; |
||||
centerPanel.add(usernameField, constraints); |
||||
constraints.gridx = 0; |
||||
constraints.gridy = 1; |
||||
centerPanel.add(passwordLabel, constraints); |
||||
constraints.gridx = 1; |
||||
constraints.gridy = 1; |
||||
centerPanel.add(passwordField, constraints); |
||||
|
||||
//Создаём две кнопки "SingIn" и "Cancel" и размещаем их в панели "southPanel"
|
||||
JButton singinButton = new JButton("SingIn"); |
||||
singinButton.addActionListener(e -> { |
||||
Set<Person> persons = PersonStore.getPersonStore().getPersons(); |
||||
persons.forEach(person -> { |
||||
if(usernameField.getText().equals(person.getUsername()) && |
||||
passwordField.getText().equals(person.getPassword())){ |
||||
new MainFrame(); |
||||
} |
||||
}); |
||||
}); |
||||
JButton cancelButton = new JButton("Cancel"); |
||||
cancelButton.addActionListener(e -> { |
||||
System.exit(0); |
||||
}); |
||||
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 40, 20)); |
||||
southPanel.add(singinButton); |
||||
southPanel.add(cancelButton); |
||||
|
||||
|
||||
//Помещаем панели в главном контейнере
|
||||
getContentPane().setLayout(new BorderLayout()); |
||||
getContentPane().add(northPanel, BorderLayout.NORTH); |
||||
getContentPane().add(centerPanel, BorderLayout.CENTER); |
||||
getContentPane().add(southPanel, BorderLayout.SOUTH); |
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,124 @@
@@ -0,0 +1,124 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing; |
||||
|
||||
import java.awt.Container; |
||||
import java.awt.FlowLayout; |
||||
import java.awt.GridLayout; |
||||
import java.awt.Label; |
||||
import java.io.File; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileReader; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.util.Properties; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
import javax.swing.JButton; |
||||
import javax.swing.JFileChooser; |
||||
import javax.swing.JFrame; |
||||
import javax.swing.JLabel; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.JTextField; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class MainFrame extends JFrame{ |
||||
|
||||
private File file; |
||||
private Properties properties; |
||||
private JTextField currentField = new JTextField(50); |
||||
private JTextField powerField = new JTextField(50); |
||||
private JTextField frecField = new JTextField(50); |
||||
|
||||
public MainFrame(){ |
||||
setTitle("Propetries"); |
||||
setSize(600, 600); |
||||
Container container = getContentPane(); |
||||
GridLayout layout = new GridLayout(0, 1); |
||||
container.setLayout(layout); |
||||
container.add(initManageButtons()); |
||||
container.add(initFields("Current: ", currentField)); |
||||
container.add(initFields("Power: ", powerField)); |
||||
container.add(initFields("Frec: ", frecField)); |
||||
|
||||
setLocationRelativeTo(null); |
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
||||
pack(); |
||||
setVisible(true); |
||||
} |
||||
|
||||
private JPanel initFields(String textLabel, JTextField field){ |
||||
JPanel panel = new JPanel(new FlowLayout()); |
||||
JLabel label = new JLabel(textLabel); |
||||
panel.add(label); |
||||
panel.add(field); |
||||
return panel; |
||||
} |
||||
|
||||
private void initFileChooser(){ |
||||
JFileChooser chooser = new JFileChooser(); |
||||
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); |
||||
int result = chooser.showOpenDialog(this); |
||||
if(result == JFileChooser.APPROVE_OPTION){ |
||||
file = chooser.getSelectedFile(); |
||||
} |
||||
} |
||||
|
||||
private JPanel initManageButtons(){ |
||||
JPanel panel = new JPanel(new FlowLayout(10, 20, 20)); |
||||
JButton openButton = new JButton("Open file"); |
||||
panel.add(openButton); |
||||
openButton.addActionListener(e -> { |
||||
initFileChooser(); |
||||
if(file!=null && file.exists() && file.canRead()){ |
||||
loadProperties(); |
||||
if(properties!=null) { |
||||
if(currentField!=null) currentField.setText(properties.getProperty("Current")); |
||||
if(powerField!=null) powerField.setText(properties.getProperty("Power")); |
||||
if(frecField!=null) frecField.setText(properties.getProperty("Frec")); |
||||
} |
||||
} |
||||
}); |
||||
JButton saveButton = new JButton("Save file"); |
||||
panel.add(saveButton); |
||||
saveButton.addActionListener(e -> { |
||||
if(file!=null && properties!=null){ |
||||
properties.put("Current", currentField.getText()); |
||||
properties.put("Power", powerField.getText()); |
||||
properties.put("Frec", frecField.getText()); |
||||
saveProperties(); |
||||
} |
||||
}); |
||||
return panel; |
||||
} |
||||
|
||||
|
||||
|
||||
private void loadProperties(){ |
||||
properties = new Properties(); |
||||
try { |
||||
properties.load(new FileReader(file)); |
||||
} catch (FileNotFoundException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
} |
||||
|
||||
private void saveProperties(){ |
||||
if(properties!=null){ |
||||
try { |
||||
properties.store(new FileWriter(file), null); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Person implements Serializable{ |
||||
|
||||
|
||||
|
||||
private String username; |
||||
private String password; |
||||
|
||||
public Person(String username, String password) { |
||||
this.username = username; |
||||
this.password = password; |
||||
} |
||||
|
||||
public String getUsername() { |
||||
return username; |
||||
} |
||||
|
||||
public void setUsername(String username) { |
||||
this.username = username; |
||||
} |
||||
|
||||
public String getPassword() { |
||||
return password; |
||||
} |
||||
|
||||
public void setPassword(String password) { |
||||
this.password = password; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "Person{" + "username=" + username + ", password=" + password + '}'; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int hash = 3; |
||||
hash = 67 * hash + Objects.hashCode(this.username); |
||||
return hash; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj == null) { |
||||
return false; |
||||
} |
||||
if (getClass() != obj.getClass()) { |
||||
return false; |
||||
} |
||||
final Person other = (Person) obj; |
||||
if (!Objects.equals(this.username, other.username) && hashCode()!=Objects.hashCode(obj)) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.ObjectInputStream; |
||||
import java.io.ObjectOutputStream; |
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
import java.util.List; |
||||
import java.util.Set; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public final class PersonStore { |
||||
private static Set<Person> persons = new HashSet<>(); |
||||
private static PersonStore personStore; |
||||
private File file; |
||||
|
||||
private PersonStore(){ |
||||
file = new File("persons.txt"); |
||||
if(!file.exists()) try { |
||||
file.createNewFile(); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(PropertiesSwing.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
readStore(); |
||||
} |
||||
|
||||
public static PersonStore getPersonStore(){ |
||||
if (personStore == null) personStore = new PersonStore(); |
||||
return personStore; |
||||
} |
||||
|
||||
public void addPerson(Person person){ |
||||
persons.add(person); |
||||
saveStore(); |
||||
} |
||||
|
||||
public HashSet<Person> getPersons(){ |
||||
return new HashSet<>(persons); |
||||
} |
||||
|
||||
private void readStore(){ |
||||
if(file==null || !file.canRead()) return; |
||||
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { |
||||
Object result = ois.readObject(); |
||||
persons = result!=null ? (HashSet<Person>)result : new HashSet<>(); |
||||
} catch (Exception ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
private void saveStore(){ |
||||
if(file==null || !file.canRead()) return; |
||||
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { |
||||
oos.writeObject(persons); |
||||
} catch (FileNotFoundException ex) { |
||||
Logger.getLogger(PropertiesSwing.class.getName()).log(Level.SEVERE, null, ex); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(PropertiesSwing.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class PropertiesSwing { |
||||
|
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
|
||||
// PersonStore personStore = PersonStore.getPersonStore();
|
||||
// personStore.addPerson(new Person("Andrey", "Andrey"));
|
||||
javax.swing.SwingUtilities.invokeLater(new Runnable() { |
||||
public void run() { |
||||
new AuthorizationFrame(); |
||||
} |
||||
}); |
||||
//new AuthorizationFrame();
|
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,104 @@
@@ -0,0 +1,104 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing2; |
||||
|
||||
import java.awt.Container; |
||||
import java.awt.FlowLayout; |
||||
import java.awt.GridBagConstraints; |
||||
import java.awt.GridBagLayout; |
||||
import java.awt.HeadlessException; |
||||
import javax.swing.JButton; |
||||
import javax.swing.JFrame; |
||||
import javax.swing.JLabel; |
||||
import javax.swing.JMenu; |
||||
import javax.swing.JMenuBar; |
||||
import javax.swing.JMenuItem; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.JPasswordField; |
||||
import javax.swing.JTextField; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class AuthenticationFrame extends JFrame{ |
||||
|
||||
public AuthenticationFrame() { |
||||
|
||||
setTitle("Authentication"); |
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
||||
init(); |
||||
setSize(500, 200); |
||||
setResizable(false); |
||||
setLocationRelativeTo(null); |
||||
//pack();
|
||||
setVisible(true); |
||||
} |
||||
|
||||
private void init() { |
||||
JMenuBar bar = new JMenuBar(); |
||||
setJMenuBar(bar); |
||||
JMenu menu = new JMenu("File"); |
||||
bar.add(menu); |
||||
JMenuItem openItem = new JMenuItem("Open"); |
||||
JMenuItem closeItem = new JMenuItem("Close"); |
||||
menu.add(openItem); |
||||
menu.add(closeItem); |
||||
closeItem.addActionListener(e -> { |
||||
System.exit(0); |
||||
}); |
||||
|
||||
|
||||
|
||||
GridBagLayout gbl = new GridBagLayout(); |
||||
GridBagConstraints constraints = new GridBagConstraints(); |
||||
Container container = getContentPane(); |
||||
container.setLayout(gbl); |
||||
|
||||
|
||||
constraints.gridx = 1; |
||||
constraints.gridy = 0; |
||||
JLabel authLabel = new JLabel("Authorization"); |
||||
container.add(authLabel, constraints); |
||||
|
||||
JLabel usernameLanbel = new JLabel("username"); |
||||
JTextField usernameText = new JTextField(30); |
||||
constraints.gridx = 0; |
||||
constraints.gridy = 1; |
||||
container.add(usernameLanbel, constraints); |
||||
constraints.gridx = 1; |
||||
constraints.gridy = 1; |
||||
container.add(usernameText, constraints); |
||||
|
||||
JLabel passwordLanbel = new JLabel("password"); |
||||
JPasswordField passwordText = new JPasswordField(30); |
||||
constraints.gridx = 0; |
||||
constraints.gridy = 2; |
||||
container.add(passwordLanbel, constraints); |
||||
constraints.gridx = 1; |
||||
constraints.gridy = 2; |
||||
container.add(passwordText, constraints); |
||||
|
||||
JButton singIn = new JButton("SingIn"); |
||||
JButton cancel = new JButton("Cancel"); |
||||
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 30, 5)); |
||||
constraints.gridx = 1; |
||||
constraints.gridy = 3; |
||||
container.add(panel, constraints); |
||||
panel.add(singIn); |
||||
panel.add(cancel); |
||||
|
||||
singIn.addActionListener(e -> { |
||||
UserCheck check = new UserCheck(); |
||||
if(check.check(usernameText.getText(), passwordText.getText())) System.out.println("Авторизация успешна"); |
||||
else System.out.println("Имя пользователя или пароль не верны"); |
||||
}); |
||||
|
||||
cancel.addActionListener(e -> { |
||||
System.exit(0); |
||||
}); |
||||
} |
||||
} |
@ -0,0 +1,162 @@
@@ -0,0 +1,162 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing2; |
||||
|
||||
import java.awt.*; |
||||
import java.awt.event.ActionListener; |
||||
import java.io.File; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileReader; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.util.Properties; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
import javax.swing.JButton; |
||||
import javax.swing.JFrame; |
||||
import javax.swing.JLabel; |
||||
import javax.swing.JPanel; |
||||
import javax.swing.JTextField; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class MainFrame extends JFrame{ |
||||
private Properties property; |
||||
private File file; |
||||
private JTextField urlField; |
||||
private JTextField usernameField; |
||||
private JTextField passwordField; |
||||
|
||||
public MainFrame(){ |
||||
setTitle("Properties"); |
||||
Container root = getContentPane(); |
||||
root.setLayout(new BorderLayout(20, 20)); |
||||
|
||||
//Добавляем группу кнопок
|
||||
root.add(createButtons(), BorderLayout.NORTH); |
||||
//Добавляем группу меток и текстовых полей
|
||||
root.add(createDataArea()); |
||||
|
||||
|
||||
setSize(800, 300); |
||||
setDefaultCloseOperation(DISPOSE_ON_CLOSE); |
||||
setLocationRelativeTo(null); |
||||
setResizable(false); |
||||
setVisible(true); |
||||
} |
||||
|
||||
private JPanel createButtons(){ |
||||
JPanel panel = new JPanel(new FlowLayout()); |
||||
JButton loadButton = new JButton("LOAD"); |
||||
JButton saveButton = new JButton("SAVE"); |
||||
panel.add(loadButton); |
||||
panel.add(saveButton); |
||||
|
||||
loadButton.addActionListener(e -> { |
||||
loadProperties(); |
||||
if(property!=null){ |
||||
if (urlField!=null) urlField.setText(property.getProperty("URL", "")); |
||||
if (usernameField!=null) usernameField.setText(property.getProperty("username", "")); |
||||
if (passwordField!=null) passwordField.setText(property.getProperty("password", "")); |
||||
} |
||||
}); |
||||
|
||||
saveButton.addActionListener(e -> { |
||||
String url = urlField!=null ? urlField.getText() : ""; |
||||
String username = usernameField!=null ? usernameField.getText() : ""; |
||||
String password = passwordField!=null ? passwordField.getText() : ""; |
||||
if(property!=null){ |
||||
property.put("URL", url); |
||||
property.put("username", username); |
||||
property.put("password", password); |
||||
} |
||||
saveProperties(); |
||||
}); |
||||
|
||||
return panel; |
||||
} |
||||
|
||||
private JPanel createDataArea(){ |
||||
JPanel panel = new JPanel(new GridBagLayout()); |
||||
GridBagConstraints constraints = new GridBagConstraints(); |
||||
|
||||
JLabel urlLabel = new JLabel("URL:"); |
||||
urlField = new JTextField(50); |
||||
|
||||
constraints.gridx = 0; |
||||
constraints.gridy = 0; |
||||
panel.add(urlLabel, constraints); |
||||
|
||||
constraints.gridx = 1; |
||||
constraints.gridy = 0; |
||||
panel.add(urlField, constraints); |
||||
|
||||
JLabel usernameLabel = new JLabel("username:"); |
||||
usernameField = new JTextField(50); |
||||
|
||||
constraints.gridx = 0; |
||||
constraints.gridy = 1; |
||||
panel.add(usernameLabel, constraints); |
||||
|
||||
constraints.gridx = 1; |
||||
constraints.gridy = 1; |
||||
panel.add(usernameField, constraints); |
||||
|
||||
JLabel passwordLabel = new JLabel("password:"); |
||||
passwordField = new JTextField(50); |
||||
|
||||
constraints.gridx = 0; |
||||
constraints.gridy = 2; |
||||
panel.add(passwordLabel, constraints); |
||||
|
||||
constraints.gridx = 1; |
||||
constraints.gridy = 2; |
||||
panel.add(passwordField, constraints); |
||||
|
||||
return panel; |
||||
} |
||||
|
||||
private void loadProperties(){ |
||||
property = new Properties(); |
||||
if(file==null) file = new File("C:\\PropDir\\prop2.properties"); |
||||
//Проверяем существование файла
|
||||
if(!file.exists()) { |
||||
try { |
||||
file.createNewFile(); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
} else { |
||||
if(file.canRead()){ |
||||
try (FileReader reader = new FileReader(file)) { |
||||
property.load(reader); |
||||
} catch (FileNotFoundException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
private void saveProperties(){ |
||||
if(file==null) file = new File("C:\\PropDir\\prop2.properties"); |
||||
if(!file.exists()) try { |
||||
file.createNewFile(); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
if(file.canWrite()){ |
||||
try (FileWriter writer = new FileWriter(file)) { |
||||
property.store(writer, null); |
||||
} catch (IOException ex) { |
||||
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing2; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.FileOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.ObjectInputStream; |
||||
import java.io.ObjectOutputStream; |
||||
import java.util.HashSet; |
||||
import java.util.Set; |
||||
import java.util.logging.Level; |
||||
import java.util.logging.Logger; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class PersonStore { |
||||
private static Set<User> persons = new HashSet<>(); |
||||
private static PersonStore personStore; |
||||
private File file; |
||||
|
||||
private PersonStore(){ |
||||
file = new File("persons.txt"); |
||||
if(!file.exists()) try { |
||||
file.createNewFile(); |
||||
} catch (IOException ex) { |
||||
ex.printStackTrace(); |
||||
}else readStore(); |
||||
} |
||||
|
||||
public static PersonStore getPersonStore(){ |
||||
if (personStore == null) personStore = new PersonStore(); |
||||
return personStore; |
||||
} |
||||
|
||||
public void addUser(User user){ |
||||
persons.add(user); |
||||
saveStore(); |
||||
} |
||||
|
||||
public HashSet<User> getPersons(){ |
||||
return new HashSet<>(persons); |
||||
} |
||||
|
||||
private void readStore(){ |
||||
if(file==null || !file.canRead()) return; |
||||
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { |
||||
Object result = ois.readObject(); |
||||
persons = result!=null ? (HashSet<User>)result : new HashSet<>(); |
||||
} catch (Exception ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
private void saveStore(){ |
||||
if(file==null || !file.canRead()) return; |
||||
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) { |
||||
oos.writeObject(persons); |
||||
} catch (FileNotFoundException ex) { |
||||
ex.printStackTrace(); |
||||
} catch (IOException ex) { |
||||
ex.printStackTrace(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class PropertiesSwing2 { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
PersonStore store = PersonStore.getPersonStore(); |
||||
store.getPersons().forEach(System.out::println); |
||||
//new MainFrame();
|
||||
|
||||
new AuthenticationFrame(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing2; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Objects; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class User implements Serializable{ |
||||
private String username; |
||||
private String password; |
||||
|
||||
public User(String username, String password) { |
||||
this.username = username; |
||||
this.password = password; |
||||
} |
||||
|
||||
public String getUsername() { |
||||
return username; |
||||
} |
||||
|
||||
public String getPassword() { |
||||
return password; |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
int hash = 3; |
||||
hash = 53 * hash + Objects.hashCode(this.username); |
||||
return hash; |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (obj == null) { |
||||
return false; |
||||
} |
||||
if (getClass() != obj.getClass()) { |
||||
return false; |
||||
} |
||||
final User other = (User) obj; |
||||
if (!Objects.equals(this.username, other.username)) { |
||||
return false; |
||||
} |
||||
if (!Objects.equals(this.hashCode(), other.hashCode()) ) { |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return "User{" + "username=" + username + ", password=" + password + '}'; |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,21 @@
@@ -0,0 +1,21 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package propertiesswing2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class UserCheck { |
||||
|
||||
public boolean check(String username, String password){ |
||||
if(username==null || username.isEmpty() || password == null || password.isEmpty()) return false; |
||||
for (User user : PersonStore.getPersonStore().getPersons()){ |
||||
if(user.getUsername().equals(username) && user.getPassword().equals(password)) return true; |
||||
} |
||||
return false; |
||||
} |
||||
} |
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package dev_j2022_2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Dev_j2022_2 { |
||||
|
||||
/** |
||||
* @param args the command line arguments |
||||
*/ |
||||
public static void main(String[] args) { |
||||
Device[] devices = new Device[5]; |
||||
String line = " Hello"; |
||||
devices[0]=new ElectronicDevice(); |
||||
devices[1]=new MechanicalDevice(); |
||||
devices[2]=new ElectronicDevice(); |
||||
devices[3]=new MechanicalDevice(); |
||||
devices[4]=new Device(); |
||||
|
||||
for(int i=0 ; i<devices.length ; i++){ |
||||
devices[i].print(); |
||||
} |
||||
ElectronicDevice e1 = new ElectronicDevice(); |
||||
Device e2 = new ElectronicDevice(); |
||||
((ElectronicDevice)e2).setPower(15); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,68 @@
@@ -0,0 +1,68 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package dev_j2022_2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class Device { |
||||
|
||||
private int length; |
||||
private int width; |
||||
private int height; |
||||
|
||||
public Device(){ |
||||
length = 9; |
||||
width = 9; |
||||
height = 9; |
||||
} |
||||
|
||||
public Device(int length, int width) { |
||||
this.length = length; |
||||
this.width = width; |
||||
} |
||||
|
||||
public Device(int length, int width, int height) { |
||||
this.length = length; |
||||
this.width = width; |
||||
this.height = height; |
||||
} |
||||
|
||||
public int getLength(){ |
||||
return length; |
||||
} |
||||
public void setLength(int length){ |
||||
if(length>0) this.length = length; |
||||
else throw new IllegalArgumentException("Значение length должно быть больше 0"); |
||||
} |
||||
|
||||
public int getWidth() { |
||||
return width; |
||||
} |
||||
|
||||
public void setWidth(int width) { |
||||
this.width = width; |
||||
} |
||||
|
||||
public int getHeight() { |
||||
return height; |
||||
} |
||||
|
||||
public void setHeight(int height) { |
||||
this.height = height; |
||||
} |
||||
|
||||
public void print(){ |
||||
System.out.println("Device: demention=" + length + "/" + width + "/" + height); |
||||
} |
||||
|
||||
public void printAll(Device[] devices){ |
||||
for(int i=0 ; i<devices.length ; i++){ |
||||
devices[i].print(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,48 @@
@@ -0,0 +1,48 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package dev_j2022_2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class ElectronicDevice extends Device{ |
||||
private int current; |
||||
private int voltage; |
||||
private int power; |
||||
|
||||
@Override |
||||
public void print() { |
||||
System.out.println("Device: demention=" + getLength() + "/" + getWidth() + "/" + getHeight() + |
||||
", electronicCharacter=" + voltage + "V"); |
||||
} |
||||
|
||||
public int getCurrent() { |
||||
return current; |
||||
} |
||||
|
||||
public void setCurrent(int current) { |
||||
this.current = current; |
||||
} |
||||
|
||||
public int getVoltage() { |
||||
return voltage; |
||||
} |
||||
|
||||
public void setVoltage(int voltage) { |
||||
this.voltage = voltage; |
||||
} |
||||
|
||||
public int getPower() { |
||||
return power; |
||||
} |
||||
|
||||
public void setPower(int power) { |
||||
this.power = power; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
/* |
||||
* To change this license header, choose License Headers in Project Properties. |
||||
* To change this template file, choose Tools | Templates |
||||
* and open the template in the editor. |
||||
*/ |
||||
package dev_j2022_2; |
||||
|
||||
/** |
||||
* |
||||
* @author denis |
||||
*/ |
||||
public class MechanicalDevice extends Device{ |
||||
private String name = "velo"; |
||||
|
||||
@Override |
||||
public void print() { |
||||
System.out.println("Device: demention=" + getLength() + "/" + getWidth() + "/" + getHeight() + |
||||
", typeDevice=" + name); |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue