diff --git a/LB05/LB05.iml b/LB05/LB05.iml new file mode 100644 index 0000000..50f8090 --- /dev/null +++ b/LB05/LB05.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/LB05/Test_Write_XML.xml b/LB05/Test_Write_XML.xml new file mode 100644 index 0000000..fdd0d5e --- /dev/null +++ b/LB05/Test_Write_XML.xml @@ -0,0 +1,5 @@ + + + 2 + 23 + diff --git a/LB05/bin/Main.class b/LB05/bin/Main.class new file mode 100644 index 0000000..628781e Binary files /dev/null and b/LB05/bin/Main.class differ diff --git a/LB05/bin/Organization/Address.class b/LB05/bin/Organization/Address.class new file mode 100644 index 0000000..2fd70d8 Binary files /dev/null and b/LB05/bin/Organization/Address.class differ diff --git a/LB05/bin/Organization/Coordinates.class b/LB05/bin/Organization/Coordinates.class new file mode 100644 index 0000000..a70f466 Binary files /dev/null and b/LB05/bin/Organization/Coordinates.class differ diff --git a/LB05/bin/Organization/Location.class b/LB05/bin/Organization/Location.class new file mode 100644 index 0000000..868fba8 Binary files /dev/null and b/LB05/bin/Organization/Location.class differ diff --git a/LB05/bin/Organization/Organization.class b/LB05/bin/Organization/Organization.class new file mode 100644 index 0000000..5a64421 Binary files /dev/null and b/LB05/bin/Organization/Organization.class differ diff --git a/LB05/bin/Organization/OrganizationType.class b/LB05/bin/Organization/OrganizationType.class new file mode 100644 index 0000000..a1dbdff Binary files /dev/null and b/LB05/bin/Organization/OrganizationType.class differ diff --git a/LB05/bin/Organizations.xml b/LB05/bin/Organizations.xml new file mode 100644 index 0000000..9cdb497 --- /dev/null +++ b/LB05/bin/Organizations.xml @@ -0,0 +1,61 @@ + + + + VkusnoiTochka + + 542 + 42 + + 2022-03-20 + 239.30 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + 1 + VkusnoiTochka + + 542 + 42 + + 2022-03-20 + 239.30 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + VkusnoiTochka + + 542 + 42 + + 2022-03-20 + 239.30 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + \ No newline at end of file diff --git a/LB05/bin/OutOrganizations.xml b/LB05/bin/OutOrganizations.xml new file mode 100644 index 0000000..96a4bb7 --- /dev/null +++ b/LB05/bin/OutOrganizations.xml @@ -0,0 +1,83 @@ + + + + 192693 + VkusnoiTochka + + 542 + 42.0 + + 2023-03-13T22:42:35.119597600 + 239.3 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + 1 + VkusnoiTochka + + 542 + 42.0 + + 2023-03-13T22:42:35.119597600 + 239.3 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + 1 + VkusnoiTochka + + 542 + 42.0 + + 2023-03-13T22:42:35.119597600 + 239.3 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + 698657280 + KFC + + 900 + 239.3 + + 2023-03-13T22:42:35.150842100 + 100000.0 + COMMERCIAL + + 23 + ulica + + 3 + 20 + Spb + + + + \ No newline at end of file diff --git a/LB05/bin/Test_Write_XML.class b/LB05/bin/Test_Write_XML.class new file mode 100644 index 0000000..12bcd89 Binary files /dev/null and b/LB05/bin/Test_Write_XML.class differ diff --git a/LB05/bin/Test_Write_XML.xml b/LB05/bin/Test_Write_XML.xml new file mode 100644 index 0000000..bdf0d77 --- /dev/null +++ b/LB05/bin/Test_Write_XML.xml @@ -0,0 +1,5 @@ + + + 2 + 23e + diff --git a/LB05/src/Auxiliary/FilterOrganizations.java b/LB05/src/Auxiliary/FilterOrganizations.java new file mode 100644 index 0000000..ef10320 --- /dev/null +++ b/LB05/src/Auxiliary/FilterOrganizations.java @@ -0,0 +1,14 @@ +package Auxiliary; + +import Organization.Organization; + +import java.util.Vector; +import java.util.stream.Collectors; + +public class FilterOrganizations { + public static Vector filterByType(Vector org, String type){ + Vector result = new Vector(); + result = org.stream().filter(o -> o.equals(type)).collect(Vector::new, Vector::add, Vector::addAll); + return result; + } +} diff --git a/LB05/src/Auxiliary/Read_XML.java b/LB05/src/Auxiliary/Read_XML.java new file mode 100644 index 0000000..dfab6a7 --- /dev/null +++ b/LB05/src/Auxiliary/Read_XML.java @@ -0,0 +1,137 @@ + + +package Auxiliary; +import Organization.*; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.*; +import static Auxiliary.SortOrganizaton.sort; + +public class Read_XML { + public static Vector CreateVector(String filename) { + File file = new File(filename); + Vector org = new Vector<>(); + ArrayList arr = new ArrayList(); + int i = 0; + String str = ""; + String[] lines; + int tId = (int)(Math.random() * 999999); + String tName = null; + Coordinates tCoordinates; //Поле не может быть null + long xC = 0; + double yC = 0; + LocalDateTime tCreationDate = LocalDateTime.now(); + Float tAnnualTurnover = null; + OrganizationType tType = null; + Address tPostalAddress; + String zipCode = null; + String street = null; + int xL = 0; + long yL = 0; + String town = null; + String lastUpdate = null; + + try (Scanner reader = new Scanner(new FileReader(file.getAbsolutePath()))) { + while (reader.hasNext()) { + str += reader.next(); + } + } + catch (Exception e){ + System.out.println("Not found input file"); + } + try { + lines = str.split("><"); + for (String el : lines) { + if (el.equalsIgnoreCase("organization")) { + arr.add(new HashMap()); + continue; + } + if (el.equalsIgnoreCase("/organization")) { + i += 1; + continue; + } + if (arr.size() > i) { + if (el.equalsIgnoreCase("coordinates")) + continue; + if (el.equalsIgnoreCase("/coordinates")) + continue; + if (el.equalsIgnoreCase("postaladdress")) + continue; + if (el.equalsIgnoreCase("/postaladdress")) + continue; + if (el.equalsIgnoreCase("location")) + continue; + if (el.equalsIgnoreCase("/location")) + continue; + arr.get(i).put(el.substring(0, el.indexOf('>')), el.substring(el.indexOf('>') + 1, el.indexOf('<'))); + } + } + + for (Map m : arr) { + for (String key : m.keySet()) { + final String value = m.get(key); + + switch (key) { + case "id": + tId = Integer.parseInt(value); + break; + case "name": + tName = value; + break; + case "xc" : + xC = Long.parseLong(value); + break; + case "yc": + yC = Double.parseDouble(value); + break; + case "date": + tCreationDate = LocalDate.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay(); + break; + case "annualTurnover": + tAnnualTurnover = Float.parseFloat(value); + break; + case "type": + tType = OrganizationType.valueOf(value); + break; + case "zipCode": + zipCode = value; + break; + case "street": + street = value; + break; + case "xl": + xL = Integer.parseInt(value); + break; + case "yl": + yL = Long.parseLong(value); + break; + case "town": + town = value; + break; + case "lastUpdate": + lastUpdate = value; + break; + default: + break; + } + } + Organization t = new Organization(tName, xC, yC, tCreationDate, tAnnualTurnover, tType, new Address(zipCode, street, new Location(xL, yL, town))); + t.setId(tId); + t.setLastUpdate(lastUpdate); + org.addElement(t); + } + } + catch(Exception e){ + System.out.println("Not download xml file"); + System.out.println(e.getMessage()); + e.printStackTrace(); + } + finally {sort(org); return org;} + + } +} diff --git a/LB05/src/Auxiliary/SortOrganizaton.java b/LB05/src/Auxiliary/SortOrganizaton.java new file mode 100644 index 0000000..76f74db --- /dev/null +++ b/LB05/src/Auxiliary/SortOrganizaton.java @@ -0,0 +1,29 @@ +package Auxiliary; + +import Organization.Organization; + +import java.util.Collections; +import java.util.Comparator; +import java.util.Vector; + +public class SortOrganizaton { + public static void sort(Vector org){ + sortByName(org); + } + public static void sortByName(Vector org){ + Collections.sort(org, Comparator.comparing(Organization::getName)); + } + public static void revSortByName(Vector org){ + Collections.sort(org, Comparator.comparing(Organization::getName).reversed()); + } + public static Vector sortByAddress(Vector org){ + Vector result = org; + Collections.sort(result, Comparator.comparing(Organization::getAddress)); + return result; + } + public static Vector revSortByAddress(Vector org){ + Vector result = org; + Collections.sort(result, Comparator.comparing(Organization::getAddress)); + return result; + } +} diff --git a/LB05/src/Auxiliary/Write_XML.java b/LB05/src/Auxiliary/Write_XML.java new file mode 100644 index 0000000..e8daab5 --- /dev/null +++ b/LB05/src/Auxiliary/Write_XML.java @@ -0,0 +1,27 @@ +package Auxiliary; + +import Organization.Organization; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Vector; + +public class Write_XML { + public static void Write(Vector vec, String filename){ + try { + FileOutputStream file = new FileOutputStream(filename); + file.write("\n\n".getBytes()); + for (Organization org : vec) + file.write(org.getXML().getBytes()); + file.write("".getBytes()); + file.close(); + } catch (FileNotFoundException e) { + //System.out.println("i dont find file"); + throw new RuntimeException(e); + } catch (IOException e) { + //System.out.println("kal"); + throw new RuntimeException(e); + } + } +} diff --git a/LB05/src/Commands/Add.java b/LB05/src/Commands/Add.java new file mode 100644 index 0000000..491daea --- /dev/null +++ b/LB05/src/Commands/Add.java @@ -0,0 +1,128 @@ +package Commands; + +import Auxiliary.SortOrganizaton; +import Organization.*; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Scanner; +import java.util.Vector; + +public class Add extends Command{ + private static final String name = "add"; + private final static String description = ": Add new organization to collection;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + /** + * + */ + public static void add(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + try { + System.out.print("Input name organization: "); + String name = scan.next(); + + System.out.print("Input coordinate 'x' organization: "); + Long xC = null; + for (;;) { + if (scan.hasNextLong()) { + xC = scan.nextLong(); + break; + } else { + System.out.print("Please input number in 'long' format: "); + scan.next(); + } + } + + System.out.print("Input coordinate 'y' organization: "); + Double yC = null; + for (;;) { + if (scan.hasNextDouble()) { + yC = scan.nextDouble(); + break; + } else { + System.out.print("Please input number in 'double' format: "); + scan.next(); + } + } + + System.out.print("Input annual turnover organization: "); + Float annualTurnover = null; + for (;;) { + if (scan.hasNextFloat()) { + annualTurnover = scan.nextFloat(); + break; + } else { + System.out.print("Please input number in 'float' format: "); + scan.next(); + } + } + + System.out.print("Choose type of organization (commercial, public, government, trust, private_limited_company): "); + String tp = scan.next(); + OrganizationType type = null; + for (;;) { + type = OrganizationType.findTypebyName(tp); + if (type == null) { + System.out.print("Please chose type of organization (commercial, public, government, trust, private_limited_company): "); + tp = scan.next(); + } + break; + } + + System.out.print("Input zipcode organization: "); + + String zipCode = null; + if (scan.hasNextLine()) + zipCode = scan.next(); + if (zipCode.equals("")) + zipCode = null; + + System.out.print("Input street organization: "); + String street = scan.next(); + + System.out.print("Input name of town organization: "); + String town = scan.next(); + + System.out.print("Input coordinate -x- town: "); + Integer xL = null; + for (;;) { + if (scan.hasNextInt()) { + xL = scan.nextInt(); + break; + } else { + System.out.print("Please input number in 'integer' format: "); + scan.next(); + } + } + + System.out.print("Input coordinate -y- town: "); + Long yL = null; + for (;;) { + if (scan.hasNextLong()) { + yL = scan.nextLong(); + break; + } else { + System.out.print("Please input number in 'long' format: "); + scan.next(); + } + } + + Organization t = new Organization(name, xC, yC, LocalDateTime.now(), annualTurnover, type, new Address(zipCode, street, new Location(xL, yL, town))); + org.addElement(t); + SortOrganizaton.sort(org); + }catch (Exception e){ + e.printStackTrace(); + System.out.println("Oops... Something going wrong. Probably you input incorrect value."); + } + } +} diff --git a/LB05/src/Commands/Clear.java b/LB05/src/Commands/Clear.java new file mode 100644 index 0000000..5697bfc --- /dev/null +++ b/LB05/src/Commands/Clear.java @@ -0,0 +1,28 @@ +package Commands; + +import Organization.Organization; + +import java.util.NoSuchElementException; +import java.util.Scanner; +import java.util.Vector; + +public class Clear extends Command{ + private static final String name = "clear"; + private final static String description = ": Clear collection;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + public static void clear(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + try { + org.clear(); + System.out.println("Now the collection is empty.\n"); + } catch (Exception e) { + System.out.println("Error cleaning.."); + } + + } +} diff --git a/LB05/src/Commands/Command.java b/LB05/src/Commands/Command.java new file mode 100644 index 0000000..b4e1912 --- /dev/null +++ b/LB05/src/Commands/Command.java @@ -0,0 +1,59 @@ +package Commands; + +import Organization.Organization; +import Server.Client; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.*; +import java.util.function.Consumer; + +@FunctionalInterface +interface CommandInt{ + void runCommand(); +} +public abstract class Command { + protected static Vector org = Client.getOrganizations(); + protected static Scanner scan = new Scanner(System.in); + protected static String[] arg = null; + protected static Iterable history = Client.getHistory(); + private static Map commandMap = new HashMap(); + static { + try { + commandMap.put(Help.getName(), Help.class.getMethod(Help.getName())); + commandMap.put(Info.getName(), Info.class.getMethod(Info.getName())); + commandMap.put(Show.getName(), Show.class.getMethod(Show.getName())); + commandMap.put(Add.getName(), Add.class.getMethod(Add.getName())); + commandMap.put(Update.getName(), Update.class.getMethod(Update.getName())); + commandMap.put(Remove.getName(), Remove.class.getMethod(Remove.getName())); + commandMap.put(Clear.getName(), Clear.class.getMethod(Clear.getName())); + commandMap.put(Save.getName(), Save.class.getMethod(Save.getName())); + commandMap.put(Execute_script.getName(), Execute_script.class.getMethod(Execute_script.getName())); + commandMap.put(Exit.getName(), Exit.class.getMethod(Exit.getName())); + commandMap.put(Reorder.getName(), Reorder.class.getMethod(Reorder.getName())); + commandMap.put(Sort.getName(), Sort.class.getMethod(Sort.getName())); + commandMap.put(History.getName(), History.class.getMethod(History.getName())); + commandMap.put(FilterByTypeAsc.getName(), FilterByTypeAsc.class.getMethod(FilterByTypeAsc.getName())); + commandMap.put(PrintByAddressAscending.getName(), PrintByAddressAscending.class.getMethod(PrintByAddressAscending.getName())); + commandMap.put(PrintByAddressDescending.getName(), PrintByAddressDescending.class.getMethod(PrintByAddressDescending.getName())); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } + } + public static boolean command(String command_arg) throws InvocationTargetException, IllegalAccessException { + String command = command_arg.split(" ").length > 1 ? command_arg.split(" ")[0] : command_arg; + String[] ar = command_arg.split(" ").length > 1 ? Arrays.copyOfRange(command_arg.split(" "), 1, command_arg.split(" ").length) : new String[0]; + arg = ar; + if (!commandMap.containsKey(command)) { + System.out.println("-" + command + "-" + ": this command doesn't exist."); + return false; + } + commandMap.get(command).invoke(null); + return true; + } + public static void showOtherList(Vector temp){ + org = temp; + Show.show(); + org = Client.getOrganizations(); + } +} \ No newline at end of file diff --git a/LB05/src/Commands/Execute_script.java b/LB05/src/Commands/Execute_script.java new file mode 100644 index 0000000..316ada8 --- /dev/null +++ b/LB05/src/Commands/Execute_script.java @@ -0,0 +1,47 @@ +package Commands; + +import Auxiliary.Write_XML; +import Organization.Organization; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.Scanner; +import java.util.Vector; + +public class Execute_script extends Command{ + private static final String name = "execute_script"; + private final static String description = ": Load commands from file;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void execute_script(){ + if (arg.length == 0) { + System.out.println("Please input filename in format: 'execute_script filename'\n"); + return; + } + String filename = arg[0]; + File file = new File(filename); + String str = ""; + String[] lines; + try (Scanner reader = new Scanner(new FileReader(file.getAbsolutePath()))) { + while (reader.hasNext()) { + str += reader.next() + "\n"; + } + lines = str.split("\n"); + for (String command_arg : lines) { + System.out.println("'" + command_arg + "'"); + Command.command(command_arg); + } + } catch (FileNotFoundException e){ + System.out.println("Sorry, I don't find this file: '" + filename + "'"); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } +} diff --git a/LB05/src/Commands/Exit.java b/LB05/src/Commands/Exit.java new file mode 100644 index 0000000..df2976b --- /dev/null +++ b/LB05/src/Commands/Exit.java @@ -0,0 +1,19 @@ +package Commands; + +public class Exit extends Command{ + private static final String name = "exit"; + private static final String description = ": exit;"; + + public static void exit(){ + + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + System.exit(30); + } + + public static String getDescription() {return description;} + + public static String getName() {return name;} +} diff --git a/LB05/src/Commands/FilterByTypeAsc.java b/LB05/src/Commands/FilterByTypeAsc.java new file mode 100644 index 0000000..3df2b06 --- /dev/null +++ b/LB05/src/Commands/FilterByTypeAsc.java @@ -0,0 +1,24 @@ +package Commands; + +import Auxiliary.FilterOrganizations; +import Organization.Organization; + +import java.util.Vector; + +public class FilterByTypeAsc extends Command{ + private static final String name = "filter_greater_than_type"; + private final static String description = ": show organizations with type > than given value;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void filter_greater_than_type() { + if (arg.length == 0) { + System.out.println("Please input type in format: filter_greater_than_type 'type'\n"); + return; + } + String type = arg[0]; + Vector result = FilterOrganizations.filterByType(org, type); + Command.showOtherList(result); + } +} diff --git a/LB05/src/Commands/Help.java b/LB05/src/Commands/Help.java new file mode 100644 index 0000000..b3fb8d0 --- /dev/null +++ b/LB05/src/Commands/Help.java @@ -0,0 +1,36 @@ +package Commands; + +public class Help extends Command{ + + private static final String name = "help"; + private static final String description = ": Output all commands;"; + + public static void help(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + System.out.println("-" + getName() + getDescription()); + System.out.println("-" + Info.getName() + Info.getDescription()); + System.out.println("-" + Show.getName() + Show.getDescription()); + System.out.println("-" + Add.getName() + Add.getDescription()); + System.out.println("-" + Update.getName() + Update.getDescription()); + System.out.println("-" + Remove.getName() + Remove.getDescription()); + System.out.println("-" + Clear.getName() + Clear.getDescription()); + System.out.println("-" + Save.getName() + Save.getDescription()); + System.out.println("-" + Execute_script.getName() + Execute_script.getDescription()); + System.out.println("-" + Exit.getName() + Exit.getDescription()); + System.out.println("-" + Reorder.getName() + Reorder.getDescription()); + System.out.println("-" + Sort.getName() + Sort.getDescription()); + System.out.println("-" + History.getName() + History.getDescription()); + System.out.println("-" + FilterByTypeAsc.getName() + FilterByTypeAsc.getDescription()); + System.out.println("-" + PrintByAddressAscending.getName() + PrintByAddressAscending.getDescription()); + System.out.println("-" + PrintByAddressDescending.getName() + PrintByAddressDescending.getDescription()); + } + + + public static String getName(){return name;} + public static String getDescription(){return description;} + + //public static void help(Object o) {} +} diff --git a/LB05/src/Commands/History.java b/LB05/src/Commands/History.java new file mode 100644 index 0000000..026cfa6 --- /dev/null +++ b/LB05/src/Commands/History.java @@ -0,0 +1,18 @@ +package Commands; + +public class History extends Command{ + private static final String name = "history"; + private final static String description = ": Show 8 last commands;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void history() { + + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + history.forEach(System.out::println); + } +} diff --git a/LB05/src/Commands/Info.java b/LB05/src/Commands/Info.java new file mode 100644 index 0000000..cdd4a8a --- /dev/null +++ b/LB05/src/Commands/Info.java @@ -0,0 +1,31 @@ +package Commands; + +import Organization.Organization; +import Server.Client; + +import java.util.Vector; + +public class Info extends Command{ + private static final String name = "info"; + private static final String description = ": About collection"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void info() { + /* + * Date of initialization + * Size + * ... + */ + + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + String dateInitialization = org.get(0).getLastUpdate(); + int size = org.size(); + System.out.println("Date of initialization collection: " + dateInitialization); + System.out.println("Size of collection: " + Integer.valueOf(size)); + } +} diff --git a/LB05/src/Commands/PrintByAddressAscending.java b/LB05/src/Commands/PrintByAddressAscending.java new file mode 100644 index 0000000..7d1851d --- /dev/null +++ b/LB05/src/Commands/PrintByAddressAscending.java @@ -0,0 +1,24 @@ +package Commands; + +import Auxiliary.FilterOrganizations; +import Auxiliary.SortOrganizaton; +import Organization.Organization; + +import java.util.Vector; + +public class PrintByAddressAscending extends Command{ + private static final String name = "print_field_ascending_postal_address"; + private final static String description = ": Print field ascending postal address;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void print_field_ascending_postal_address(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + Vector result = SortOrganizaton.sortByAddress(org); + Command.showOtherList(result); + } +} diff --git a/LB05/src/Commands/PrintByAddressDescending.java b/LB05/src/Commands/PrintByAddressDescending.java new file mode 100644 index 0000000..49bf0ee --- /dev/null +++ b/LB05/src/Commands/PrintByAddressDescending.java @@ -0,0 +1,23 @@ +package Commands; + +import Auxiliary.SortOrganizaton; +import Organization.Organization; + +import java.util.Vector; + +public class PrintByAddressDescending extends Command{ + private static final String name = "print_field_descending_postal_address"; + private final static String description = ": Print field descending postal address;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void print_field_descending_postal_address(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + Vector result = SortOrganizaton.revSortByAddress(org); + Command.showOtherList(result); + } +} diff --git a/LB05/src/Commands/Remove.java b/LB05/src/Commands/Remove.java new file mode 100644 index 0000000..73d2ba8 --- /dev/null +++ b/LB05/src/Commands/Remove.java @@ -0,0 +1,32 @@ +package Commands; + +import Organization.Organization; + +import java.util.NoSuchElementException; +import java.util.Scanner; +import java.util.Vector; + +import static java.lang.Integer.parseInt; + +public class Remove extends Command{ + private static final String name = "remove_by_id"; + private final static String description = ": Remove organization by id;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + public static void remove_by_id(){ + if (arg.length == 0) + { + System.out.println("Please input command in format: remove_by_id 'id'"); + return; + } + Integer id = arg.length > 0 ? parseInt(arg[0]) : -1; + try { + final Organization t = org.stream().filter(o -> o.getId() == id).findFirst().orElseThrow(); + org.remove(t); + System.out.println("Organization by id: '" + arg[0] + "' was successfully deleted"); + } catch (NoSuchElementException e) { + System.out.println("Sorry...\nDon't find organization by this id("); + } + } +} diff --git a/LB05/src/Commands/Reorder.java b/LB05/src/Commands/Reorder.java new file mode 100644 index 0000000..cbf61f4 --- /dev/null +++ b/LB05/src/Commands/Reorder.java @@ -0,0 +1,22 @@ +package Commands; + +import Organization.Organization; + +import java.util.Vector; + +import static Auxiliary.SortOrganizaton.revSortByName; + +public class Reorder extends Command{ + private static final String name = "reorder"; + private final static String description = ": Reverse sort collection;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void reorder(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + revSortByName(org);} +} diff --git a/LB05/src/Commands/Save.java b/LB05/src/Commands/Save.java new file mode 100644 index 0000000..64a7ae6 --- /dev/null +++ b/LB05/src/Commands/Save.java @@ -0,0 +1,30 @@ +package Commands; + +import Auxiliary.Write_XML; +import Organization.Organization; + +import java.io.FileNotFoundException; +import java.util.Arrays; +import java.util.Vector; + +public class Save extends Command{ + private static final String name = "save"; + private final static String description = ": Save collection in file;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + public static void save(){ + if (arg.length == 0) { + System.out.println("Please input filename in format: 'save filename'\n"); + return; + } + String filename = arg[0]; + try { + filename = filename.contains(".") ? filename.split(".")[0] : filename; + Write_XML.Write(org, filename + ".xml"); + System.out.println("File was successfully written"); + } catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/LB05/src/Commands/Show.java b/LB05/src/Commands/Show.java new file mode 100644 index 0000000..0895094 --- /dev/null +++ b/LB05/src/Commands/Show.java @@ -0,0 +1,26 @@ +package Commands; + +import Organization.Organization; + +import java.util.Vector; + +public class Show extends Command{ + private static final String name = "show"; + private final static String description = ": Show all organizations in collection;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + + public static void show(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + System.out.println(); + for (Organization t : org){ + System.out.println("Organization #" + (int)(org.indexOf(t) + 1) + ":"); + t.print(); + System.out.println("-----------------------"); + } + } +} diff --git a/LB05/src/Commands/Sort.java b/LB05/src/Commands/Sort.java new file mode 100644 index 0000000..73deb63 --- /dev/null +++ b/LB05/src/Commands/Sort.java @@ -0,0 +1,20 @@ +package Commands; + +import Auxiliary.SortOrganizaton; +import Organization.Organization; + +import java.util.Vector; + +public class Sort extends Command{ + private static final String name = "sort"; + private final static String description = ": Sort collection;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + public static void sort(){ + if (arg.length > 0){ + System.out.println("I don't understand u\n What does it mean: " + getName() + " " + arg[0]); + return; + } + SortOrganizaton.sort(org);} +} diff --git a/LB05/src/Commands/Update.java b/LB05/src/Commands/Update.java new file mode 100644 index 0000000..d201e58 --- /dev/null +++ b/LB05/src/Commands/Update.java @@ -0,0 +1,50 @@ +package Commands; + +import Organization.Organization; + +import java.time.LocalDateTime; +import java.util.*; + +import static java.lang.Integer.parseInt; + +public class Update extends Command{ + private static final String name = "update"; + private final static String description = ": Update value of field for organization from collection;"; + + public static String getName(){return name;} + public static String getDescription(){return description;} + public static void update(){ + Integer id = arg.length > 0 ? parseInt(arg[0]) : -1; + if (arg.length == 0) + { + System.out.println("Please input command in format: update 'id'"); + return; + } + try { + final Organization t = org.stream().filter(o -> o.getId() == id).findFirst().orElseThrow(); + System.out.println("What do u wanna change: "); + System.out.println("0 - nothing\n1 - name\n2 - coordinates\n3 - creation date\n4 - annual turnover\n5 - type\n6 - postal address"); + + switch (scan.nextLine()) { + case "0": case "nothing": case "-": break; + case "1": case "name": case "Name": {System.out.print("Input name: "); t.setName(scan.next());} + case "2": case "coordinates": case "Coordinates": case "coordinate": case "Coordinate": {System.out.print("Input coordinates 'x , y ': "); + t.setCoordinates(scan.next()); break;} + case "3": case "date": case "Date": case "creation date": case "creationDate": case "creation_date": {System.out.print("Input date 'yyyy-mm-dd': "); + t.setCreationDate(LocalDateTime.parse(scan.next())); break;} + case "4": case "annual turnover": case "annualTurnover": case "annualTurnOver": case "annual_turnover": case "annual_Turnover": case "annual_TurnOver": {System.out.print("Input annual turnover: "); + t.setAnnualTurnover(scan.next()); break;} + case "5": case "type": case "Type": {System.out.print("Input type: "); + t.setType(scan.next()); break;} + case "6": case "postalAddress": case "postal address": case "postal_address": case "postal addres": case "postal_addres": case "postal adress": case "postal_adress": case "postal adres": case "postal_adres": {System.out.print("Input postal address ' zipcode, street, x, y, town': "); + t.setPostalAddress(scan.next()); break;} + + + default: throw new IllegalStateException("Unexpected value: " + scan.next()); + } + } catch (NoSuchElementException e) { + System.out.println("Sorry...\nDon't find organization by this id("); + } + + } +} diff --git a/LB05/src/LB05.iml b/LB05/src/LB05.iml new file mode 100644 index 0000000..b107a2d --- /dev/null +++ b/LB05/src/LB05.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/LB05/src/META-INF/MANIFEST.MF b/LB05/src/META-INF/MANIFEST.MF new file mode 100644 index 0000000..37197ef --- /dev/null +++ b/LB05/src/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main + diff --git a/LB05/src/Main.class b/LB05/src/Main.class new file mode 100644 index 0000000..545d3cc Binary files /dev/null and b/LB05/src/Main.class differ diff --git a/LB05/src/Main.java b/LB05/src/Main.java new file mode 100644 index 0000000..17a22bb --- /dev/null +++ b/LB05/src/Main.java @@ -0,0 +1,29 @@ +/* FILE NAME : Main.java + * PROGRAMMER : DS6 + * @author : Sokolov Dmitry + * LAST UPDATE : 14.03.2023 + * PURPOSE : Main file + */ + +import java.io.*; +import java.util.*; + +import Auxiliary.Read_XML; +import Auxiliary.Write_XML; +import Organization.*; +import Server.Client; + +public class Main { + public static void main(String[] args) throws FileNotFoundException { + + String filename = "src/Organizations.xml"; + if (args.length != 0) + filename = args[0]; + String fileOut = "OutOrganizations.xml"; + Vector Organizations = Read_XML.CreateVector(filename); + Client client = new Client(Organizations); + + client.run(client); + Write_XML.Write(Organizations, fileOut); + } +} diff --git a/LB05/src/Organization/Address.java b/LB05/src/Organization/Address.java new file mode 100644 index 0000000..13f45eb --- /dev/null +++ b/LB05/src/Organization/Address.java @@ -0,0 +1,74 @@ +/* FILE NAME : Address.java + * PROGRAMMER : DS6 + * @author : Sokolov Dmitry + * LAST UPDATE : 13.03.2023 + * PURPOSE : Address Organization + */ + +package Organization; + +import java.util.Arrays; +import java.util.regex.Pattern; + +public class Address +{ + private String street; //Строка не может быть пустой, Поле может быть null + private String zipCode; //Поле не может быть null + private Location town; //Поле может быть null + + public Address(String zipCode, String street, Location town){ + this.zipCode = zipCode; + this.street = street; + this.town = town; + } + + public Address(String postalAddress, Address old) { + if (postalAddress.contains(", ")) + { + try { + String[] split = Arrays.stream(postalAddress.split(Pattern.quote("[,\s]\s*"))).map(String::trim).toArray(String[]::new); + if (split[0].equals("-")) + this.zipCode = old.getZipCode(); + if (split[1].equals("-")) + this.street = old.getStreet(); + if (split[2].equals("-")) { + this.town = old.getTown(); + } + this.street = split[1]; + this.town = new Location(postalAddress); + } catch (IndexOutOfBoundsException e){ + System.out.println("Incorrect data"); + } + } + } + + private Location getTown() { + return this.town; + } + + private String getStreet() { + return this.street; + } + + private String getZipCode() { + return this.zipCode; + } + + public void print(){ + System.out.println(" - ZipCode = " + this.zipCode); + System.out.println(" - Street = " + this.street); + town.print(); + } + public String getAddressinXML(){ + String start = "\t\t\n"; + String zipCode = "\t\t\t" + this.zipCode + "\n"; + String street = "\t\t\t" + this.street + "\n"; + String location = town.getLocationinXML(); + String end = "\t\t\n"; + return start + zipCode + street + location + end; + } + + public String getAddress() { + return getZipCode() + ", " + getStreet() + ", " + this.town.getTown(); + } +} \ No newline at end of file diff --git a/LB05/src/Organization/Coordinates.java b/LB05/src/Organization/Coordinates.java new file mode 100644 index 0000000..b325e5e --- /dev/null +++ b/LB05/src/Organization/Coordinates.java @@ -0,0 +1,50 @@ +/* FILE NAME : Coordinates.java + * PROGRAMMER : DS6 + * @author : Sokolov Dmitry + * LAST UPDATE : 10.03.2023 + * PURPOSE : Coordinates Organization + */ + +package Organization; + +public class Coordinates { + private long x; //Максимальное значение поля: 890 + private double y; + + public Coordinates(long x, double y){ + if (x >= 890) + this.x = 890; + this.x = x; + this.y = y; + } + public Coordinates(String coord){ + if (coord.contains(",")){ + this.x = Long.parseLong(coord.split(", ")[0]); + this.y = Double.parseDouble(coord.split(", ")[1]); + } + if (coord.contains(" ")){ + this.x = Long.parseLong(coord.split(" ")[0]); + this.y = Double.parseDouble(coord.split(" ")[1]); + } + if (coord.contains(".")){ + this.x = Long.parseLong(coord.split(". ")[0]); + this.y = Double.parseDouble(coord.split(". ")[1]); + } + if (coord.contains(",")){ + this.x = Long.parseLong(coord.split("\n")[0]); + this.y = Double.parseDouble(coord.split("\n")[1]); + } + } + public void print(){ + System.out.println(" - Coordinates 'x' = " + this.x); + System.out.println(" - Coordinates 'y' = " + this.y); + } + + public String getCoordinatesinXML(){ + String start = "\t\t\n"; + String x = "\t\t\t" + String.valueOf(this.x) + "\n"; + String y = "\t\t\t" + String.valueOf(this.y) + "\n"; + String end = "\t\t\n"; + return start + x + y + end; + } +} \ No newline at end of file diff --git a/LB05/src/Organization/Location.java b/LB05/src/Organization/Location.java new file mode 100644 index 0000000..6a95778 --- /dev/null +++ b/LB05/src/Organization/Location.java @@ -0,0 +1,59 @@ +/* FILE NAME : Location.java + * PROGRAMMER : DS6 + * @author : Sokolov Dmitry + * LAST UPDATE : 13.03.2023 + * PURPOSE : Location Organization + */ + +package Organization; + +public class Location { + private int x; + private long y; + private String town; //Поле не может быть null + public Location(int x, long y, String town){ + this.x = x; + this.y = y; + this.town = town; + } + + public Location(String postalAddress) { + if (postalAddress.contains(", ")) + { + try { + if (!postalAddress.split(", ")[2].equals("-")) + this.x = Integer.parseInt(postalAddress.split(", ")[2]); + if (!postalAddress.split(", ")[2].equals("- ")) + this.x = Integer.parseInt(postalAddress.split(", ")[2]); + if (!postalAddress.split(", ")[3].equals("-")) + this.y = Long.parseLong(postalAddress.split(", ")[3]); + if (!postalAddress.split(", ")[3].equals("- ")) + this.y = Long.parseLong(postalAddress.split(", ")[3]); + if (!postalAddress.split(", ")[4].equals("-")) + this.town = postalAddress.split(", ")[4]; + if (!postalAddress.split(", ")[4].equals("- ")) + this.town = postalAddress.split(", ")[4]; + } catch (IndexOutOfBoundsException e){ + System.out.println("Incorrect data"); + } + } + } + + public void print(){ + System.out.println(" - Location 'x' = " + this.x); + System.out.println(" - Location 'y' = " + this.y); + System.out.println(" - Town = " + this.town); + } + public String getLocationinXML(){ + String start = "\t\t\t\n"; + String x = "\t\t\t\t" + String.valueOf(this.x) + "\n"; + String y = "\t\t\t\t" + String.valueOf(this.y) + "\n"; + String town = "\t\t\t\t" + String.valueOf(this.town) + "\n"; + String end = "\t\t\t\n"; + return start + x + y + town + end; + } + + public String getTown() { + return town; + } +} \ No newline at end of file diff --git a/LB05/src/Organization/Organization.java b/LB05/src/Organization/Organization.java new file mode 100644 index 0000000..5acda15 --- /dev/null +++ b/LB05/src/Organization/Organization.java @@ -0,0 +1,107 @@ +/* FILE NAME : Organization.java + * PROGRAMMER : DS6 + * @author : Sokolov Dmitry + * LAST UPDATE : 13.03.2023 + * PURPOSE : Info Organization + */ + +package Organization; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +public class Organization +{ + private int id = 30; //Значение поля должно быть больше 0, Значение этого поля должно быть уникальным, Значение этого поля должно генерироваться автоматически + private String name; //Поле не может быть null, Строка не может быть пустой + private Coordinates coordinates; //Поле не может быть null + private LocalDateTime creationDate; //Поле не может быть null, Значение этого поля должно генерироваться автоматически + private Float annualTurnover; //Поле может быть null, Значение поля должно быть больше 0 + private OrganizationType type; //Поле может быть null + private Address postalAddress; //Поле может быть null + private String lastUpdate; + + public Organization(String name, long x, double y, LocalDateTime creationDate, Float annualTurnover, OrganizationType type, Address ad){ + this.id = (int) (Math.random() * 999999999 + 1); + this.name = name; + this.coordinates = new Coordinates(x, y); + setCreationDate(creationDate); + this.annualTurnover = annualTurnover; + this.type = type; + this.postalAddress = ad; + this.lastUpdate = String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); + } + + public Organization(String name, long x, double y, Float annualTurnover, OrganizationType type, Address ad){ + this.id = (int) (Math.random() * 999999999 + 1); + this.name = name; + this.coordinates = new Coordinates(x, y); + setCreationDate(LocalDateTime.now()); + this.annualTurnover = annualTurnover; + this.type = type; + this.postalAddress = ad; + this.lastUpdate = String.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); + } + + public void print() { + System.out.println(" - id = " + this.id); + System.out.println(" - name = " + this.name); + coordinates.print(); + System.out.println(" - Date = " + this.creationDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); + System.out.println(" - Annual Turnover = " + this.annualTurnover); + System.out.println(" - Type = " + this.type); + postalAddress.print(); + } + public String getName(){return this.name;} + + public String getXML(){ + String start = "\t\n"; + String id = getIdinXML(); + String name = getNameinXMl(); + String coordinates = getCoordinatesinXML(); + String creationDate = getCreatonDateinXML(); + String annualTurnover = getAnnualTurnoverinXML(); + String type = getTypeinXML(); + String address = getAddressinXML(); + String lastUpdate = "\t\t" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "\n"; + String end = "\t\n"; + return start + id + name + coordinates + creationDate + annualTurnover + type + address + lastUpdate + end; + } + private String getIdinXML(){return "\t\t" + String.valueOf(this.id) + "\n";} + private String getNameinXMl(){return "\t\t" + this.name + "\n";} + private String getCoordinatesinXML(){return coordinates.getCoordinatesinXML();} + public String getCreatonDateinXML(){return "\t\t" + String.valueOf(this.creationDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))) + "\n";} + public String getAnnualTurnoverinXML(){return "\t\t" + String.valueOf(this.annualTurnover) + "\n";} + public String getTypeinXML(){return "\t\t" + String.valueOf(this.type) + "\n";} + public String getAddressinXML(){return postalAddress.getAddressinXML();} + public void setCreationDate(LocalDateTime creationDate){ + this.creationDate = creationDate; + } + public void setId(int id){this.id = id;} + public void setLastUpdate(String s){this.lastUpdate = s;} + public String getLastUpdate(){return this.lastUpdate;} + public void setName(String name) {this.name = name;} + public void setType(String type) {this.type = OrganizationType.valueOf(type);} + + public void setCoordinates(String coordinates) { + this.coordinates = new Coordinates(coordinates); + } + + public void setAnnualTurnover(String annualTurnover) { + this.annualTurnover = Float.valueOf(annualTurnover); + } + + public String getAddress(){ + return this.postalAddress.getAddress(); + } + public void setPostalAddress(String postalAddress) { + this.postalAddress = new Address(postalAddress, this.postalAddress); + } + public int getId(){return id;} + public OrganizationType getType() { + return this.type; + } + public boolean equals(String type){ + return (this.type.toString().compareTo(type) > 0); + } +} \ No newline at end of file diff --git a/LB05/src/Organization/OrganizationType.java b/LB05/src/Organization/OrganizationType.java new file mode 100644 index 0000000..ae2f94e --- /dev/null +++ b/LB05/src/Organization/OrganizationType.java @@ -0,0 +1,36 @@ +/* FILE NAME : OrganizationType.java + * PROGRAMMER : DS6 + * @author : Sokolov Dmitry + * LAST UPDATE : 13.03.2023 + * PURPOSE : Type of Organization + */ + +package Organization; + +public enum OrganizationType { + COMMERCIAL("commercial"), + PUBLIC("public"), + GOVERNMENT("government"), + TRUST("trust"), + PRIVATE_LIMITED_COMPANY("private_limited_company"); + + private final String name; + + OrganizationType(String name){ + this.name = name; + } + public static OrganizationType findTypebyName(String s) { + switch (s) { + case ("commercial"), ("Commercial"), ("COMMERCIAL") -> {return COMMERCIAL;} + case ("public"), ("Public"), ("PUBLIC") -> {return PUBLIC;} + case ("government"), ("Government"), ("GOVERNMENT") -> {return GOVERNMENT;} + case ("trust"), ("Trust"), ("TRUST") -> {return TRUST;} + case ("private_limited_company"), ("Private limited company"), ("PRIVATE_LIMITED_COMPANY"), ("PrivateLimitedCompany"), ("Private_Limited_Company") -> {return PRIVATE_LIMITED_COMPANY;} + default -> {return null;} + } + } + public static String findNamebyType(OrganizationType t) { + return t.name; + } + +} diff --git a/LB05/src/Organizations.xml b/LB05/src/Organizations.xml new file mode 100644 index 0000000..11b7709 --- /dev/null +++ b/LB05/src/Organizations.xml @@ -0,0 +1,64 @@ + + + + VkusnoiTochka + + 542 + 42 + + 2022-03-20 + 239.30 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + 2023-03-14 + + + 1 + VkusnoiTochka + + 542 + 42 + + 2022-03-20 + 239.30 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + 2023-03-14 + + + VkusnoiTochka + + 542 + 42 + + 2022-03-20 + 239.30 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + 2023-03-14 + + \ No newline at end of file diff --git a/LB05/src/OutOrganizations.xml b/LB05/src/OutOrganizations.xml new file mode 100644 index 0000000..2314485 --- /dev/null +++ b/LB05/src/OutOrganizations.xml @@ -0,0 +1,83 @@ + + + + 305706 + VkusnoiTochka + + 542 + 42.0 + + 2023-03-14 + 239.3 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + 1 + VkusnoiTochka + + 542 + 42.0 + + 2023-03-14 + 239.3 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + 1 + VkusnoiTochka + + 542 + 42.0 + + 2023-03-14 + 239.3 + GOVERNMENT + + 239239 + CGSG + + 542 + 42 + name + + + + + 645641861 + KFC + + 900 + 239.3 + + 2023-03-14 + 100000.0 + COMMERCIAL + + 23 + ulica + + 3 + 20 + Spb + + + + \ No newline at end of file diff --git a/LB05/src/Server/Client.java b/LB05/src/Server/Client.java new file mode 100644 index 0000000..2062b39 --- /dev/null +++ b/LB05/src/Server/Client.java @@ -0,0 +1,94 @@ +package Server; + +import Commands.*; +import Organization.Organization; + +import java.lang.reflect.InvocationTargetException; +import java.util.*; + +public class Client { + private Vector org; + private static Client client; + private final Deque history = new ArrayDeque<>(8); + public Client(Vector org){this.org = org;} + public void run(Client client) { + this.client = client; + Scanner scanner = new Scanner(System.in); + //Show.show(); + try{ + Help.help(); + }catch (Exception e){ + System.out.println("au"); + } + //System.out.print("Input command: "); + loop(scanner); + } + + private void memorize(final String command) { + history.offer(command); + + if (history.size() > 8) { + history.pop(); + } + } + + /** + * Loop of client commands. + */ + public void loop(Scanner scan) { + while (true) { + System.out.print("Input command: "); + + if (!scan.hasNext()) { + break; + } + + String command_arg = scan.nextLine(); +/* + switch (command) { + case "help" -> Help.help(); + case "info" -> Info.info(this.org); + case "show" -> Show.show(this.org); + case "add" -> Add.add(scan, this.org); + case "update" -> Update.update(scan, this.org); + case "remove" -> Remove.remove(scan, this.org); + case "clear" -> { + Clear.clear(org); + System.out.println("Now the collection is empty.\n"); + } + case "save" -> { + System.out.print("Input filename: "); + String outfile = scan.nextLine(); + Save.save(outfile, org); + } + case "execute_script" -> Execute_script.execute_script(arg[0], org); + case "exit" -> Exit.exit(); + case "reorder" -> Reorder.reorder(org); + case "sort" -> Sort.sort(org); + case "history" -> History.history(history); + case "filter_greater_than_type" -> {System.out.print("Input type: ");FilterByTypeAsc.filter_greater_than_type(org, scan.nextLine());} + case "print_field_ascending_postal_address" -> PrintByAddressAscending.printByAddressAscending(org); + case "print_field_descending_postal_address" -> PrintByAddressDescending.printByAddressDescending(org); + default -> { + System.out.println(command + ": this command doesn't exist."); + continue; + } + }*/ + try { + if (Command.command(command_arg)) + memorize(command_arg); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + scan.close(); + } + + private final Vector cyganskie_fokusy() {return this.client.org;} + public static Vector getOrganizations(){return client.cyganskie_fokusy();} + private Deque fokus(){return this.client.history;} + public static Deque getHistory(){return client.fokus();} +} diff --git a/LB05/src/Test_Write_XML.java b/LB05/src/Test_Write_XML.java new file mode 100644 index 0000000..98397d5 --- /dev/null +++ b/LB05/src/Test_Write_XML.java @@ -0,0 +1,112 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.StringWriter; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class Test_Write_XML { + + /** + * @param args + * @throws TransformerException + * @throws ParserConfigurationException + * @throws IOException + */ + public static void main(String[] args) throws TransformerException, ParserConfigurationException, IOException { + // TODO Auto-generated method stub + BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); +// System.out.print("Enter number to add elements in your XML file: "); +// String str = bf.readLine(); + int no=2; +// System.out.print("Enter root: "); + String root = "SMS"; + DocumentBuilderFactory documentBuilderFactory =DocumentBuilderFactory.newInstance(); + + DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder(); + + Document document = documentBuilder.newDocument(); + + Element rootElement = document.createElement(root); + + document.appendChild(rootElement); +// for (int i = 1; i <= no; i++) +// System.out.print("Enter the element: "); +// String element = bf.readLine(); + String element ="Number"; + System.out.print("Enter the Number: "); + String data = bf.readLine(); + Element em = document.createElement(element); + em.appendChild(document.createTextNode(data)); + rootElement.appendChild(em); + + String element1 ="message"; + System.out.print("Enter the SMS: "); + String data1 = bf.readLine(); + Element em1 = document.createElement(element1); + em1.appendChild(document.createTextNode(data1)); + rootElement.appendChild(em1); + + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + DOMSource source = new DOMSource(document); + + StreamResult result = new StreamResult(new StringWriter()); + + //t.setParameter(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5"); + transformer.transform(source, result); + + //writing to file + FileOutputStream fop = null; + File file; + try { + + file = new File("Test_Write_XML.xml"); + fop = new FileOutputStream(file); + + // if file doesnt exists, then create it + if (!file.exists()) { + file.createNewFile(); + } + + // get the content in bytes + String xmlString = result.getWriter().toString(); + System.out.println(xmlString); + byte[] contentInBytes = xmlString.getBytes(); + + fop.write(contentInBytes); + fop.flush(); + fop.close(); + + System.out.println("Done"); + + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fop != null) { + fop.close(); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + +} \ No newline at end of file diff --git a/LB05/src/Test_Write_XML.xml b/LB05/src/Test_Write_XML.xml new file mode 100644 index 0000000..bdf0d77 --- /dev/null +++ b/LB05/src/Test_Write_XML.xml @@ -0,0 +1,5 @@ + + + 2 + 23e + diff --git a/LB05/src/ex b/LB05/src/ex new file mode 100644 index 0000000..c63e762 --- /dev/null +++ b/LB05/src/ex @@ -0,0 +1,3 @@ +show +clear +save