Skip to content
Draft
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.example.Controller;

public class TransactionController{
public class TransferCalculator {

public int receiveMoneyTransaction( int receiverCustomerAmount, int transferAmount){
return receiverCustomerAmount + transferAmount;
Expand Down
139 changes: 139 additions & 0 deletions src/main/java/org/example/DBController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.example;
import org.example.Model.Customer;
import org.example.Model.TransactionAudit;

import java.sql.*;
import java.sql.DriverManager;
Expand Down Expand Up @@ -128,6 +129,144 @@ public boolean updateCustomerFunds(int customerId, int newAmount) { //void
return false;
}

public void createTransactionAudit(int senderId, String senderName, int moneyInCents, int getterId, String getterName) throws SQLException {
String sql = "INSERT INTO transactions_audit (sender_id, sender_nickname, amount_sent, receiver_id, receiver_nickname) VALUES (?, ?, ?, ?, ?)";

PreparedStatement preparedStatement = connection.prepareStatement(
sql,
Statement.RETURN_GENERATED_KEYS
);
{

ps.setInt(1, senderId);
ps.setString(2, senderName);
ps.setInt(3, moneyInCents);
ps.setInt(4, getterId);
ps.setString(5, getterName);

int rows = ps.executeUpdate(); // returns 1
System.out.println("Rows inserted: " + rows);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename the variable to rowsInserted just so it is a little clearer. rows implies an entire row data structure with columns. rowsInserted implies just a number.

Suggested change
System.out.println("Rows inserted: " + rows);
System.out.println("Rows inserted: " + rowsInserted);

}
}

public TransactionAudit returnTransactionAudit(int transactionid) throws SQLException {

int id = transactionid;
int sender_id = 0;
String sender_nickname = null;
int amount_sent = 0;
int receiver_id = 0;
String receiver_nickname = null;
String time = null;

String sql = "SELECT id, sender_id, sender_nickname, amount_sent, receiver_id, receiver_nickname FROM transactions_audit WHERE id = ?";
// + customerId + " (id, nickname, money, password) " + "VALUES (?, ?, ?)";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid these kinds of comments. Generally speaking comments are a hot topic and I've seen both a lot of good and bad comments. The best kind of comments are no comments where the code is self-documenting. But if you believe you really need to add a comment read https://alexkondov.com/comments-the-good-the-bad-the-ugly and make your own conclusion. There is no hard rule here.


try (
PreparedStatement ps = connection.prepareStatement(sql)) {
ps.setLong(1, id);

try (ResultSet rs = ps.executeQuery()) {
Comment on lines +165 to +169
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't nest try blocks. Let one trycatch exist and handle the cases there.


while (rs.next()) {
id = rs.getInt("id");
sender_id = rs.getInt("sender_id");
sender_nickname = rs.getString("sender_nickname");
amount_sent = rs.getInt("amount_sent");
receiver_id = rs.getInt("receiver_id");
receiver_nickname = rs.getString("receiver_nickname");
time = rs.getString("time");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can return this data immediately here. This would remove the need to instantiate these variables at the beginning of this function:

Suggested change
}
return new TransactionAudit(id, sender_id, sender_nickname, amount_sent, receiver_id, receiver_nickname, time);
}

And you then can just throw an error at the end if something went wrong and there is nothing to return.

}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
Comment on lines +181 to +183
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useful for debugging, but I would advise not put these when writing production-ready code. You don't want your end-users see the bug message.

return new TransactionAudit(id, sender_id, sender_nickname, amount_sent, receiver_id, receiver_nickname, time);
}

public void insertIntoFriendships(int senderId, int receiverId) throws SQLException {
String sql = "INSERT INTO friendships (customer_id, friend_id) VALUES (?, ?)";

PreparedStatement ps = connection.prepareStatement(sql);
ps.setLong(1, senderId); // customer_id
ps.setLong(2, receiverId); // friend_id
Comment on lines +191 to +192
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either remove the comments or make the variables more meaningful:

Suggested change
ps.setLong(1, senderId); // customer_id
ps.setLong(2, receiverId); // friend_id
ps.setLong(1, customerId);
ps.setLong(2, friendId);

ps.executeUpdate();
}

public boolean checkIfHasFriend(int senderId, int receiverId) throws SQLException {
String sql = "SELECT FROM friendships WHERE customer_id = ? AND friend_id = ?";
PreparedStatement ps = connection.prepareStatement(sql);

ps.setLong(1, senderId);
ps.setLong(2, receiverId);

ResultSet rs = ps.executeQuery();
boolean result = rs.next();

if (!result) {
ps.setLong(1, receiverId);
ps.setLong(2, senderId);

rs = ps.executeQuery();
return rs.next();
}
return true;
}

public void insertIntoTransfers(int senderId, int receiverId, int amount) throws SQLException {

PreparedStatement addTx = connection.prepareStatement("INSERT INTO transfers (sender_id, receiver_id, amount_cents) VALUES (?,?,?)");
addTx.setLong(1, senderId);
addTx.setLong(2, receiverId);
addTx.setLong(3, amount);
addTx.executeUpdate();
Comment on lines +219 to +222
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fluent setter here if possible.

}

public void insertIntoTransferTotals1(int senderId, int receiverId, int amount) throws SQLException {
PreparedStatement upsert1 = connection.prepareStatement(
"""
INSERT INTO transfer_totals (customer_id, friend_id, sent_cents)
VALUES (?, ?, ?)
ON CONFLICT (customer_id, friend_id)
DO UPDATE SET sent_cents = transfer_totals.sent_cents + EXCLUDED.sent_cents
""");
upsert1.setLong(1, senderId);
upsert1.setLong(2, receiverId);
upsert1.setLong(3, amount);
upsert1.executeUpdate();
Comment on lines +225 to +236
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird variable naming. Why the 1 here?

}

public void insertIntoTransferTotals2(int senderId, int receiverId, int amount) throws SQLException {
PreparedStatement upsert2 = connection.prepareStatement(
"""
INSERT INTO transfer_totals (customer_id, friend_id, received_cents)
VALUES (?, ?, ?)
ON CONFLICT (customer_id, friend_id)
DO UPDATE SET received_cents = transfer_totals.received_cents + EXCLUDED.received_cents
""");
upsert2.setLong(1, receiverId);
upsert2.setLong(2, senderId);
upsert2.setLong(3, amount);
upsert2.executeUpdate();
Comment on lines +239 to +250
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird variable naming. Would prefer to change it to something simpler. Why the 2?

}

public void createTable() throws SQLException {
String sql = """
CREATE TABLE IF NOT EXISTS test (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
friend_name TEXT NOT NULL,
friend_status TEXT NOT NULL,
total_money_sent BIGINT NOT NULL,
total_money_received BIGINT NOT NULL,
since_when TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
""";

Statement st = connection.createStatement();
st.executeUpdate(sql);

}

Comment on lines +253 to +269
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is OK for this project, but this is also a good pivot for learning about database migrations.

See https://medium.com/@AlexanderObregon/best-practices-for-database-migrations-in-java-for-beginners-cbe42e50cc76.

You don't need to add it to this project, but you should learn about it once you got time. Same concept as here, only with ordered migrations + a reliable package does table schema changes for you.

}


18 changes: 15 additions & 3 deletions src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
package org.example;

import org.example.Model.Customer;
import org.example.Model.TransactionAudit;
import org.example.Services.CustomerServices;
import org.example.Services.ThreadTest;
import org.example.Services.TransferHandler;

import java.io.IOException;
import java.sql.*;

public class Main {

public static void main(String[] args) throws ClassNotFoundException, SQLException {
public static void main(String[] args) throws SQLException, IOException {

// DBController dbcontrol = new DBController();
//
// ThreadTest R1 = new ThreadTest( "Thread-1");
// R1.start();
//
// ThreadTest R2 = new ThreadTest( "Thread-2");
// R2.start();
Comment on lines +16 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful with comments like these. This is dead code which may stay dead forever. Either commit it uncommented or remove it, or leave a TODO at the top regarding what to do with it.


CustomerServices cservices = new CustomerServices();

//drivermanager.addCustomerRecord(name, money, password);

cservices.makeTransfer("derry", "jennifer", 2000);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/example/Model/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class Customer{
private String user;
private int moneyInCents;
private String password;
private String realName;
private String email;
private String realName;
private String email;
Comment on lines 6 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a perfect class to replace with a record. See https://www.baeldung.com/java-record-keyword.

You set the data once when it instantiates, and it becomes permanently immutable.


public Customer(int i, String ss, int i1, String password) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should consider changing these parameters to be identical to their field names.

this.Id = i;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/example/Model/TransactionAudit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.example.Model;

public record TransactionAudit(int id, int senderId, String senderName, int moneyInCents, int getterId, String getterName, String time) {


}

// money in cents
8 changes: 0 additions & 8 deletions src/main/java/org/example/Model/TransactionRecords.java

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/java/org/example/Other/DataAutomation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.Other;

public class DataAutomation {


}
Comment on lines +1 to +6
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be careful with leaving potentially unused classes.

121 changes: 121 additions & 0 deletions src/main/java/org/example/Other/nicknames.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
Valentino
Keshawn
Gunner
Jenny
Adonis
Andre
Eddie
Heidi
Rebecca
John
Philip
Kadin
Saige Fuentes.
Bowen Higgins.
Leighton Kramer.
Kylan Gentry.
Amelie Griffith.
Franklin Sierra.
Marceline Avila.
Jaylen Blackwell.

Kaisley Webster
Shawn Wilson
Luna Farley
Graysen Bradley
Vanessa Nixon
Cory Valencia
Maddison Lawson
Lane Harrell
Kara Velez
Kareem Robinson
Nora Bradley
Richard Patrick
Lyra Hurst
Neil Lee
Scarlett Reid
Josue McCall
Kai Martin
Mateo Velazquez
Jaliyah Curtis
Muhammad Parker
Aubrey Castillo
Kai Weeks
Karen Sloan
Ocean Allison
Chelsea Gallagher
Marcos Palacios
Bria Rojas
Colin Tucker
Esther Peterson
Santiago Payne
London Bass
Landen Shannon
Harlee Rubio
Titan Peck
Crystal Potter
Lucca Schneider
Delaney Martin
Mateo Portillo
Nathalie Richards
Holden Weiss
Lennox Vaughan
Castiel Patton
Lorelei Lim
Cal Kirk
Ellis Jefferson
Raylan Avila
Amiyah Marshall
Kaiden Henson
Kinslee Lynch
Zane Silva
Lucia Quinn
Rhys Townsend
Azalea Cano
Terry Cook
Aaliyah Noble
Idris Higgins
Leighton Velez
Kareem Terrell
Paityn Villa
Clay Skinner
Mara Cisneros
Alden Leach
Martha Chase
Otis Frederick
Sariyah Zhang
Isaias Brewer
Thea Farrell
Ty Vo
Artemis Saunders
Kasen Moyer
Zola Walton
Dominick Novak
Kaiya May
Finley McLean
Sky Ali
Arjun Gilbert
Jocelyn Daniels
Xander Monroe
Carly Calhoun
Gary Horton
Aitana Hickman
Jakobe Snyder
Callie Humphrey
Krew Chandler
Viviana Saunders
Kasen Pace
Giana Crawford
Kevin Harper
Ana Rivera
Charles Zuniga
Leslie McDowell
Lachlan Quinn
Heaven Armstrong
Grant Bryant
Parker Price
Brooks Frazier
Octavia Walter
Lochlan Atkins
Mina Zavala
Dillon Petersen
13 changes: 0 additions & 13 deletions src/main/java/org/example/Output_files/allcustomers.csv

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading