Example #1
0
  @SuppressWarnings("unchecked")
  public void paid(ActionEvent e) {
    try {
      StudentJdc jdc = classList.getSelectionModel().getSelectedItem();
      Bill bill = new Bill();
      bill.setPaid(stringToInt.apply(pay.getText()));
      bill.setStudentJdc(jdc);
      Transaction tran = new Transaction();
      tran.setIncome(bill.getPaid());
      tran.setComment(jdc.toString() + " -> " + jdc.getStudent().getName());

      bill.setTransaction(tran);

      BillBroker.getInstance().persist(bill, Bill.class);

      // referesh student
      ApplicationContext.put(CommonList.Student, StudentBroker.getInstance().getAll());

      // select student
      studentList.getItems().clear();
      studentList.getItems().addAll((List<Student>) ApplicationContext.get(CommonList.Student));
      needToPay.getItems().clear();
      needToPay.getItems().addAll(StudentBroker.getInstance().getAllToPaid());
      studentList.getSelectionModel().select(jdc.getStudent());

      // select class
      classList.getSelectionModel().select(jdc);

    } catch (JdcException je) {
      if (je.isAlert()) showError("Error", je.getMessage());
    }
  }
Example #2
0
  @SuppressWarnings("unchecked")
  @Override
  public void initialize(URL location, ResourceBundle resources) {
    super.initialize(location, resources);
    ToggleGroup gender = new ToggleGroup();
    gender.getToggles().add(male);
    gender.getToggles().add(female);
    male.setSelected(true);
    this.townships.getItems().addAll((List<Township>) ApplicationContext.get(CommonList.Township));
    // students
    this.studentList.getItems().addAll((List<Student>) ApplicationContext.get(CommonList.Student));

    this.studentList
        .getSelectionModel()
        .selectedItemProperty()
        .addListener(
            (a, b, c) -> {
              if (null != c) {
                setStudent(this.studentList.getSelectionModel().getSelectedItem());
                needToPay.getSelectionModel().clearSelection();
              }
            });

    // students to pay
    this.needToPay.getItems().addAll(StudentBroker.getInstance().getAllToPaid());

    this.needToPay
        .getSelectionModel()
        .selectedItemProperty()
        .addListener(
            (a, b, c) -> {
              if (null != c) {
                setStudent(this.needToPay.getSelectionModel().getSelectedItem());
                studentList.getSelectionModel().clearSelection();
              }
            });

    // init list
    this.classList
        .getSelectionModel()
        .selectedItemProperty()
        .addListener(
            (a, b, c) -> {
              this.edit.setDisable(false);
              this.submit.setDisable(false);
              this.pay.setEditable(true);
              // set bills
              StudentJdc jdc = classList.getSelectionModel().getSelectedItem();
              this.billList.getItems().clear();
              List<Bill> bills = StudentBroker.getInstance().getBillByJdcClass(jdc);
              this.billList.getItems().addAll(bills);
              // set balance
              if (null != bills && bills.size() > 0) {
                this.courseFee.setText(
                    String.valueOf(
                        bills
                            .get(bills.size() - 1)
                            .getStudentJdc()
                            .getJdcClass()
                            .getCourse()
                            .getFee()));
                this.discount.setText(String.valueOf(bills.get(bills.size() - 1).getDiscount()));
                this.fee.setText(substract.apply(this.courseFee, this.discount));
                this.paid.setText(String.valueOf(bills.stream().mapToInt(v -> v.getPaid()).sum()));
                this.remain.setText(substract.apply(this.fee, this.paid));
                this.pay.clear();
                if ("0".equals(this.remain.getText())) {
                  this.pay.setEditable(false);
                  this.submit.setDisable(true);
                }
              } else {
                clearTextFields.accept(
                    Arrays.asList(courseFee, discount, fee, paid, remain)
                        .toArray(new TextField[5]));
              }
            });

    filter
        .textProperty()
        .addListener(
            (a, b, c) ->
                studentListFilter =
                    (w, list) -> {
                      final List<Student> students = new ArrayList<>();
                      if (null == w || w.isEmpty()) {
                        students.addAll(StudentBroker.getInstance().getAllToPaid());
                      } else {
                        students.addAll(
                            list.getItems()
                                .stream()
                                .filter(s -> s.getName().startsWith(w))
                                .collect(Collectors.toList()));
                      }
                      list.getItems().clear();
                      list.getItems().addAll(students);
                    });

    filter.textProperty().addListener((a, b, c) -> studentListFilter.accept(c, studentList));

    cancel.setOnAction(
        e -> {
          setEditMode(false);
          edit.setText("Edit");
        });

    this.submit.setOnAction(StudentsController.this::paid);

    this.edit.setOnAction(
        e -> {
          if ("Edit".equals(((Button) e.getSource()).getText())) {
            this.setEditMode(true);
            edit.setText("Save");
          } else {
            boolean fromTop = this.needToPay.getSelectionModel().getSelectedItem() != null;
            this.setEditMode(false);
            Student stu = getStudent();
            StudentBroker.getInstance().update(stu, Student.class);
            ApplicationContext.put(CommonList.Student, StudentBroker.getInstance().getAll());
            this.studentList.getItems().clear();
            this.studentList
                .getItems()
                .addAll((List<Student>) ApplicationContext.get(CommonList.Student));
            this.needToPay.getItems().clear();
            this.needToPay.getItems().addAll(StudentBroker.getInstance().getAllToPaid());
            if (fromTop) {
              this.selectById(stu.getId(), needToPay);
            } else {
              this.selectById(stu.getId(), studentList);
            }
            edit.setText("Edit");
          }
        });
  }