From 124a8f8b0c4ab6a43bc0b0c384b85068dd973d06 Mon Sep 17 00:00:00 2001 From: ByizaBryan <145932306+ByizaBryan@users.noreply.github.com> Date: Wed, 13 Nov 2024 22:10:19 +0200 Subject: [PATCH] Create FirstGroupAssignment 223006749&&223021381 --- FirstGroupAssignment | 181 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 FirstGroupAssignment diff --git a/FirstGroupAssignment b/FirstGroupAssignment new file mode 100644 index 0000000..82d9421 --- /dev/null +++ b/FirstGroupAssignment @@ -0,0 +1,181 @@ +/* + +Q1 + + +/* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + */ + +package firstquestion.loan; + +/** + * + * @author Bryan + */ +public class Loan { + double monthlyPayment; + int numYearsforpayment; + double totalAmountforpayment; + public Loan(double monthlyPayment, int numYearsforpayment, double totalAmount) { + this.monthlyPayment = monthlyPayment; + this.numYearsforpayment = numYearsforpayment; + this.totalAmountforpayment = totalAmountforpayment; + } + public void settingLoanDetails(double monthlyPayment, int numYearsforpayment, double totalAmount) { + this.monthlyPayment = monthlyPayment; + this.numYearsforpayment = numYearsforpayment; + this.totalAmountforpayment = totalAmountforpayment; + } + public void calculateTotalAmount() { + this.totalAmountforpayment = this.monthlyPayment * this.numYearsforpayment * 12; + } + public double getMonthlyPayment() { + return monthlyPayment; + } + + public int getNumYears() { + return numYearsforpayment; + } + + public double getTotalAmount() { + return totalAmountforpayment; + } + + public static void main(String[] args) { + Loan myLoan = new Loan(5000, 5, 0); + myLoan.calculateTotalAmount(); + System.out.println("Total amount to be payable in five years: RWF " + myLoan.getTotalAmount()); + } +} + + + +/* + +Q2 + + +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + */ + +package firstquestion.studentgui; + +/** + * + * @author Bryan + */ +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.sql.*; + +public class StudentGUI extends JFrame { + JTextField REGnoField, nameField, ageField, emailField, departmentField; + JButton addButton, searchButton; + + + private final String DB_URL = "jdbc:mysql://localhost:3306/student_db"; + private final String USER = "root"; + private final String PASS = ""; + + public StudentGUI() { + + setTitle("Student Database"); + setSize(400, 300); + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + setLayout(new GridLayout(6, 2)); + + + REGnoField = new JTextField(); + nameField = new JTextField(); + ageField = new JTextField(); + emailField = new JTextField(); + departmentField = new JTextField(); + addButton = new JButton("Add Student"); + searchButton = new JButton("Search by REGno"); + + + add(new JLabel("REGno:")); + add(REGnoField); + add(new JLabel("Names:")); + add(nameField); + + add(new JLabel("Age:")); + add(ageField); + add(new JLabel("Email:")); + add(emailField); + add(new JLabel("Department:")); + add(departmentField); + add(addButton); + add(searchButton); + + + addButton.addActionListener(new AddButtonListener()); + searchButton.addActionListener(new SearchButtonListener()); + } + + + private class AddButtonListener implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + int REGno = Integer.parseInt(REGnoField.getText()); + String names = nameField.getText(); + int age = Integer.parseInt(ageField.getText()); + String email = emailField.getText(); + String department = departmentField.getText(); + + try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); + PreparedStatement stmt = conn.prepareStatement("INSERT INTO students (REGno, Names, Age, Email, Department) VALUES (?, ?, ?, ?,?)")) { + stmt.setInt(1, REGno); + stmt.setString(2, names); + stmt.setInt(3, age); + stmt.setString(4, email); + stmt.setString(4, department); + stmt.executeUpdate(); + JOptionPane.showMessageDialog(null, "Student added successfully!"); + } catch (SQLException ex) { + ex.printStackTrace(); + JOptionPane.showMessageDialog(null, "Error adding student to database."); + } + } + } + + + private class SearchButtonListener implements ActionListener { + @Override + public void actionPerformed(ActionEvent e) { + int REGno= Integer.parseInt(REGnoField.getText()); + + try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); + PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students WHERE id = ?")) { + stmt.setInt(1, REGno); + ResultSet rs = stmt.executeQuery(); + + if (rs.next()) { + nameField.setText(rs.getString("name")); + ageField.setText(String.valueOf(rs.getInt("age"))); + emailField.setText(rs.getString("email")); + departmentField.setText(rs.getString("department")); + } else { + JOptionPane.showMessageDialog(null, "Student not found."); + } + } catch (SQLException ex) { + ex.printStackTrace(); + JOptionPane.showMessageDialog(null, "Error retrieving student data."); + } + } + } + + public static void main(String[] args) { + SwingUtilities.invokeLater(() -> { + StudentGUI gui = new StudentGUI(); + gui.setVisible(true); + }); + } +} + + + +