@Override public void actionPerformed(ActionEvent e) { Connection conn = null; Statement statement = null; try { conn = ConnectToDatabase.connect(); Complaint compl = new Complaint(); statement = conn.createStatement(); compl.setDateTime(DateTime.dateTime()); String insertComplaint = "INSERT INTO book (complaint, date) VALUES('" + text.getText() + "', '" + compl.getDateTime() + "')"; statement.executeUpdate(insertComplaint); } catch (SQLException e1) { System.err.println("Could not connect to the database."); System.err.println("Msg: " + e1.getMessage()); System.exit(1); } catch (ClassNotFoundException e3) { System.err.println("JDBC library is not loaded."); System.err.println("Msg: " + e3.getMessage()); System.exit(1); } finally { CloseConnections.closeStatement(statement); CloseConnections.closeConnection(conn); } }
/** * The construct creates an instance of a DeathInfo class. * * @param person the dead person */ public DeathInfo(Person person) { // Initialize data members timeOfDeath = Simulation.instance().getMasterClock().getMarsClock().getDateTimeStamp(); Complaint serious = person.getPhysicalCondition().getMostSerious(); if (serious != null) illness = serious.getName(); if (person.getLocationSituation() == LocationSituation.OUTSIDE) placeOfDeath = "Outside"; else { containerUnit = person.getContainerUnit(); placeOfDeath = containerUnit.getName(); } locationOfDeath = person.getCoordinates(); Mind mind = person.getMind(); job = mind.getJob(); if (mind.getMission() != null) { mission = mind.getMission().getName(); missionPhase = mind.getMission().getPhaseDescription(); } TaskManager taskMgr = mind.getTaskManager(); if (taskMgr.hasTask()) { task = taskMgr.getTaskName(); TaskPhase phase = taskMgr.getPhase(); if (phase != null) { taskPhase = phase.getName(); } else { taskPhase = ""; } } Iterator<Malfunctionable> i = MalfunctionFactory.getMalfunctionables(person).iterator(); Malfunction mostSerious = null; int severity = 0; while (i.hasNext()) { Malfunctionable entity = i.next(); MalfunctionManager malfunctionMgr = entity.getMalfunctionManager(); if (malfunctionMgr.hasEmergencyMalfunction()) { Malfunction m = malfunctionMgr.getMostSeriousEmergencyMalfunction(); if (m.getSeverity() > severity) { mostSerious = m; severity = m.getSeverity(); } } } if (mostSerious != null) malfunction = mostSerious.getName(); this.gender = person.getGender(); }
private static JPAQuery query( String select, Zone zone, CulpritCategory category, Integer status, String keyword) { StringBuilder sql = new StringBuilder(); sql.append(" FROM Culprit c WHERE 1 = 1 "); if (zone != null) sql.append("AND c.zone = :zone "); if (category != null) sql.append("AND c.category = :category "); if (status != null) sql.append("AND c.status = :status "); else sql.append("AND c.status != :status "); if (StringUtils.isNotEmpty(keyword)) sql.append("AND c.story like :keyword "); sql.append("ORDER BY c.log.createdAt DESC "); JPAQuery query = Complaint.find(select + sql.toString()); if (status != null) query.setParameter("status", status); else query.setParameter("status", Constant.SLA_DELETED); if (zone != null) query.bind("zone", zone); if (category != null) query.bind("category", category); if (StringUtils.isNotEmpty(keyword)) query.bind("keyword", "%" + keyword + "%"); return query; }
/** * this method manages adding, updating, displaying and removing complaints. firstly user is asked * to enter either of 4 options */ private void manageComplaints() { System.out.println(" Please select one of the following options: "); System.out.println(" ---------"); System.out.println(" 1) Add Complaint"); System.out.println(" 2) Update Complaint"); System.out.println(" 3) Display Complaints by Doctor"); System.out.println(" 4) Remove Complaint"); System.out.println("==>> "); boolean correct = false; int option = 0; do { try { option = input.nextInt(); if (option >= 1 && option <= 4) { correct = true; } else { System.out.println("Invalid option selected. Please select 1,2,3 or 4"); } } catch (InputMismatchException e) { input.nextLine(); System.out.println("Number needed... Please try again"); } } while (!correct); ArrayList<Integer> validIndices = new ArrayList<Integer>(); int doctorIndex = 0; switch (option) { /* * ArrayList of doctors is iterated and valid indices (those of * qualified doctors) are added to ArrayList of Integers. then user is * prompted to pick on of the indices displayed and if chooses valid one * - a complaint is assigned to the doctor's ArrayList of complaint */ case 1: System.out.println("QuaLified doctors in the council: "); for (int i = 0; i < doctors.size(); i++) { if (!(doctors.get(i) instanceof Internship)) { System.out.println(i + " :" + doctors.get(i).getName()); validIndices.add(i); } } System.out.print( "\nPlease select an index of doctor to add new complaint to his/her record: "); correct = false; do { try { doctorIndex = input.nextInt(); if (validIndices.contains(doctorIndex)) { correct = true; } else { System.out.println("Invalid index chosen. Please enter correct index >> "); } } catch (InputMismatchException e) { input.nextLine(); System.out.println("Number needed... Please try again"); } } while (!correct); if (doctors.get(doctorIndex) instanceof General) { ((General) doctors.get(doctorIndex)) .addComplaint(new Complaint(getNextAvailableComplaintId())); nextAvailableComplaintId++; } else { ((Specialist) doctors.get(doctorIndex)) .addComplaint(new Complaint(getNextAvailableComplaintId())); nextAvailableComplaintId++; } System.out.println( "A complaint has been added to " + doctors.get(doctorIndex).getName() + " record"); break; case 2: /* * ArrayList of doctors is iterated and valid indices (those of * qualified doctors) are added to ArrayList of Integers. then user * is prompted to pick on of the indices displayed and if chooses * valid one - list of indices of complains is displayed along with * a prompt to choose one of them. Then user is asked to enter a * status to be changed for the complaint chosen. */ validIndices = new ArrayList<Integer>(); doctorIndex = 0; if (noComplaints()) { System.out.println("No complaints to update"); } else { System.out.println("Please select doctor index to update his/her complaints"); for (int i = 0; i < doctors.size(); i++) { if (doctors.get(i) instanceof QualifiedDoctor) { if (((QualifiedDoctor) doctors.get(i)).getComplaintsCount() > 0) { System.out.println(i + ": " + doctors.get(i).getName()); validIndices.add(i); } } } correct = false; do { try { doctorIndex = input.nextInt(); if (validIndices.contains(doctorIndex)) { correct = true; } else { System.out.println("Invalid index chosen. Please enter correct index >> "); } } catch (InputMismatchException e) { input.nextLine(); System.out.println("Number needed... Please try again"); } } while (!correct); validIndices = new ArrayList<Integer>(); correct = false; System.out.println("Please select complaint id to update from following valid indices: "); for (Complaint complaint : (((QualifiedDoctor) doctors.get(doctorIndex)).getComplaints())) { System.out.print(complaint.getComplaintId()); validIndices.add(complaint.getComplaintId()); } int complaintId = 0; do { try { complaintId = input.nextInt(); if (validIndices.contains(complaintId)) { correct = true; } else { System.out.println("Invalid index chosen. Please enter correct index >> "); } } catch (InputMismatchException e) { input.nextLine(); System.out.println("Number needed... Please try again"); } } while (!correct); correct = false; System.out.println("Please enter status (\"open\",\"upheld\" or \"rejected\"): "); input.nextLine(); String status; do { status = input.nextLine(); if (status.equalsIgnoreCase("open") || status.equalsIgnoreCase("rejected") || status.equalsIgnoreCase("upheld")) { correct = true; } else { System.out.println( "Invalid status entered. Please enter: \"open\",\"upheld\" or \"rejected\""); } } while (!correct); Complaint complaintToUpdate; for (int i = 0; i < ((QualifiedDoctor) doctors.get(doctorIndex)).getComplaints().size(); i++) { if (((QualifiedDoctor) doctors.get(doctorIndex)).getComplaints().get(i).getComplaintId() == complaintId) { complaintToUpdate = ((QualifiedDoctor) doctors.get(doctorIndex)).getComplaints().get(i); ((QualifiedDoctor) doctors.get(doctorIndex)) .updateComplaintStatus(complaintToUpdate, status); } } } break; case 3: /* * the same mechanism as above. when user successfully picks all the * options complaints of chosen doctor are displayed */ doctorIndex = 0; validIndices = new ArrayList<Integer>(); correct = false; if (noComplaints()) { System.out.println("No complaints to display"); } else { System.out.println( "Please choose an index of a doctor to display his/her complaints details: "); for (int i = 0; i < doctors.size(); i++) { if (doctors.get(i) instanceof QualifiedDoctor) { if (((QualifiedDoctor) doctors.get(i)).getComplaintsCount() > 0) { System.out.println(i + ": " + doctors.get(i).getName()); validIndices.add(i); } } } do { try { doctorIndex = input.nextInt(); if (validIndices.contains(doctorIndex)) { correct = true; } else { System.out.println("Invalid index chosen. Please enter correct index >> "); } } catch (InputMismatchException e) { input.nextLine(); System.out.println("Number needed... Please try again"); } } while (!correct); for (Complaint complaint : ((QualifiedDoctor) doctors.get(doctorIndex)).getComplaints()) { System.out.println(complaint.toString()); } } break; case 4: /* * as above, but this time a complaint gets removed */ doctorIndex = 0; validIndices = new ArrayList<Integer>(); correct = false; if (noComplaints()) { System.out.println("No complaints to remove"); } else { System.out.println("Please select doctor index to remove his/her complaints"); for (int i = 0; i < doctors.size(); i++) { if (doctors.get(i) instanceof QualifiedDoctor) { if (((QualifiedDoctor) doctors.get(i)).getComplaintsCount() > 0) { System.out.println(i + ": " + doctors.get(i).getName()); validIndices.add(i); } } } do { try { doctorIndex = input.nextInt(); if (validIndices.contains(doctorIndex)) { correct = true; } else { System.out.println("Invalid index chosen. Please enter correct index >> "); } } catch (InputMismatchException e) { input.nextLine(); System.out.println("Number needed... Please try again"); } } while (!correct); correct = false; System.out.println( "Please select complaint index to remove from following valid indices: "); for (Complaint complaint : (((QualifiedDoctor) doctors.get(doctorIndex)).getComplaints())) { System.out.println(complaint.getComplaintId()); validIndices.add(complaint.getComplaintId()); } int complaintIndex = 0; do { try { complaintIndex = input.nextInt(); if (validIndices.contains(complaintIndex)) { correct = true; } else { System.out.println("Invalid index chosen. Please enter correct index >> "); } } catch (InputMismatchException e) { input.nextLine(); System.out.println("Number needed... Please try again"); } } while (!correct); System.out.println( ((QualifiedDoctor) doctors.get(doctorIndex)) .getComplaints() .remove(complaintIndex) .toString() + "has been removed"); } break; default: System.out.println("Invalid option. Press any key to continue..."); break; } }
public void writeCsvFile(ArrayList<Complaint> complaints) throws IOException { int arraySize = complaints.size(); // Index start int beginIndex = getAmountLines(); // Index end int endIndex = arraySize - beginIndex; try { if (beginIndex > 0) { append = true; remainingItems = complaints.subList(beginIndex, endIndex); } else if (beginIndex == 0) { append = false; remainingItems = complaints; } // FileWriter fileWriter = new FileWriter(file, append); // Write the CSV file header fileWriter.append(FILE_HEADER); // Add a new line separator after the header fileWriter.append(NEW_LINE_SEPARATOR); // Write a new student object list to the CSV file for (Complaint complaint : remainingItems) { fileWriter.append(String.valueOf(complaint.getIngevoerd())); fileWriter.append(COMMA_DELIMITER); fileWriter.append(String.valueOf(complaint.getDatum())); fileWriter.append(COMMA_DELIMITER); fileWriter.append(String.valueOf(complaint.getAantal())); fileWriter.append(COMMA_DELIMITER); fileWriter.append(complaint.getPostcode()); fileWriter.append(COMMA_DELIMITER); fileWriter.append(complaint.getPlaatsnaam()); fileWriter.append(COMMA_DELIMITER); fileWriter.append(complaint.getAardOverlast()); fileWriter.append(COMMA_DELIMITER); fileWriter.append(complaint.getSubOverlast()); fileWriter.append(COMMA_DELIMITER); fileWriter.append(complaint.getSubSubaardOverlast()); fileWriter.append(COMMA_DELIMITER); fileWriter.append(String.valueOf(complaint.getTerugkoppeling())); fileWriter.append(COMMA_DELIMITER); fileWriter.append(String.valueOf(complaint.getLatitude())); fileWriter.append(COMMA_DELIMITER); fileWriter.append(String.valueOf(complaint.getLongitude())); fileWriter.append(NEW_LINE_SEPARATOR); } logger.info("CSV created!"); } catch (Exception e) { logger.error("Error in FileWriter"); logger.error(e); } finally { fileWriter.close(); } }