From f21c528a8a1faa4519746d3de06f46a6c7a3945b Mon Sep 17 00:00:00 2001 From: Xi Xu Date: Wed, 3 Jul 2024 03:02:39 +0800 Subject: [PATCH] A --- init.sql | 4 +-- src/client/Client.java | 21 ++++++++-------- src/server/tools/crawling/Book.java | 30 ++++++++++++----------- src/server/tools/crawling/NewsThread.java | 14 +++++------ 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/init.sql b/init.sql index 97afaa5..159df45 100644 --- a/init.sql +++ b/init.sql @@ -15,8 +15,8 @@ CREATE TABLE IF NOT EXISTS title VARCHAR(255) NOT NULL PRIMARY KEY, author VARCHAR(255) NOT NULL, publisher VARCHAR(255), - oldprice DECIMAL(10, 2), - newprice DECIMAL(10, 2), + originalprice DECIMAL(10, 2), + discountedprice DECIMAL(10, 2), href VARCHAR(255) ); diff --git a/src/client/Client.java b/src/client/Client.java index e77e2d9..58abaaa 100644 --- a/src/client/Client.java +++ b/src/client/Client.java @@ -215,7 +215,7 @@ public class Client { System.out.println("Please enter the publisher:"); break; case 4: - sql = "SELECT * FROM book WHERE oldprice = ?"; + sql = "SELECT * FROM book WHERE originalprice = ?"; System.out.println("Please enter the price:"); break; case 5: @@ -233,11 +233,12 @@ public class Client { String title = rs.getString(1); String author = rs.getString(2); String pubilsher = rs.getString(3); - String oldprice = rs.getString(4); - String newprice = rs.getString(5); + String originalprice = rs.getString(4); + String discountedprice = rs.getString(5); String url = rs.getString(6); System.out.println("Title: " + title + ", Author: " + author + ", Publisher: " + pubilsher - + ", Original Price: " + oldprice + ", Discounted Price: " + newprice + ", URL: " + url); + + ", Original Price: " + originalprice + ", Discounted Price: " + discountedprice + + ", URL: " + url); } System.out.println("Querying ended!"); } @@ -321,14 +322,14 @@ public class Client { private static void updatePrice(String title) throws Exception { System.out.println("Please enter the modified price:"); - String newPrice = scan.nextLine(); - String sqlUpdateOldPrice = "UPDATE book SET oldprice = newprice WHERE title = ?"; - ps = con.prepareStatement(sqlUpdateOldPrice); + String discountedPrice = scan.nextLine(); + String sqlUpdateOriginalPrice = "UPDATE book SET originalprice = discountedprice WHERE title = ?"; + ps = con.prepareStatement(sqlUpdateOriginalPrice); ps.setString(1, title); ps.executeUpdate(); - String sqlUpdateNewPrice = "UPDATE book SET newprice = ? WHERE title = ?"; - ps = con.prepareStatement(sqlUpdateNewPrice); - ps.setString(1, newPrice); + String sqlUpdateDiscountedPrice = "UPDATE book SET discountedprice = ? WHERE title = ?"; + ps = con.prepareStatement(sqlUpdateDiscountedPrice); + ps.setString(1, discountedPrice); ps.setString(2, title); int affectedRows = ps.executeUpdate(); if (affectedRows > 0) { diff --git a/src/server/tools/crawling/Book.java b/src/server/tools/crawling/Book.java index 9d98864..56e5f5f 100644 --- a/src/server/tools/crawling/Book.java +++ b/src/server/tools/crawling/Book.java @@ -4,19 +4,20 @@ public class Book { private String title; private String author; private String publisher; - private double oldprice; - private double newprice; + private double originalprice; + private double discountedprice; private String href; public Book() { } - public Book(String title, String author, String publisher, double oldprice, double newprice, String href) { + public Book(String title, String author, String publisher, double originalprice, double discountedprice, + String href) { this.title = title; this.author = author; this.publisher = publisher; - this.oldprice = oldprice; - this.newprice = newprice; + this.originalprice = originalprice; + this.discountedprice = discountedprice; this.href = href; } @@ -44,20 +45,20 @@ public class Book { this.publisher = publisher; } - public double getOldprice() { - return oldprice; + public double getOriginalPrice() { + return originalprice; } - public void setOldprice(double oldprice) { - this.oldprice = oldprice; + public void setOriginalPrice(double originalprice) { + this.originalprice = originalprice; } - public double getNewprice() { - return newprice; + public double getDiscountedPrice() { + return discountedprice; } - public void setNewprice(double newprice) { - this.newprice = newprice; + public void setDiscountedPrice(double discountedprice) { + this.discountedprice = discountedprice; } public String getHref() { @@ -71,6 +72,7 @@ public class Book { @Override public String toString() { return "Title: " + this.title + ", Author: " + this.author + ", Publisher: " + this.publisher - + ", Original Price: " + this.oldprice + ", Discounted Price: " + this.newprice + ", URL: " + this.href; + + ", Original Price: " + this.originalprice + ", Discounted Price: " + this.discountedprice + ", URL: " + + this.href; } } diff --git a/src/server/tools/crawling/NewsThread.java b/src/server/tools/crawling/NewsThread.java index 9204a05..131cf15 100644 --- a/src/server/tools/crawling/NewsThread.java +++ b/src/server/tools/crawling/NewsThread.java @@ -37,9 +37,9 @@ public class NewsThread implements Runnable { String oprice = bookElement.select(".price_r").text(); String nprice = bookElement.select(".price_n").first().text(); String href = bookElement.select(".name a").attr("href"); - double oldprice = Double.parseDouble(oprice.substring(1)); - double newprice = Double.parseDouble(nprice.substring(1)); - insertBook(con, title, author, publisher, oldprice, newprice, href); + double originalprice = Double.parseDouble(oprice.substring(1)); + double discountedprice = Double.parseDouble(nprice.substring(1)); + insertBook(con, title, author, publisher, originalprice, discountedprice, href); } } catch (SQLException e) { e.printStackTrace(); @@ -49,15 +49,15 @@ public class NewsThread implements Runnable { } } - private void insertBook(Connection con, String title, String author, String publisher, double oldprice, - double newprice, String href) throws SQLException { + private void insertBook(Connection con, String title, String author, String publisher, double originalprice, + double discountedprice, String href) throws SQLException { String sql = "INSERT INTO book VALUES (?, ?, ?, ?, ?, ?)"; try (PreparedStatement ps = con.prepareStatement(sql)) { ps.setString(1, title); ps.setString(2, author); ps.setString(3, publisher); - ps.setDouble(4, oldprice); - ps.setDouble(5, newprice); + ps.setDouble(4, originalprice); + ps.setDouble(5, discountedprice); ps.setString(6, href); ps.executeUpdate(); }