Encoder decoder application by debugger#12
Encoder decoder application by debugger#12Shubham25104 wants to merge 10 commits intoTech-Bizz-Devs:mainfrom
Conversation
Tech-Bizz-Devs#8 Basic text editor
Tech-Bizz-Devs#1 Encoder Decoder Application
WalkthroughThis update introduces a new Java Swing application, Changes
Poem
Tip ⚡💬 Agentic Chat (Pro Plan, General Availability)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 4
🔭 Outside diff range comments (1)
readme.md (1)
1-44: 🛠️ Refactor suggestionAdd missing documentation about building with Gradle
The README mentions manual compilation but doesn't include instructions about building with Gradle, which is supported by the repository according to the workflow file.
Add a new section after the "How to Run" section:
## How to Run 1. Ensure you have Java installed on your PC. If not, download and install Java from [here](https://www.java.com/en/download/). 2. Download the `editor.java` file from this repository. 3. Open Command Prompt (on Windows) or Terminal (on macOS/Linux). 4. Navigate to the directory where the `editor.java` file is located. 5. Compile the Java source file using the following command: ```bash javac EncoderDecoder.java ``` 6. Run the compiled Java program using the following command: ```bash java EncoderDecoder ``` + +## Building with Gradle + +This project supports building with Gradle: + +1. Ensure you have JDK 17 or later installed +2. Clone the repository +3. Navigate to the project directory +4. Run: + ```bash + ./gradlew build + ``` +5. To run the application: + ```bash + ./gradlew run + ``` + ## Usage
🧹 Nitpick comments (5)
.github/workflows/gradle-publish.yml (1)
1-44: Good implementation of GitHub Actions workflow for Gradle publishingThe workflow is well structured and follows best practices by:
- Triggering only on release creation
- Setting appropriate permissions
- Using a specific commit hash for the Gradle action (security best practice)
- Configuring JDK 17 with the Temurin distribution
Consider adding these improvements for better reliability and performance:
- Add a verification step to ensure the build is successful before publishing
- Implement caching for Gradle dependencies
name: Gradle Package on: release: types: [created] jobs: build: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v4 - name: Set up JDK 17 uses: actions/setup-java@v4 with: java-version: '17' distribution: 'temurin' server-id: github # Value of the distributionManagement/repository/id field of the pom.xml settings-path: ${{ github.workspace }} # location for the settings.xml file - name: Setup Gradle uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 + + - name: Cache Gradle packages + uses: actions/cache@v3 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- - name: Build with Gradle run: ./gradlew build + - name: Verify build success + run: | + if [ ! -d "build" ]; then + echo "Build directory not found, build may have failed" + exit 1 + fi # The USERNAME and TOKEN need to correspond to the credentials environment variables used in # the publishing section of your build.gradle - name: Publish to GitHub Packages run: ./gradlew publish env: USERNAME: ${{ github.actor }} TOKEN: ${{ secrets.GITHUB_TOKEN }}readme.md (1)
39-43: Expand or remove the empty Notes sectionThe Notes section contains only a single bullet point about Java Swing, with empty lines around it. Consider either expanding this section with more useful information or removing it if unnecessary.
## Notes - - - The application uses Java Swing for the graphical user interface. +- The Caesar Cipher uses a fixed shift of 3 characters +- Error handling is implemented for invalid Base64 decoding attemptsEncoderDecoder.java (3)
11-61: Improve the GUI layout for better usabilityThe current layout places the algorithm selection panel on the west side of the frame, which is not intuitive. Consider reorganizing the components for better user experience.
public EncoderDecoder() { setTitle("Encoder Decoder"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 300); setLocationRelativeTo(null); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BorderLayout()); + + // Create a panel for algorithm selection at the top + JPanel controlPanel = new JPanel(); + controlPanel.setLayout(new FlowLayout()); + + algorithmComboBox = new JComboBox<>(); + algorithmComboBox.addItem("Caesar Cipher"); + algorithmComboBox.addItem("Base64 Encoding"); + // Add more encoding algorithms as needed + controlPanel.add(new JLabel("Select Algorithm:")); + controlPanel.add(algorithmComboBox); inputTextArea = new JTextArea(5, 20); JScrollPane inputScrollPane = new JScrollPane(inputTextArea); outputTextArea = new JTextArea(5, 20); JScrollPane outputScrollPane = new JScrollPane(outputTextArea); outputTextArea.setEditable(false); JPanel inputPanel = new JPanel(); inputPanel.setLayout(new GridLayout(1, 2)); inputPanel.add(new JLabel("Input:")); inputPanel.add(inputScrollPane); JPanel outputPanel = new JPanel(); outputPanel.setLayout(new GridLayout(1, 2)); outputPanel.add(new JLabel("Output:")); outputPanel.add(outputScrollPane); JPanel buttonPanel = new JPanel(); encodeButton = new JButton("Encode"); decodeButton = new JButton("Decode"); + JButton clearButton = new JButton("Clear"); buttonPanel.add(encodeButton); buttonPanel.add(decodeButton); + buttonPanel.add(clearButton); - JPanel algorithmPanel = new JPanel(); - algorithmComboBox = new JComboBox<>(); - algorithmComboBox.addItem("Caesar Cipher"); - algorithmComboBox.addItem("Base64 Encoding"); - // Add more encoding algorithms as needed - algorithmPanel.add(new JLabel("Select Algorithm:")); - algorithmPanel.add(algorithmComboBox); - + JPanel contentPanel = new JPanel(new BorderLayout()); + contentPanel.add(inputPanel, BorderLayout.NORTH); + contentPanel.add(outputPanel, BorderLayout.CENTER); + - mainPanel.add(inputPanel, BorderLayout.NORTH); - mainPanel.add(outputPanel, BorderLayout.CENTER); + mainPanel.add(controlPanel, BorderLayout.NORTH); + mainPanel.add(contentPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); - mainPanel.add(algorithmPanel, BorderLayout.WEST); encodeButton.addActionListener(this); decodeButton.addActionListener(this); + clearButton.addActionListener(e -> { + inputTextArea.setText(""); + outputTextArea.setText(""); + }); add(mainPanel); setVisible(true); }
130-135: Add similar error handling for Base64 encodingYou've implemented good error handling for Base64 decoding, but there's no equivalent error handling for encoding. While Base64 encoding rarely fails, it could fail if there are issues with the encoding of the input string.
} else if (algorithm.equals("Base64 Encoding")) { - encodedText.append(Base64.getEncoder().encodeToString(input.getBytes())); + try { + encodedText.append(Base64.getEncoder().encodeToString(input.getBytes())); + } catch (Exception e) { + encodedText.append("Error encoding to Base64: " + e.getMessage()); + } } else {
81-153: Extract common character manipulation into a reusable methodBoth encode and decode methods contain similar code for manipulating characters based on whether they are letters. Consider extracting this functionality into a reusable method.
Create a helper method to process characters:
private char processChar(char c, int shift, boolean isEncode) { if (!Character.isLetter(c)) { return c; } char base = Character.isUpperCase(c) ? 'A' : 'a'; int direction = isEncode ? 1 : -1; return (char) (((c - base + direction * shift + 26) % 26) + base); }Then simplify your encode and decode methods to use this helper:
private String encode(String input, String algorithm) { StringBuilder encodedText = new StringBuilder(); if (algorithm.equals("Caesar Cipher")) { int shift = 3; // Define the shift value for the Caesar Cipher - for (char c : input.toCharArray()) { - if (Character.isLetter(c)) { - char base = Character.isUpperCase(c) ? 'A' : 'a'; - encodedText.append((char) (((c - base + shift) % 26) + base)); - } else { - encodedText.append(c); - } + for (char c : input.toCharArray()) { + encodedText.append(processChar(c, shift, true)); } } else if (algorithm.equals("Base64 Encoding")) { // ...And similarly for the decode method.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
.github/workflows/gradle-publish.yml(1 hunks)EncoderDecoder.java(1 hunks)README.md(0 hunks)readme.md(1 hunks)
💤 Files with no reviewable changes (1)
- README.md
🔇 Additional comments (2)
EncoderDecoder.java (2)
129-136: Good error handling for Base64 decodingThe implementation properly catches IllegalArgumentException when decoding invalid Base64 input and provides a helpful error message to the user.
156-158: Good use of SwingUtilities.invokeLaterProperly using SwingUtilities.invokeLater to ensure the UI is created on the Event Dispatch Thread is a good practice for Swing applications.
| ## How to Run | ||
|
|
||
| 1. Ensure you have Java installed on your PC. If not, download and install Java from [here](https://www.java.com/en/download/). | ||
| 2. Download the `editor.java` file from this repository. |
There was a problem hiding this comment.
Fix incorrect filename in download instructions
The instruction mentions editor.java but the actual file name is EncoderDecoder.java as seen in the implementation.
-2. Download the `editor.java` file from this repository.
+2. Download the `EncoderDecoder.java` file from this repository.📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| 2. Download the `editor.java` file from this repository. | |
| 2. Download the `EncoderDecoder.java` file from this repository. |
| if (algorithm.equals("Caesar Cipher")) { | ||
| int shift = 3; // Define the shift value for the Caesar Cipher (you can make it dynamic if needed) | ||
| for (char c : input.toCharArray()) { | ||
| if (Character.isLetter(c)) { | ||
| char base = Character.isUpperCase(c) ? 'A' : 'a'; | ||
| encodedText.append((char) (((c - base + shift) % 26) + base)); | ||
| } else { | ||
| encodedText.append(c); | ||
| } | ||
| } | ||
| } else if (algorithm.equals("Base64 Encoding")) { | ||
| encodedText.append(Base64.getEncoder().encodeToString(input.getBytes())); | ||
| } else { |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Make Caesar Cipher shift value configurable
The shift value for Caesar Cipher is hardcoded to 3, which limits the usefulness of this cipher algorithm. Consider making this value configurable through the UI.
Add a spinner or text field for the user to select the shift value:
if (algorithm.equals("Caesar Cipher")) {
- int shift = 3; // Define the shift value for the Caesar Cipher (you can make it dynamic if needed)
+ int shift = getCaesarShiftValue(); // Get the shift value from the UI
for (char c : input.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
encodedText.append((char) (((c - base + shift) % 26) + base));
} else {
encodedText.append(c);
}
}
} else if (algorithm.equals("Base64 Encoding")) {And add a method to get the shift value:
private int getCaesarShiftValue() {
// If you've added a spinner or text field for the shift value, get its value here
// For now, return the default value
return 3;
}| // Handle custom encoding algorithms | ||
| // For example, let's say you have a custom substitution cipher where 'A' maps to 'X', 'B' maps to 'Y', etc. | ||
| StringBuilder customEncodedText = new StringBuilder(); | ||
| for (char c : input.toCharArray()) { | ||
| if (Character.isLetter(c)) { | ||
| char base = Character.isUpperCase(c) ? 'A' : 'a'; | ||
| char encodedChar = (char) (((c - base + 3) % 26) + base); // This is just an example custom encoding logic | ||
| customEncodedText.append(encodedChar); | ||
| } else { | ||
| customEncodedText.append(c); | ||
| } | ||
| } | ||
| encodedText.append(customEncodedText); | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Remove redundant custom encoding implementation
The "custom" encoding implementation is identical to the Caesar Cipher with a shift of 3. This creates code duplication and confusion. Either remove this code or implement a truly different custom algorithm.
- } else {
- // Handle custom encoding algorithms
- // For example, let's say you have a custom substitution cipher where 'A' maps to 'X', 'B' maps to 'Y', etc.
- StringBuilder customEncodedText = new StringBuilder();
- for (char c : input.toCharArray()) {
- if (Character.isLetter(c)) {
- char base = Character.isUpperCase(c) ? 'A' : 'a';
- char encodedChar = (char) (((c - base + 3) % 26) + base); // This is just an example custom encoding logic
- customEncodedText.append(encodedChar);
- } else {
- customEncodedText.append(c);
- }
- }
- encodedText.append(customEncodedText);
+ } else {
+ // Handle any other algorithms that might be added in the future
+ encodedText.append("Unsupported algorithm: " + algorithm);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Handle custom encoding algorithms | |
| // For example, let's say you have a custom substitution cipher where 'A' maps to 'X', 'B' maps to 'Y', etc. | |
| StringBuilder customEncodedText = new StringBuilder(); | |
| for (char c : input.toCharArray()) { | |
| if (Character.isLetter(c)) { | |
| char base = Character.isUpperCase(c) ? 'A' : 'a'; | |
| char encodedChar = (char) (((c - base + 3) % 26) + base); // This is just an example custom encoding logic | |
| customEncodedText.append(encodedChar); | |
| } else { | |
| customEncodedText.append(c); | |
| } | |
| } | |
| encodedText.append(customEncodedText); | |
| } | |
| } else { | |
| // Handle any other algorithms that might be added in the future | |
| encodedText.append("Unsupported algorithm: " + algorithm); | |
| } |
| // Handle custom decoding algorithms | ||
| // For example, let's say you have a custom substitution cipher where 'X' maps to 'A', 'Y' maps to 'B', etc. | ||
| StringBuilder customDecodedText = new StringBuilder(); | ||
| for (char c : input.toCharArray()) { | ||
| if (Character.isLetter(c)) { | ||
| char base = Character.isUpperCase(c) ? 'A' : 'a'; | ||
| char decodedChar = (char) (((c - base - 3 + 26) % 26) + base); // This is just an example custom decoding logic | ||
| customDecodedText.append(decodedChar); | ||
| } else { | ||
| customDecodedText.append(c); | ||
| } | ||
| } | ||
| decodedText.append(customDecodedText); | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Remove redundant custom decoding implementation
Similar to the encoding method, the "custom" decoding implementation is identical to the Caesar Cipher with a shift of 3. This creates code duplication and should be removed or replaced with a truly different algorithm.
- } else {
- // Handle custom decoding algorithms
- // For example, let's say you have a custom substitution cipher where 'X' maps to 'A', 'Y' maps to 'B', etc.
- StringBuilder customDecodedText = new StringBuilder();
- for (char c : input.toCharArray()) {
- if (Character.isLetter(c)) {
- char base = Character.isUpperCase(c) ? 'A' : 'a';
- char decodedChar = (char) (((c - base - 3 + 26) % 26) + base); // This is just an example custom decoding logic
- customDecodedText.append(decodedChar);
- } else {
- customDecodedText.append(c);
- }
- }
- decodedText.append(customDecodedText);
+ } else {
+ // Handle any other algorithms that might be added in the future
+ decodedText.append("Unsupported algorithm: " + algorithm);📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Handle custom decoding algorithms | |
| // For example, let's say you have a custom substitution cipher where 'X' maps to 'A', 'Y' maps to 'B', etc. | |
| StringBuilder customDecodedText = new StringBuilder(); | |
| for (char c : input.toCharArray()) { | |
| if (Character.isLetter(c)) { | |
| char base = Character.isUpperCase(c) ? 'A' : 'a'; | |
| char decodedChar = (char) (((c - base - 3 + 26) % 26) + base); // This is just an example custom decoding logic | |
| customDecodedText.append(decodedChar); | |
| } else { | |
| customDecodedText.append(c); | |
| } | |
| } | |
| decodedText.append(customDecodedText); | |
| } | |
| } else { | |
| // Handle any other algorithms that might be added in the future | |
| decodedText.append("Unsupported algorithm: " + algorithm); | |
| } |
#1 Encoder decoder application
Summary by CodeRabbit