diff --git a/src/main/java/billgates/database/MySQLDatabaseGateway.java b/src/main/java/billgates/database/MySQLDatabaseGateway.java index d3a3531..e1bc8f2 100644 --- a/src/main/java/billgates/database/MySQLDatabaseGateway.java +++ b/src/main/java/billgates/database/MySQLDatabaseGateway.java @@ -33,7 +33,7 @@ public MySQLDatabaseGateway() { this.columnToDatabaseColumn.put("From", "from"); this.columnToDatabaseColumn.put("To", "to"); this.columnToDatabaseColumn.put("Location", "location"); - this.columnToDatabaseColumn.put("Splitter", "splitter"); + this.columnToDatabaseColumn.put("Splitter", "split_bill_id"); this.columnToDatabaseColumn.put("Payee", "payee"); this.columnToDatabaseColumn.put("Paid Back", "paid_back"); } @@ -630,7 +630,7 @@ public void createBillTable(int billId) { Statement statement = connection.createStatement(); String query = String.format(""" - CREATE TABLE bill_%d + CREATE TABLE IF NOT EXISTS bill_%d ( entry_id INT AUTO_INCREMENT PRIMARY KEY, @@ -658,7 +658,7 @@ public void createSplitBillTable(int billId) { Statement statement = connection.createStatement(); String query = String.format(""" - CREATE TABLE bill_%d_%d + CREATE TABLE IF NOT EXISTS bill_%d_%d ( entry_id INT AUTO_INCREMENT PRIMARY KEY, @@ -699,7 +699,7 @@ public void createUsersTable() { String query = """ - CREATE TABLE users + CREATE TABLE IF NOT EXISTS users ( user_id INT AUTO_INCREMENT PRIMARY KEY, diff --git a/src/main/java/billgates/use_cases/bill_update/BillUpdateUseCase.java b/src/main/java/billgates/use_cases/bill_update/BillUpdateUseCase.java index f009b01..71e0bf5 100644 --- a/src/main/java/billgates/use_cases/bill_update/BillUpdateUseCase.java +++ b/src/main/java/billgates/use_cases/bill_update/BillUpdateUseCase.java @@ -39,11 +39,30 @@ public void updateBill(int billId) { case -2 -> billId = user.getBillId(); } user.setCurrentBillID(billId); - // get all entries of the current bill - List result = this.gateway.getBillData(user.getCurrentBillID()).getEntries() - .stream().map(d -> d.toEntryBuilder().buildEntry()).toList(); - List> list = result.stream().map(Entry::toObjects).toList(); - // if the current bill id is not the same as the bill id, then the current bill is a splitter bill - this.presenter.updateBill(new BillUpdateResponseModel(list, user.getCurrentBillID() != user.getBillId())); + // we query the database asynchronously to make the program run smoother + Thread thread = new Thread(() -> { + List result; + if (user.getBillId() != user.getCurrentBillID()) { + // if we are updating the splitter bill, then we create the splitter bill if + // not exist + this.gateway.createSplitBillTable(user.getCurrentBillID()); + // modify the split_bill_id column + this.gateway.modifyEntry(user.getBillId(), user.getCurrentBillID(), + "Splitter", String.valueOf(user.getCurrentBillID())); + // get all entries of the current bill + result = this.gateway.getSplitBillData(user.getCurrentBillID()).getEntries() + .stream().map(d -> d.toEntryBuilder().buildEntry()).toList(); + } else { + result = this.gateway.getBillData(user.getCurrentBillID()).getEntries() + .stream().map(d -> d.toEntryBuilder().buildEntry()).toList(); + } + // transform all entries to lists of raw objects like int, ZonedDateTime, ... + List> list = result.stream().map(Entry::toObjects).toList(); + // if the current bill id is not the same as the bill id, + // then the current bill is a splitter bill + this.presenter.updateBill(new BillUpdateResponseModel(list, + user.getCurrentBillID() != user.getBillId())); + }); + thread.start(); } } diff --git a/src/main/java/billgates/view/gui/ActionPanel.java b/src/main/java/billgates/view/gui/ActionPanel.java index 4a9f42f..c89197b 100644 --- a/src/main/java/billgates/view/gui/ActionPanel.java +++ b/src/main/java/billgates/view/gui/ActionPanel.java @@ -120,7 +120,7 @@ private void initButtonTextArea() { this.backButton.setAlignmentX(CENTER_ALIGNMENT); this.add(Box.createRigidArea(new Dimension(0, VERTICAL_GAP))); // Back from splitting bills event - this.addEntryButton.addActionListener((e -> this.backFromSplit())); + this.backButton.addActionListener((e -> this.backFromSplit())); // backButton should be disabled at the beginning this.backButton.setEnabled(false); @@ -196,6 +196,7 @@ private void signOut() { this.signOutButton.setEnabled(false); this.addEntryButton.setEnabled(false); this.deleteEntryButton.setEnabled(false); + this.backButton.setEnabled(false); // The usernameField and passwordField should be editable after signing out this.usernameField.setEditable(true); @@ -210,7 +211,7 @@ private void signOut() { topMenuBar.getFileMenu().setEnabled(false); // Disable the billTable - BillTable billTable = (BillTable) this.mainFrame.getBillPanel().getBillTable(); + BillTable billTable = this.mainFrame.getBillPanel().getBillTable(); billTable.setEnabled(false); billTable.setVisible(false); } @@ -292,6 +293,7 @@ public void view(UserJoinViewModel viewModel) { this.signInButton.setEnabled(false); this.signOutButton.setEnabled(true); this.addEntryButton.setEnabled(true); + this.backButton.setEnabled(true); // the usernameField and passwordField shouldn't be editable this.usernameField.setEditable(false); diff --git a/src/main/java/billgates/view/gui/BillPanel.java b/src/main/java/billgates/view/gui/BillPanel.java index 3004d53..9e2b56a 100644 --- a/src/main/java/billgates/view/gui/BillPanel.java +++ b/src/main/java/billgates/view/gui/BillPanel.java @@ -80,12 +80,15 @@ public void changeFont(String f){ @Override public void update(BillUpdateViewModel viewModel) { - String[] columns = viewModel.getColumns(); - List> entries = viewModel.getEntries(); - BillTableModel model = this.getBillTable().getModel(); - model.setColumnNames(columns); - model.setData(entries); - this.getBillTable().updateUI(); + // we use invoke later here because this method is called from other threads. + SwingUtilities.invokeLater(() -> { + String[] columns = viewModel.getColumns(); + List> entries = viewModel.getEntries(); + BillTableModel model = this.getBillTable().getModel(); + model.setColumnNames(columns); + model.setData(entries); + this.getBillTable().updateUI(); + }); } /** @@ -105,7 +108,6 @@ public void mouseClicked(MouseEvent e) { @Override public void mousePressed(MouseEvent e) { if (e.getClickCount() == 2) { - System.out.println(2); // trigger to splitter bill Point point = new Point(e.getX(), e.getY()); int row = BillPanel.this.billTable.rowAtPoint(point); @@ -113,19 +115,13 @@ public void mousePressed(MouseEvent e) { if (row == -1 || column == -1) return; String name = BillPanel.this.billTable.getColumnName(column); - if ("Splitter".equals(name)) + if (!"Splitter".equals(name)) return; - String splitter = (String) BillPanel.this.billTable.getModel().getValueAt(row, column); - if ("No".equals(splitter)) { - // TODO: call create splitter bill use case - } // get the entry id int entryId = (int) BillPanel.this.getBillTable().getModel().getValueAt(row, 0); - // for debugging TODO: delete it - System.out.println(entryId); // call the bill update use case on the entryId - // TODO: uncomment the line below. I commented because I don't have create splitter use case now. - // SwingUtilities.invokeLater(() -> this.mainFrame.getBillUpdateController().update(entryId)); + SwingUtilities.invokeLater(() -> + BillPanel.this.mainFrame.getBillUpdateController().update(entryId)); } } }