Last active
February 25, 2019 15:01
-
-
Save uvlad7/8235a95205dde3d8396114ce60166af2 to your computer and use it in GitHub Desktop.
УП 2.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
View view = new View(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Objects; | |
public class Student implements Comparable<Student> { | |
private int course; | |
private int group; | |
private String surname; | |
private String name; | |
public Student(int course, int group, String surname, String name) { | |
this.course = course; | |
this.group = group; | |
this.surname = surname; | |
this.name = name; | |
} | |
public int getCourse() { | |
return course; | |
} | |
public void setCourse(int course) { | |
this.course = course; | |
} | |
public int getGroup() { | |
return group; | |
} | |
public void setGroup(int group) { | |
this.group = group; | |
} | |
public String getSurname() { | |
return surname; | |
} | |
public void setSurname(String surname) { | |
this.surname = surname; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
return surname + " " + name; | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Student student = (Student) o; | |
return course == student.course && | |
group == student.group && | |
surname.equals(student.surname) && | |
name.equals(student.name); | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(course, group, surname, name); | |
} | |
@Override | |
public int compareTo(Student o) { | |
int diff = surname.compareTo(o.surname); | |
if (diff == 0) | |
return name.compareTo(o.name); | |
return diff; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Арташевич|Станислав|1|3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
import javax.swing.tree.DefaultMutableTreeNode; | |
import javax.swing.tree.DefaultTreeCellRenderer; | |
import javax.swing.tree.DefaultTreeModel; | |
import javax.swing.tree.TreeNode; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.List; | |
import java.util.*; | |
public class View extends JFrame implements ActionListener { | |
JButton edit; | |
JButton add; | |
JButton delete; | |
private Map<Integer, Map<Integer, List<Student>>> database; | |
private JTree tree; | |
private DefaultTreeModel model; | |
public View() { | |
super("University"); | |
try { | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
database = new HashMap<>(4, 1); | |
try (Scanner scanner = new Scanner(new File("src\\students.txt"))) { | |
scanner.useDelimiter("[|]|[\r\n]+"); | |
int course, group; | |
String surname, name; | |
List<Student> groupList; | |
Map<Integer, List<Student>> groupsMap; | |
while (scanner.hasNext()) { | |
surname = scanner.next(); | |
name = scanner.next(); | |
course = scanner.nextInt(); | |
group = scanner.nextInt(); | |
if (database.containsKey(course)) { | |
if (database.get(course).containsKey(group)) { | |
groupList = database.get(course).get(group); | |
groupList.add(new Student(course, group, surname, name)); | |
} else { | |
groupsMap = database.get(course); | |
groupList = new ArrayList<>(); | |
groupList.add(new Student(course, group, surname, name)); | |
groupsMap.put(group, groupList); | |
} | |
} else { | |
groupsMap = new HashMap<>(12, 1); | |
groupList = new ArrayList<>(); | |
groupList.add(new Student(course, group, surname, name)); | |
groupsMap.put(group, groupList); | |
database.put(course, groupsMap); | |
} | |
} | |
} | |
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Студенты"); | |
DefaultMutableTreeNode courseNode, groupNode, studentNode; | |
for (int course : database.keySet()) { | |
courseNode = new DefaultMutableTreeNode(course + " курс"); | |
for (int group : database.get(course).keySet()) { | |
groupNode = new DefaultMutableTreeNode(group + " группа"); | |
Collections.sort(database.get(course).get(group)); | |
for (Student student : database.get(course).get(group)) { | |
studentNode = new DefaultMutableTreeNode(student); | |
groupNode.add(studentNode); | |
} | |
courseNode.add(groupNode); | |
} | |
root.add(courseNode); | |
} | |
model = new DefaultTreeModel(root); | |
tree = new JTree(model); | |
tree.setRowHeight(48); | |
tree.setCellRenderer(new CellRenderer()); | |
setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS)); | |
add(new JScrollPane(tree)); | |
JPanel panel = new JPanel(); | |
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS)); | |
add = new JButton(); | |
add.setIcon(new ImageIcon("src\\add.png")); | |
panel.add(Box.createVerticalGlue()); | |
add.addActionListener(this); | |
panel.add(add); | |
edit = new JButton(); | |
edit.setIcon(new ImageIcon("src\\edit.png")); | |
panel.add(Box.createVerticalGlue()); | |
edit.addActionListener(this); | |
panel.add(edit); | |
delete = new JButton(); | |
delete.setIcon(new ImageIcon("src\\delete.png")); | |
panel.add(Box.createVerticalGlue()); | |
delete.addActionListener(this); | |
panel.add(delete); | |
panel.setAlignmentX(Component.LEFT_ALIGNMENT); | |
add(Box.createHorizontalGlue()); | |
add(panel); | |
pack(); | |
setSize(900, 600); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
} catch (FileNotFoundException | ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
int course, group; | |
String surname, name; | |
JTextField surnameField = new JTextField(15); | |
JTextField nameField = new JTextField(15); | |
JTextField courseField = new JTextField(1); | |
JTextField groupField = new JTextField(2); | |
JPanel inputPanel = new JPanel(); | |
inputPanel.add(new JLabel("Surname")); | |
inputPanel.add(surnameField); | |
inputPanel.add(Box.createHorizontalStrut(5)); | |
inputPanel.add(new JLabel("Name")); | |
inputPanel.add(nameField); | |
inputPanel.add(Box.createHorizontalStrut(5)); | |
inputPanel.add(new JLabel("Course")); | |
inputPanel.add(courseField); | |
inputPanel.add(Box.createHorizontalStrut(5)); | |
inputPanel.add(new JLabel("Group")); | |
inputPanel.add(groupField); | |
if (e.getSource() == add) { | |
surnameField.setText("Иванов"); | |
nameField.setText("Иван"); | |
courseField.setText("1"); | |
groupField.setText("1"); | |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); | |
if (node != null) { | |
while (!node.isLeaf()) | |
node = node.getFirstLeaf(); | |
Student student = (Student) node.getUserObject(); | |
courseField.setText(String.valueOf(student.getCourse())); | |
groupField.setText(String.valueOf(student.getGroup())); | |
} | |
if (JOptionPane.showConfirmDialog(this, inputPanel, "Добавить студента", | |
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, | |
new ImageIcon("src\\add.png")) == JOptionPane.OK_OPTION) { | |
try { | |
surname = surnameField.getText(); | |
name = nameField.getText(); | |
course = Integer.parseInt(courseField.getText()); | |
group = Integer.parseInt(groupField.getText()); | |
if ((group > 12) || (group < 1)) | |
throw new NumberFormatException(); | |
if ((course > 4) || (course < 1)) | |
throw new NumberFormatException(); | |
addStudent(course, group, surname, name); | |
tree.updateUI(); | |
} catch (NumberFormatException ex) { | |
JOptionPane.showMessageDialog(this, "Некорректый ввод", "Добавить студента", | |
JOptionPane.ERROR_MESSAGE, new ImageIcon("src\\add.png")); | |
} | |
} | |
} else if (e.getSource() == edit) { | |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); | |
if ((node != null) && (node.isLeaf())) { | |
Student student = (Student) node.getUserObject(); | |
courseField.setText(String.valueOf(student.getCourse())); | |
groupField.setText(String.valueOf(student.getGroup())); | |
surnameField.setText(student.getSurname()); | |
nameField.setText(student.getName()); | |
if (JOptionPane.showConfirmDialog(this, inputPanel, "Редактировать студента", | |
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, | |
new ImageIcon("src\\edit.png")) == JOptionPane.OK_OPTION) { | |
try { | |
surname = surnameField.getText(); | |
name = nameField.getText(); | |
course = Integer.parseInt(courseField.getText()); | |
if ((course > 4) || (course < 1)) | |
throw new NumberFormatException(); | |
group = Integer.parseInt(groupField.getText()); | |
if ((group > 12) || (group < 1)) | |
throw new NumberFormatException(); | |
addStudent(course, group, surname, name); | |
deleteStudent(student, node); | |
tree.setSelectionPath(null); | |
tree.updateUI(); | |
} catch (NumberFormatException ex) { | |
JOptionPane.showMessageDialog(this, "Некорректый ввод", "Редактировать студента", | |
JOptionPane.ERROR_MESSAGE, new ImageIcon("src\\edit.png")); | |
} | |
} | |
} else { | |
JOptionPane.showMessageDialog(this, "Выберите студента", "Редактировать студента", | |
JOptionPane.INFORMATION_MESSAGE, new ImageIcon("src\\edit.png")); | |
} | |
} else if (e.getSource() == delete) { | |
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); | |
if ((node != null) && (node.isLeaf())) { | |
Student student = (Student) node.getUserObject(); | |
surnameField.setText(student.getSurname()); | |
nameField.setText(student.getName()); | |
courseField.setText(String.valueOf(student.getCourse())); | |
groupField.setText(String.valueOf(student.getGroup())); | |
surnameField.setEditable(false); | |
nameField.setEditable(false); | |
courseField.setEditable(false); | |
groupField.setEditable(false); | |
if (JOptionPane.showConfirmDialog(this, inputPanel, "Удалить студента", | |
JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, | |
new ImageIcon("src\\delete.png")) == JOptionPane.OK_OPTION) { | |
deleteStudent(student, node); | |
tree.setSelectionPath(null); | |
tree.updateUI(); | |
} | |
} else { | |
JOptionPane.showMessageDialog(this, "Выберите студента", "Удалить студента", | |
JOptionPane.INFORMATION_MESSAGE, new ImageIcon("src\\delete.png")); | |
} | |
} | |
} | |
private void insert(DefaultMutableTreeNode parent, DefaultMutableTreeNode child, Iterator<TreeNode> iterator, int s) { | |
DefaultMutableTreeNode temp; | |
do { | |
temp = (DefaultMutableTreeNode) iterator.next(); | |
if (Integer.parseInt(((String) temp.getUserObject()).replaceAll("\\D+","")) > s) | |
break; | |
} while (iterator.hasNext()); | |
if (Integer.parseInt(((String) temp.getUserObject()).replaceAll("\\D+","")) > s) { | |
parent.insert(child, parent.getIndex(temp)); | |
} else { | |
parent.add(child); | |
} | |
} | |
private DefaultMutableTreeNode find(DefaultMutableTreeNode root, String s) { | |
Iterator<TreeNode> iterator; | |
DefaultMutableTreeNode courseNode; | |
iterator = root.children().asIterator(); | |
do { | |
courseNode = (DefaultMutableTreeNode) iterator.next(); | |
if (courseNode.getUserObject().equals(s)) | |
break; | |
} while (iterator.hasNext()); | |
return courseNode; | |
} | |
private void addStudent(int course, int group, String surname, String name) { | |
List<Student> groupList; | |
Map<Integer, List<Student>> groupsMap; | |
DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); | |
DefaultMutableTreeNode courseNode, groupNode, studentNode, temp; | |
Student student = new Student(course, group, surname, name); | |
Iterator<TreeNode> iterator; | |
String sCourse = course + " курс"; | |
String sGroup = group + " группа"; | |
if (database.containsKey(course)) { | |
if (database.get(course).containsKey(group)) { | |
groupList = database.get(course).get(group); | |
courseNode = find(root, sCourse); | |
groupNode = find(courseNode, sGroup); | |
studentNode = new DefaultMutableTreeNode(student); | |
iterator = groupNode.children().asIterator(); | |
do { | |
temp = (DefaultMutableTreeNode) iterator.next(); | |
if (((Student) temp.getUserObject()).compareTo(student) > 0) | |
break; | |
} while (iterator.hasNext()); | |
if (((Student) temp.getUserObject()).compareTo(student) > 0) { | |
groupNode.insert(studentNode, groupNode.getIndex(temp)); | |
groupList.add(groupNode.getIndex(temp), student); | |
} else { | |
groupNode.add(studentNode); | |
groupList.add(student); | |
} | |
} else { | |
groupsMap = database.get(course); | |
groupList = new ArrayList<>(); | |
groupList.add(student); | |
groupsMap.put(group, groupList); | |
courseNode = find(root, sCourse); | |
groupNode = new DefaultMutableTreeNode(sGroup); | |
studentNode = new DefaultMutableTreeNode(student); | |
groupNode.add(studentNode); | |
iterator = courseNode.children().asIterator(); | |
insert(courseNode, groupNode, iterator, group); | |
} | |
} else { | |
groupsMap = new HashMap<>(); | |
groupList = new ArrayList<>(); | |
groupList.add(student); | |
groupsMap.put(group, groupList); | |
database.put(course, groupsMap); | |
courseNode = new DefaultMutableTreeNode(sCourse); | |
groupNode = new DefaultMutableTreeNode(sGroup); | |
studentNode = new DefaultMutableTreeNode(student); | |
groupNode.add(studentNode); | |
courseNode.add(groupNode); | |
iterator = root.children().asIterator(); | |
if (iterator.hasNext()) { | |
insert(root, courseNode, iterator, course); | |
} else { | |
root.add(courseNode); | |
} | |
} | |
} | |
private void deleteStudent(Student student, DefaultMutableTreeNode studentNode) { | |
database.get(student.getCourse()).get(student.getGroup()).remove(student); | |
DefaultMutableTreeNode groupNode = (DefaultMutableTreeNode) studentNode.getParent(); | |
groupNode.remove(studentNode); | |
if (groupNode.isLeaf()) { | |
database.get(student.getCourse()).remove(student.getGroup()); | |
DefaultMutableTreeNode courseNode = (DefaultMutableTreeNode) groupNode.getParent(); | |
courseNode.remove(groupNode); | |
if (courseNode.isLeaf()) { | |
database.remove(student.getCourse()); | |
((DefaultMutableTreeNode) model.getRoot()).remove(courseNode); | |
} | |
} | |
} | |
} | |
class CellRenderer extends DefaultTreeCellRenderer { | |
private Icon[] icons; | |
private JLabel label; | |
public CellRenderer() { | |
icons = new Icon[]{new ImageIcon("src\\database.png"), | |
new ImageIcon("src\\course.png"), new ImageIcon("src\\group.png"), | |
new ImageIcon("src\\student.png")}; | |
super.setFont(new Font("Verdana", Font.PLAIN, 14)); | |
} | |
@Override | |
public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) { | |
label = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, | |
leaf, row, hasFocus); | |
if (leaf) { | |
label.setIcon(icons[3]); | |
} else if (value.toString().contains("группа")) { | |
label.setIcon(icons[2]); | |
} else if (value.toString().contains("курс")) { | |
label.setIcon(icons[1]); | |
} else { | |
label.setIcon(icons[0]); | |
} | |
return label; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://img.icons8.com/color/48/000000/database.png | |
https://img.icons8.com/color/48/000000/team-skin-type-7.png | |
https://img.icons8.com/color/48/000000/add-user-female-skin-type-7.png | |
https://img.icons8.com/color/48/000000/registration-skin-type-7.png | |
https://img.icons8.com/color/48/000000/banned-skin-type-7.png | |
https://img.icons8.com/color/48/000000/user-group-skin-type-7.png | |
https://img.icons8.com/color/48/000000/person-female-skin-type-7.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment