Commit 8175fcc5 by Paktalin

Initial commit

parents
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="true" project-jdk-name="10" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Task_2.iml" filepath="$PROJECT_DIR$/Task_2.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit5.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.0.0/junit-jupiter-api-5.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.0.0/opentest4j-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.0.0/junit-platform-commons-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
\ No newline at end of file
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
class AnyOrderSorter implements Sortable {
private static HashMap<Integer, Integer> occurrences = new HashMap<>();
public List<Integer> sort(List<Integer> list) {
for (Integer value : list)
countOccurrences(value);
removeRepeating(list);
occurrences.clear();
return list;
}
private static void countOccurrences(Integer value) {
Integer count = occurrences.get(value);
if (count == null)
occurrences.put(value, 1);
else
occurrences.put(value, count + 1);
}
private static void removeRepeating(List list) {
Iterator<Integer> i = list.iterator();
while (i.hasNext()) {
Integer value = i.next();
if (occurrences.get(value) >= 3)
i.remove();
}
}
}
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AnyOrderSorterTest extends SortableTest{
@Override
void initializeSorter() {
sorter = new AnyOrderSorter();
}
@Override
void verifyValuesInList(List list) {
assertEquals(7, list.size());
assertEquals(4, (int) list.get(0));
assertEquals(6, (int) list.get(1));
assertEquals(8, (int) list.get(2));
assertEquals(235, (int) list.get(3));
assertEquals(5, (int) list.get(4));
assertEquals(45, (int) list.get(5));
assertEquals(4, (int) list.get(6));
}
@Override
void verifyValuesInList2(List<Integer> list) {
assertEquals(6, list.size());
assertEquals(6, (int) list.get(0));
assertEquals(7, (int) list.get(1));
assertEquals(8, (int) list.get(2));
assertEquals(9, (int) list.get(3));
assertEquals(12, (int) list.get(4));
assertEquals(1, (int) list.get(5));
}
}
\ No newline at end of file
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
private static List<Integer> list = new ArrayList<>();
private static Sortable sorter;
public static void main(String[] args) {
printWelcome();
readList();
printModeInstructions();
readMode();
printInitialList();
sortList();
printEditedList();
}
private static void readList() {
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
if (input.equals("d") || input.equals("d2"))
setDefaultList(input);
else
toList(input);
}
private static void readMode() {
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
if(input.equals("a"))
sorter = new AnyOrderSorter();
else if(input.equals("r"))
sorter = new SorterInARow();
else {
System.out.println("Неверный формат ввода! Попробуйте снова\n");
readMode();
}
}
private static void sortList() {
list = sorter.sort(list);
}
private static void setDefaultList(String listCode) {
if (listCode.equals("d"))
list = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 4, 6, 8, 235, 5, 45, 3, 1, 2, 2, 2, 3, 4));
else list = new ArrayList<>(Arrays.asList(4, 4, 5, 4, 4, 4, 4, 6, 7, 8, 9, 12, 3, 3, 3, 3, 3, 3, 5, 5, 1));
}
private static void toList(String input) {
for (String item : input.split(",")) {
try {
list.add(Integer.valueOf(item));
} catch (NumberFormatException e) {
System.out.println("Неверный формат ввода! Попробуйте снова.");
readList();
return;
}
}
}
private static void printWelcome() {
System.out.println("Привествую!\n" +
"Введите \"d\" для использования стандартного списка №1, \"d2\" для стандартного списка №2\n" +
"или введите свой, разделяя числа запятыми (Например: \"1,45,5,6\")");
}
private static void printModeInstructions() {
System.out.println("Введите \"r\", если хотите удалить повторяющиеся 3 и более раз числа подряд\n" +
"или \"a\" для того, чтобы удалить повторяющиеся 3 и более раз числа не обязательно подряд.");
}
private static void printInitialList() {
System.out.println("\nИсходный список: \n" + list.toString() + "\n");
}
private static void printEditedList() {
System.out.println("Измененный список: \n" + list.toString() + "\n");
}
}
import java.util.List;
interface Sortable {
List<Integer> sort(List<Integer> list);
}
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
abstract class SortableTest {
private List<Integer> arrayList, arrayList2, arrayList3;
Sortable sorter;
@BeforeEach
void executedBeforeEach() {
List data = Arrays.asList(1, 1, 2, 3, 4, 6, 8, 235, 5, 45, 3, 1, 2, 2, 2, 3, 4);
List data2 = Arrays.asList(4, 4, 5, 4, 4, 4, 4, 6, 7, 8, 9, 12, 3, 3, 3, 3, 3, 3, 5, 5, 1);
List data3 = Arrays.asList(1, 4, 5, 6, 8, 8, 8, 88, 8, 8, 8, 8);
arrayList = new ArrayList<>(data);
arrayList2 = new ArrayList<>(data2);
arrayList3 = new ArrayList<>(data3);
initializeSorter();
}
@Test
void sort() {
arrayList = sorter.sort(arrayList);
verifyValuesInList(arrayList);
arrayList2 = sorter.sort(arrayList2);
verifyValuesInList2(arrayList2);
arrayList3 = sorter.sort(arrayList3);
verifyValuesInList3(arrayList3);
}
abstract void initializeSorter();
abstract void verifyValuesInList(List<Integer> list);
abstract void verifyValuesInList2(List<Integer> list);
private void verifyValuesInList3(List<Integer> list) {
assertEquals(5, (list.size()));
assertEquals(1, (int) list.get(0));
assertEquals(4, (int) list.get(1));
assertEquals(5, (int) list.get(2));
assertEquals(6, (int) list.get(3));
assertEquals(88, (int) list.get(4));
}
}
\ No newline at end of file
import java.util.List;
class SorterInARow implements Sortable {
private List<Integer> list;
public List<Integer> sort(List<Integer> list) {
this.list = list;
int i = 0;
int repeatingStartedIndex = -1;
int repeatingCounter = 0;
Integer focusValue = null;
while (i < list.size()) {
if (repeatingStartedIndex < 0 || !focusValue.equals(list.get(i))) {
if (repeatingCounter >= 3) {
removeNumbers(repeatingStartedIndex, i);
i = i - repeatingCounter;
}
repeatingStartedIndex = i;
repeatingCounter = 1;
focusValue = list.get(i);
}
else repeatingCounter++;
i++;
}
if (repeatingCounter >= 3)
removeNumbers(repeatingStartedIndex, i);
return list;
}
private void removeNumbers(int from, int to) {
for(int j = to - 1; j >= from; j--)
list.remove(j);
}
}
\ No newline at end of file
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class SorterInARowTest extends SortableTest{
@Override
void initializeSorter() {
sorter = new SorterInARow();
}
@Override
void verifyValuesInList(List list) {
assertEquals(14, list.size());
assertEquals(1, (int) list.get(0));
assertEquals(1, (int) list.get(1));
assertEquals(2, (int) list.get(2));
assertEquals(3, (int) list.get(3));
assertEquals(4, (int) list.get(4));
assertEquals(6, (int) list.get(5));
assertEquals(8, (int) list.get(6));
assertEquals(235, (int) list.get(7));
assertEquals(5, (int) list.get(8));
assertEquals(45, (int) list.get(9));
assertEquals(3, (int) list.get(10));
assertEquals(1, (int) list.get(11));
assertEquals(3, (int) list.get(12));
assertEquals(4, (int) list.get(13));
}
@Override
void verifyValuesInList2(List<Integer> list) {
assertEquals(11, list.size());
assertEquals(4, (int) list.get(0));
assertEquals(4, (int) list.get(1));
assertEquals(5, (int) list.get(2));
assertEquals(6, (int) list.get(3));
assertEquals(7, (int) list.get(4));
assertEquals(8, (int) list.get(5));
assertEquals(9, (int) list.get(6));
assertEquals(12, (int) list.get(7));
assertEquals(5, (int) list.get(8));
assertEquals(5, (int) list.get(9));
assertEquals(1, (int) list.get(10));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment