-
Notifications
You must be signed in to change notification settings - Fork 0
Featuresbranch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
32887e3
9fcb2de
61069dd
22eb132
649e78a
5fec424
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||||||||||||
|
|
@@ -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); | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rename the variable to
Suggested change
|
||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| 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 (?, ?, ?)"; | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||||||||||||
| } | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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. |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| CustomerServices cservices = new CustomerServices(); | ||
|
|
||
| //drivermanager.addCustomerRecord(name, money, password); | ||
|
|
||
| cservices.makeTransfer("derry", "jennifer", 2000); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a perfect class to replace with a You set the data once when it instantiates, and it becomes permanently immutable. |
||
|
|
||
| public Customer(int i, String ss, int i1, String password) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
||
| 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 |
This file was deleted.
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be careful with leaving potentially unused classes. |
||
| 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 |
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.
Uh oh!
There was an error while loading. Please reload this page.