Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions FirstGroupAssignment
Original file line number Diff line number Diff line change
@@ -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);
});
}
}