├── Database └── Database.java ├── EventListners ├── Calculator$1.class ├── Calculator$2.class ├── Calculator$3.class ├── Calculator$4.class ├── Calculator$5.class ├── Calculator$ButtonClickListener.class ├── Calculator.class └── Calculator.java ├── JavaRemoteMethodInvocaton ├── RMIClient.java ├── RMIServer.java ├── RemoteImplementation.java └── RemoteInterface.java └── VOTING_SYSTEM ├── Voter.class ├── VoterRegistrationGUI$1.class ├── VoterRegistrationGUI$2.class ├── VoterRegistrationGUI.class ├── VoterRegistrationGUI.java ├── registerVoter.class ├── registerVoter.java ├── registration.csv └── temp.csv /Database/Database.java: -------------------------------------------------------------------------------- 1 | import java.sql.*; 2 | 3 | public class Database { 4 | static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 5 | static final String DB_URL = "jdbc:mysql://localhost/JavaDatabase"; 6 | static final String USER = "root"; 7 | static final String PASS = ""; 8 | 9 | public static void main(String[] args) { 10 | Connection conn = null; 11 | Statement stmt = null; 12 | 13 | try { 14 | // Register JDBC driver 15 | Class.forName("com.mysql.cj.jdbc.Driver"); 16 | 17 | System.out.println("Connecting to the database..."); 18 | conn = DriverManager.getConnection(DB_URL, USER, PASS); 19 | 20 | System.out.println("Creating statement..."); 21 | stmt = conn.createStatement(); 22 | String sql; 23 | sql = "SELECT name, salary, department FROM employees"; 24 | ResultSet rs = stmt.executeQuery(sql); 25 | 26 | // Process the result set 27 | while (rs.next()) { 28 | String name = rs.getString("name"); 29 | double salary = rs.getDouble("salary"); 30 | String department = rs.getString("department"); 31 | 32 | // Display values 33 | System.out.println("Name: " + name); 34 | System.out.println("Salary: " + salary); 35 | System.out.println("Department: " + department); 36 | } 37 | 38 | // Clean-up environment 39 | rs.close(); 40 | stmt.close(); 41 | conn.close(); 42 | } catch (SQLException se) { 43 | // Handle errors for JDBC 44 | se.printStackTrace(); 45 | } catch (Exception e) { 46 | // Handle errors for Class.forName 47 | e.printStackTrace(); 48 | } finally { 49 | // Finally block used to close resources 50 | try { 51 | if (stmt != null) 52 | stmt.close(); 53 | } catch (SQLException se) { 54 | se.printStackTrace(); 55 | } 56 | try { 57 | if (conn != null) 58 | conn.close(); 59 | } catch (SQLException se) { 60 | se.printStackTrace(); 61 | } 62 | } 63 | System.out.println("Goodbye!"); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /EventListners/Calculator$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/EventListners/Calculator$1.class -------------------------------------------------------------------------------- /EventListners/Calculator$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/EventListners/Calculator$2.class -------------------------------------------------------------------------------- /EventListners/Calculator$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/EventListners/Calculator$3.class -------------------------------------------------------------------------------- /EventListners/Calculator$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/EventListners/Calculator$4.class -------------------------------------------------------------------------------- /EventListners/Calculator$5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/EventListners/Calculator$5.class -------------------------------------------------------------------------------- /EventListners/Calculator$ButtonClickListener.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/EventListners/Calculator$ButtonClickListener.class -------------------------------------------------------------------------------- /EventListners/Calculator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/EventListners/Calculator.class -------------------------------------------------------------------------------- /EventListners/Calculator.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | 6 | public class Calculator { 7 | 8 | private JFrame frame; 9 | private JTextField display; 10 | private String currentInput = ""; 11 | private double result = 0; 12 | private String lastOperator = ""; 13 | private boolean isNewInput = true; 14 | 15 | public Calculator() { 16 | frame = new JFrame("Calculator"); 17 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 | frame.setSize(300, 400); 19 | frame.setLayout(new BorderLayout()); 20 | 21 | display = new JTextField(); 22 | display.setEditable(false); 23 | frame.add(display, BorderLayout.NORTH); 24 | 25 | JPanel buttonPanel = new JPanel(); 26 | buttonPanel.setLayout(new GridLayout(4, 4)); 27 | 28 | String[] buttonLabels = { 29 | "7", "8", "9", "/", 30 | "4", "5", "6", "*", 31 | "1", "2", "3", "-", 32 | "0", "C", "=", "+" 33 | }; 34 | 35 | for (String label : buttonLabels) { 36 | JButton button = new JButton(label); 37 | button.addActionListener(new ButtonClickListener()); 38 | buttonPanel.add(button); 39 | } 40 | 41 | frame.add(buttonPanel, BorderLayout.CENTER); 42 | 43 | frame.setVisible(true); 44 | } 45 | 46 | private class ButtonClickListener implements ActionListener { 47 | public void actionPerformed(ActionEvent e) { 48 | JButton source = (JButton) e.getSource(); 49 | String buttonText = source.getText(); 50 | 51 | if (isNewInput) { 52 | display.setText(""); 53 | currentInput = ""; 54 | isNewInput = false; 55 | } 56 | 57 | if (buttonText.matches("[0-9]")) { 58 | currentInput += buttonText; 59 | display.setText(display.getText() + buttonText); 60 | } else if (buttonText.matches("[/+*-]")) { 61 | if (!currentInput.isEmpty()) { 62 | double inputNumber = Double.parseDouble(currentInput); 63 | if (lastOperator.equals("+")) { 64 | result += inputNumber; 65 | } else if (lastOperator.equals("-")) { 66 | result -= inputNumber; 67 | } else if (lastOperator.equals("*")) { 68 | result *= inputNumber; 69 | } else if (lastOperator.equals("/")) { 70 | if (inputNumber != 0) { 71 | result /= inputNumber; 72 | } else { 73 | display.setText("Error"); 74 | return; 75 | } 76 | } else { 77 | result = inputNumber; 78 | } 79 | } 80 | currentInput = ""; 81 | lastOperator = buttonText; 82 | } else if (buttonText.equals("=")) { 83 | if (!currentInput.isEmpty()) { 84 | double inputNumber = Double.parseDouble(currentInput); 85 | if (lastOperator.equals("+")) { 86 | result += inputNumber; 87 | } else if (lastOperator.equals("-")) { 88 | result -= inputNumber; 89 | } else if (lastOperator.equals("*")) { 90 | result *= inputNumber; 91 | } else if (lastOperator.equals("/")) { 92 | if (inputNumber != 0) { 93 | result /= inputNumber; 94 | } else { 95 | display.setText("Error"); 96 | return; 97 | } 98 | } 99 | display.setText(Double.toString(result)); 100 | isNewInput = true; 101 | } 102 | } else if (buttonText.equals("C")) { 103 | currentInput = ""; 104 | result = 0; 105 | lastOperator = ""; 106 | display.setText(""); 107 | } 108 | } 109 | } 110 | 111 | public static void main(String[] args) { 112 | SwingUtilities.invokeLater(new Runnable() { 113 | public void run() { 114 | new Calculator(); 115 | } 116 | }); 117 | } 118 | } -------------------------------------------------------------------------------- /JavaRemoteMethodInvocaton/RMIClient.java: -------------------------------------------------------------------------------- 1 | // RMIClient.java 2 | import java.rmi.Naming; 3 | 4 | public class RMIClient { 5 | public static void main(String[] args) { 6 | try { 7 | // Lookup the remote object 8 | RemoteInterface remoteObj = (RemoteInterface) Naming.lookup("rmi://localhost/RemoteObject"); 9 | 10 | // Invoke remote method 11 | String result = remoteObj.sayHello(); 12 | 13 | // Display the result 14 | System.out.println("Message from server: " + result); 15 | } catch (Exception e) { 16 | e.printStackTrace(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /JavaRemoteMethodInvocaton/RMIServer.java: -------------------------------------------------------------------------------- 1 | // RMIServer.java 2 | import java.rmi.Naming; 3 | import java.rmi.registry.LocateRegistry; 4 | 5 | public class RMIServer { 6 | public static void main(String[] args) { 7 | try { 8 | // Create and export the remote object 9 | RemoteInterface remoteObj = new RemoteImplementation(); 10 | 11 | // Create RMI registry on port 1099 12 | LocateRegistry.createRegistry(1099); 13 | 14 | // Bind the remote object to the registry 15 | Naming.bind("rmi://localhost/RemoteObject", remoteObj); 16 | 17 | System.out.println("Server is ready."); 18 | } catch (Exception e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /JavaRemoteMethodInvocaton/RemoteImplementation.java: -------------------------------------------------------------------------------- 1 | // RemoteImplementation.java 2 | import java.rmi.RemoteException; 3 | import java.rmi.server.UnicastRemoteObject; 4 | 5 | public class RemoteImplementation extends UnicastRemoteObject implements RemoteInterface { 6 | protected RemoteImplementation() throws RemoteException { 7 | super(); 8 | } 9 | 10 | @Override 11 | public String sayHello() throws RemoteException { 12 | return "Hello, from the server!"; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /JavaRemoteMethodInvocaton/RemoteInterface.java: -------------------------------------------------------------------------------- 1 | // RemoteInterface.java 2 | import java.rmi.Remote; 3 | import java.rmi.RemoteException; 4 | 5 | public interface RemoteInterface extends Remote { 6 | String sayHello() throws RemoteException; 7 | } 8 | -------------------------------------------------------------------------------- /VOTING_SYSTEM/Voter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/VOTING_SYSTEM/Voter.class -------------------------------------------------------------------------------- /VOTING_SYSTEM/VoterRegistrationGUI$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/VOTING_SYSTEM/VoterRegistrationGUI$1.class -------------------------------------------------------------------------------- /VOTING_SYSTEM/VoterRegistrationGUI$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/VOTING_SYSTEM/VoterRegistrationGUI$2.class -------------------------------------------------------------------------------- /VOTING_SYSTEM/VoterRegistrationGUI.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/VOTING_SYSTEM/VoterRegistrationGUI.class -------------------------------------------------------------------------------- /VOTING_SYSTEM/VoterRegistrationGUI.java: -------------------------------------------------------------------------------- 1 | import javax.swing.*; 2 | import java.awt.*; 3 | import java.awt.event.ActionEvent; 4 | import java.awt.event.ActionListener; 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | 8 | public class VoterRegistrationGUI { 9 | private JFrame frame; 10 | private JTextField nameField; 11 | private JTextField ageField; 12 | private JTextField districtField; 13 | private JTextField electorateField; 14 | private List voters; 15 | private int selectedIndex = -1; 16 | 17 | public static void main(String[] args) { 18 | EventQueue.invokeLater(() -> { 19 | try { 20 | VoterRegistrationGUI window = new VoterRegistrationGUI(); 21 | window.frame.setVisible(true); 22 | } catch (Exception e) { 23 | e.printStackTrace(); 24 | } 25 | }); 26 | } 27 | 28 | public VoterRegistrationGUI() { 29 | initialize(); 30 | voters = new ArrayList<>(); 31 | } 32 | 33 | private void initialize() { 34 | frame = new JFrame(); 35 | frame.setBounds(100, 100, 450, 300); 36 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 37 | frame.getContentPane().setLayout(new BorderLayout(0, 0)); 38 | 39 | JPanel inputPanel = new JPanel(); 40 | frame.getContentPane().add(inputPanel, BorderLayout.CENTER); 41 | inputPanel.setLayout(new GridLayout(4, 2, 0, 10)); 42 | 43 | JLabel lblName = new JLabel("Name:"); 44 | inputPanel.add(lblName); 45 | 46 | nameField = new JTextField(); 47 | inputPanel.add(nameField); 48 | nameField.setColumns(10); 49 | 50 | JLabel lblAge = new JLabel("Age:"); 51 | inputPanel.add(lblAge); 52 | 53 | ageField = new JTextField(); 54 | inputPanel.add(ageField); 55 | ageField.setColumns(10); 56 | 57 | JLabel lblDistrict = new JLabel("District:"); 58 | inputPanel.add(lblDistrict); 59 | 60 | districtField = new JTextField(); 61 | inputPanel.add(districtField); 62 | districtField.setColumns(10); 63 | 64 | JLabel lblElectorate = new JLabel("Electorate:"); 65 | inputPanel.add(lblElectorate); 66 | 67 | electorateField = new JTextField(); 68 | inputPanel.add(electorateField); 69 | electorateField.setColumns(10); 70 | 71 | JPanel buttonPanel = new JPanel(); 72 | frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH); 73 | 74 | JButton btnRegister = new JButton("Register"); 75 | buttonPanel.add(btnRegister); 76 | btnRegister.addActionListener(new ActionListener() { 77 | public void actionPerformed(ActionEvent e) { 78 | registerVoter(); 79 | } 80 | }); 81 | 82 | JButton btnEdit = new JButton("Edit"); 83 | buttonPanel.add(btnEdit); 84 | btnEdit.addActionListener(new ActionListener() { 85 | public void actionPerformed(ActionEvent e) { 86 | editVoter(); 87 | } 88 | }); 89 | } 90 | 91 | private void registerVoter() { 92 | String name = nameField.getText(); 93 | int age = Integer.parseInt(ageField.getText()); 94 | String district = districtField.getText(); 95 | String electorate = electorateField.getText(); 96 | 97 | Voter voter = new Voter(name, age, district, electorate); 98 | voters.add(voter); 99 | 100 | JOptionPane.showMessageDialog(frame, "Voter registered successfully!"); 101 | clearFields(); 102 | } 103 | 104 | private void editVoter() { 105 | selectedIndex = getSelectedIndex(); 106 | if (selectedIndex != -1) { 107 | Voter selectedVoter = voters.get(selectedIndex); 108 | nameField.setText(selectedVoter.getName()); 109 | ageField.setText(String.valueOf(selectedVoter.getAge())); 110 | districtField.setText(selectedVoter.getDistrict()); 111 | electorateField.setText(selectedVoter.getElectorate()); 112 | } 113 | } 114 | 115 | private void clearFields() { 116 | nameField.setText(""); 117 | ageField.setText(""); 118 | districtField.setText(""); 119 | electorateField.setText(""); 120 | } 121 | 122 | private int getSelectedIndex() { 123 | // For now, returning 0 for demonstration purposes 124 | return 0; 125 | } 126 | } 127 | 128 | class Voter { 129 | private String name; 130 | private int age; 131 | private String district; 132 | private String electorate; 133 | 134 | public Voter(String name, int age, String district, String electorate) { 135 | this.name = name; 136 | this.age = age; 137 | this.district = district; 138 | this.electorate = electorate; 139 | } 140 | 141 | public String getName() { 142 | return name; 143 | } 144 | 145 | public int getAge() { 146 | return age; 147 | } 148 | 149 | public String getDistrict() { 150 | return district; 151 | } 152 | 153 | public String getElectorate() { 154 | return electorate; 155 | } 156 | } 157 | -------------------------------------------------------------------------------- /VOTING_SYSTEM/registerVoter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/VOTING_SYSTEM/registerVoter.class -------------------------------------------------------------------------------- /VOTING_SYSTEM/registerVoter.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.time.LocalDateTime; 3 | import java.time.format.DateTimeFormatter; 4 | import java.util.Scanner; 5 | import java.util.UUID; 6 | 7 | public class registerVoter { 8 | 9 | public static void main(String[] args) { 10 | int userIdCounter = readLastUserIdFromCSV(); 11 | Scanner scanner = new Scanner(System.in); 12 | 13 | boolean continueRegistration = true; 14 | 15 | while (continueRegistration) { 16 | System.out.println("Options:"); 17 | System.out.println("1. Register a voter"); 18 | System.out.println("2. Edit voter information"); 19 | System.out.println("3. Exit"); 20 | System.out.print("Enter your choice: "); 21 | 22 | int choice = getValidChoice(scanner, 1, 3); 23 | 24 | switch (choice) { 25 | case 1: 26 | userIdCounter = registerVoter(userIdCounter, scanner); 27 | break; 28 | case 2: 29 | editVoterInfo("registration.csv", userIdCounter, scanner); 30 | break; 31 | case 3: 32 | continueRegistration = false; 33 | break; 34 | default: 35 | System.out.println("Invalid choice. Please try again."); 36 | break; 37 | } 38 | } 39 | 40 | System.out.println("Thank you for using the Voter Registration System!"); 41 | scanner.close(); 42 | } 43 | 44 | private static int getValidChoice(Scanner scanner, int minChoice, int maxChoice) { 45 | while (true) { 46 | try { 47 | int choice = Integer.parseInt(scanner.nextLine()); 48 | if (choice >= minChoice && choice <= maxChoice) { 49 | return choice; 50 | } else { 51 | System.out.println("Invalid choice. Please enter a valid option (" + minChoice + "-" + maxChoice + ")."); 52 | } 53 | } catch (NumberFormatException e) { 54 | System.out.println("Invalid input. Please enter a number (" + minChoice + "-" + maxChoice + ")."); 55 | } 56 | } 57 | } 58 | 59 | private static int registerVoter(int userIdCounter, Scanner scanner) { 60 | int userId = ++userIdCounter; 61 | String guid = UUID.randomUUID().toString(); 62 | String uniqueIdentifier = guid + "_" + userId; 63 | 64 | System.out.println("Welcome to the Voter Registration System!"); 65 | 66 | System.out.print("Enter Voter's Name: "); 67 | String name = scanner.nextLine(); 68 | 69 | int age; 70 | do { 71 | System.out.print("Enter Voter's Age: "); 72 | while (!scanner.hasNextInt()) { 73 | System.out.println("Invalid input. Age must be a positive integer."); 74 | System.out.print("Enter Voter's Age: "); 75 | scanner.next(); 76 | } 77 | age = scanner.nextInt(); 78 | scanner.nextLine(); // Consume the newline character 79 | if (age <= 0 || age < 19) { 80 | System.out.println("Invalid input. Age must be a positive integer and above 18."); 81 | } 82 | } while (age <= 0 || age < 19); 83 | 84 | System.out.print("Enter Voter's District: "); 85 | String district = scanner.nextLine(); 86 | 87 | System.out.print("Enter Voter's Electorate: "); 88 | String electorate = scanner.nextLine(); 89 | 90 | LocalDateTime registrationDateTime = LocalDateTime.now(); 91 | String registrationTimestamp = registrationDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 92 | 93 | String fileName = "registration.csv"; 94 | 95 | try { 96 | File file = new File(fileName); 97 | if (!file.exists()) { 98 | file.createNewFile(); 99 | } 100 | 101 | FileWriter fileWriter = new FileWriter(fileName, true); 102 | BufferedWriter writer = new BufferedWriter(fileWriter); 103 | 104 | writer.write(uniqueIdentifier + "," + name + "," + age + "," + district + "," + electorate + "," 105 | + registrationTimestamp + "\n"); 106 | System.out.println("Registration successful! Voter's unique identifier is: " + uniqueIdentifier); 107 | 108 | writer.close(); 109 | } catch (IOException e) { 110 | e.printStackTrace(); 111 | System.err.println("An error occurred while saving the user registration information."); 112 | } 113 | 114 | return userIdCounter; 115 | } 116 | 117 | private static void editVoterInfo(String fileName, int userIdCounter, Scanner scanner) { 118 | System.out.print("Enter the userId of the voter you want to edit: "); 119 | int userIdToEdit = getValidUserId(scanner); 120 | 121 | String tempFileName = "temp.csv"; 122 | 123 | try (FileWriter fileWriter = new FileWriter(tempFileName); 124 | BufferedWriter writer = new BufferedWriter(fileWriter); 125 | BufferedReader reader = new BufferedReader(new FileReader(fileName))) { 126 | 127 | String line; 128 | boolean voterFound = false; 129 | 130 | while ((line = reader.readLine()) != null) { 131 | String[] parts = line.split(","); 132 | String uniqueIdentifier = parts[0]; 133 | 134 | String[] idParts = uniqueIdentifier.split("_"); 135 | String guid = idParts[0]; 136 | int userId = Integer.parseInt(idParts[1]); 137 | 138 | if (userId == userIdToEdit) { 139 | voterFound = true; 140 | System.out.println("Current Voter Information:"); 141 | System.out.println("Name: " + parts[1]); 142 | System.out.println("Age: " + parts[2]); 143 | System.out.println("District: " + parts[3]); 144 | System.out.println("Electorate: " + parts[4]); 145 | 146 | System.out.println("Options:"); 147 | System.out.println("1. View Information"); 148 | System.out.println("2. Edit Information"); 149 | System.out.println("3. Cancel"); 150 | 151 | int editChoice = getValidChoice(scanner, 1, 3); 152 | 153 | if (editChoice == 1) { 154 | 155 | } else if (editChoice == 2) { 156 | 157 | 158 | String existingName = parts[1]; 159 | int existingAge = Integer.parseInt(parts[2]); 160 | String existingDistrict = parts[3]; 161 | String existingElectorate = parts[4]; 162 | 163 | System.out.print("Enter new name (or press Enter to keep existing): "); 164 | String newNameInput = scanner.nextLine(); 165 | String newName = newNameInput.isEmpty() ? existingName : newNameInput; 166 | 167 | if (newNameInput.isEmpty()) { 168 | System.out.println("Existing Name: " + existingName); 169 | } else { 170 | System.out.println("New Name: " + newName); 171 | } 172 | 173 | System.out.print("Enter new age (or press Enter to keep existing): "); 174 | String newAgeInput = scanner.nextLine(); 175 | int newAge; 176 | if (newAgeInput.isEmpty()) { 177 | newAge = existingAge; // Keep existing age 178 | System.out.println("Existing Age: " + existingAge); 179 | } else { 180 | newAge = Integer.parseInt(newAgeInput); 181 | System.out.println("New Age: " + newAge); 182 | } 183 | 184 | System.out.print("Enter new district (or press Enter to keep existing): "); 185 | String newDistrictInput = scanner.nextLine(); 186 | String newDistrict = newDistrictInput.isEmpty() ? existingDistrict : newDistrictInput; 187 | 188 | if (newDistrictInput.isEmpty()) { 189 | System.out.println("Existing District: " + existingDistrict); 190 | } else { 191 | System.out.println("New District: " + newDistrict); 192 | } 193 | 194 | System.out.print("Enter new electorate (or press Enter to keep existing): "); 195 | String newElectorateInput = scanner.nextLine(); 196 | String newElectorate = newElectorateInput.isEmpty() ? existingElectorate : newElectorateInput; 197 | 198 | if (newElectorateInput.isEmpty()) { 199 | System.out.println("Existing Electorate: " + existingElectorate); 200 | } else { 201 | System.out.println("New Electorate: " + newElectorate); 202 | } 203 | 204 | 205 | 206 | 207 | LocalDateTime editDateTime = LocalDateTime.now(); 208 | String editTimestamp = editDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); 209 | 210 | line = guid + "_" + userId + "," + newName + "," + newAge + "," + newDistrict + "," + newElectorate + "," 211 | + editTimestamp; 212 | System.out.println("Voter information updated successfully."); 213 | } else { 214 | 215 | } 216 | } 217 | writer.write(line + "\n"); 218 | } 219 | 220 | if (!voterFound) { 221 | System.out.println("Voter with the specified userId not found."); 222 | } 223 | } catch (IOException e) { 224 | e.printStackTrace(); 225 | System.err.println("An error occurred while editing the voter information."); 226 | } 227 | 228 | File tempFile = new File(tempFileName); 229 | File originalFile = new File(fileName); 230 | if (tempFile.renameTo(originalFile)) { 231 | System.out.println("File updated successfully."); 232 | } else { 233 | System.err.println("Could not rename the file."); 234 | } 235 | 236 | if (originalFile.delete() && tempFile.renameTo(originalFile)) { 237 | System.out.println("File updated successfully."); 238 | } else { 239 | System.err.println("Could not rename the file."); 240 | } 241 | } 242 | 243 | private static int getValidUserId(Scanner scanner) { 244 | while (true) { 245 | try { 246 | int userId = Integer.parseInt(scanner.nextLine()); 247 | if (userId > 0) { 248 | return userId; 249 | } else { 250 | System.out.println("Invalid userId. Please enter a positive integer."); 251 | } 252 | } catch (NumberFormatException e) { 253 | System.out.println("Invalid input. Please enter a valid userId."); 254 | } 255 | } 256 | } 257 | 258 | private static int getValidAge(String ageInput) { 259 | while (true) { 260 | try { 261 | int age = Integer.parseInt(ageInput); 262 | if (age > 0 && age >= 19) { 263 | return age; 264 | } else { 265 | System.out.println("Invalid age. Age must be a positive integer and above 18."); 266 | } 267 | } catch (NumberFormatException e) { 268 | System.out.println("Invalid input. Please enter a valid age."); 269 | } 270 | } 271 | } 272 | 273 | private static int readLastUserIdFromCSV() { 274 | int lastUserId = 0; 275 | String fileName = "registration.csv"; 276 | File file = new File(fileName); 277 | if (file.exists()) { 278 | try (Scanner fileScanner = new Scanner(file)) { 279 | while (fileScanner.hasNextLine()) { 280 | String line = fileScanner.nextLine(); 281 | String[] parts = line.split(","); 282 | String uniqueIdentifier = parts[0]; 283 | String[] idParts = uniqueIdentifier.split("_"); 284 | int userId = Integer.parseInt(idParts[1]); 285 | if (userId > lastUserId) { 286 | lastUserId = userId; 287 | } 288 | } 289 | } catch (FileNotFoundException e) { 290 | e.printStackTrace(); 291 | } 292 | } 293 | return lastUserId; 294 | } 295 | } 296 | -------------------------------------------------------------------------------- /VOTING_SYSTEM/registration.csv: -------------------------------------------------------------------------------- 1 | 4e6c4e7d-ce9d-41d5-8f27-1f62a3f91504_1,Ankit Pangeni,21,Syangja,Area 2 2 | 4394dee5-3d2d-46ac-8360-46a0d75219c2_2,ankit,21,syangja,2 3 | 1435f8a3-16b0-43ca-bceb-9a694fed6c62_3,kushal,23,syangja,2 4 | 7091728b-1efa-4542-9ee3-54f1bf36c9f9_4,bipin,23,syagja,2 5 | ef969a38-d2ca-482b-b3f7-2237e39a85aa_5,Bipin,21,Syangja,2,2023-10-04 21:49:52 6 | -------------------------------------------------------------------------------- /VOTING_SYSTEM/temp.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dyells07/Java_Sem/61f763582adddf52a0c6291daffca9e55a4671b6/VOTING_SYSTEM/temp.csv --------------------------------------------------------------------------------