The code given below represents a saveTransaction() method which is used to save data to a database from the Java program. Given the classes in the image as well as an image of the screen which will call the function, modify the given code so that it loops through the items again, this time as it loops through you are to insert the data into the salesdetails table, note that the SalesNumber from the AUTO-INCREMENT field from above is to be inserted here with each record being placed into the salesdetails table. Finally, as you loop through the items the product table must be update because as products are sold the onhand field in the products table must be updated.  When multiple tables are to be updated with related data, you should wrap it into a DMBS transaction.  The schema for the database is also depicted.  public class PosDAO {     private Connection connection;          public PosDAO(Connection connection) {         this.connection = connection;     }          public void saveTransaction(ArrayList salesDetailsList) {         try {             // Get the total sales by iterating through the sales details list

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

The code given below represents a saveTransaction() method which is used to save data to a database from the Java program. Given the classes in the image as well as an image of the screen which will call the function, modify the given code so that it loops through the items again, this time as it loops through you are to insert the data into the salesdetails table, note that the SalesNumber from the AUTO-INCREMENT field from above is to be inserted here with each record being placed into the salesdetails table. Finally, as you loop through the items the product table must be update because as products are sold the onhand field in the products table must be updated. 
When multiple tables are to be updated with related data, you should wrap it into a DMBS transaction. 

The schema for the database is also depicted. 

public class PosDAO {
    private Connection connection;
    
    public PosDAO(Connection connection) {
        this.connection = connection;
    }
    
    public void saveTransaction(ArrayList<SalesDetails> salesDetailsList) {
        try {
            // Get the total sales by iterating through the sales details list
            double totalSales = 0.0;
            for (SalesDetails salesDetails : salesDetailsList) {
                totalSales += salesDetails.getProduct().getPrice() * salesDetails.getQuantity();
            }
            
            // Insert the current date and total sales into the sales table
            PreparedStatement salesStatement = connection.prepareStatement(
                "INSERT INTO Sales (SalesDate, TotalSales) VALUES (?, ?)"
            );
            salesStatement.setDate(1, new Date(System.currentTimeMillis())); // use current date
            salesStatement.setDouble(2, totalSales);
            salesStatement.executeUpdate();
            
            // Get the auto-generated sales number
            Statement salesNumberStatement = connection.createStatement();
            ResultSet salesNumberResult = salesNumberStatement.executeQuery(
                "SELECT last_insert_rowid() AS SalesNumber"
            );
            int salesNumber = salesNumberResult.getInt("SalesNumber");
            
            // Insert each sales detail with the generated sales number
            PreparedStatement salesDetailsStatement = connection.prepareStatement(
                "INSERT INTO SalesDetails (SalesNumber, ProductCode, Quantity) VALUES (?, ?, ?)"
            );
            for (SalesDetails salesDetails : salesDetailsList) {
                salesDetailsStatement.setInt(1, salesNumber);
                salesDetailsStatement.setString(2, salesDetails.getProduct().getCode());
                salesDetailsStatement.setInt(3, salesDetails.getQuantity());
                salesDetailsStatement.executeUpdate();
            }
        } catch (SQLException e) {
            System.err.println("Error saving transaction: " + e.getMessage());
        }
    }
}

Point of Sales Screen
Product Code 4225
Code
9813
3700
Product Name
Wine Muscadet Sur Lie
Compound - Rum
Save Transaction
Cancel Transaction
Quantity 20
Unit Price
34.1
32.15
I
Qty Total
15
26
X
511.5
835.9
1347.40
Transcribed Image Text:Point of Sales Screen Product Code 4225 Code 9813 3700 Product Name Wine Muscadet Sur Lie Compound - Rum Save Transaction Cancel Transaction Quantity 20 Unit Price 34.1 32.15 I Qty Total 15 26 X 511.5 835.9 1347.40
public class PosDAO {
}
private Connection conn;
PreparedStatement stmt = null;
public boolean openConnection(){
try {
// db parameters
String url = "jdbc:mysql://localhost: 4306/swen2005";
String user = "root";
String password = "";
// create a connection to the database
conn - DriverManager.getConnection(url, user, password);
if (conn!= null)
return true;
}
catch(SQLException e) {
System.out.println(e.getMessage());
}
|}
return false;
}
public void closeConnection ()
return false;
try
{
}
if (conn!=null)
conn.close();
catch(SQLException ex) {
System.out.println(ex.getMessage());
}
public void saveTransaction(List <Salesdetails> items) throws SQLException{
}
public List<Pos> getSales (String fromDate, String toDate) {
VO swen2005 sales
SalesNumber: int(10)
SalesDate : date
# Sales Total: decimal(7,2)
v swen2005 salesdetails
number: int(10)
# SalesNumber: int(10)
prodid: varchar(20)
# price : decimal(7,2)
# qty: int(10)
V
class Product{
public int number;
public String prodID;
public String prodName;
public float prodPrice;
public int prodOnHand;
Product(int num, String id, String name, float price, int qty)
{
number-num;
prodID=id;
prodName=name;
prodPrice-price;
prodonHand-qty;
}
class Salesdetails {
public String prodcode;
public String prodname;
public double unit;
public int qty;
public Salesdetails(String pcode, String pname, double ucost, int q)
{
this.prodcode = pcode;
this.qty = q;
this.prodname = pname;
this.unit = ucost;
swen2005 products
number: int(11)
prodid: varchar(20)
prodname : varchar(30)
#price: decimal(10,2)
#onhand int(11)
public class Pos {
public String salesDate;
public int sales Number;
public String prodcode;
public String prodname;
public double salesTotal;
public double unit;
public int qty;
public Pos (String sd,int sn, String pcode, String pname,double ucost, int q, double st)
this.salesDate = sd;
this.salesNumber - sn;
this.prodcode - pcode;
this.qty = 9;
this.prodname - pname;
this.unit = ucost;
this.salesTotal = st;
Transcribed Image Text:public class PosDAO { } private Connection conn; PreparedStatement stmt = null; public boolean openConnection(){ try { // db parameters String url = "jdbc:mysql://localhost: 4306/swen2005"; String user = "root"; String password = ""; // create a connection to the database conn - DriverManager.getConnection(url, user, password); if (conn!= null) return true; } catch(SQLException e) { System.out.println(e.getMessage()); } |} return false; } public void closeConnection () return false; try { } if (conn!=null) conn.close(); catch(SQLException ex) { System.out.println(ex.getMessage()); } public void saveTransaction(List <Salesdetails> items) throws SQLException{ } public List<Pos> getSales (String fromDate, String toDate) { VO swen2005 sales SalesNumber: int(10) SalesDate : date # Sales Total: decimal(7,2) v swen2005 salesdetails number: int(10) # SalesNumber: int(10) prodid: varchar(20) # price : decimal(7,2) # qty: int(10) V class Product{ public int number; public String prodID; public String prodName; public float prodPrice; public int prodOnHand; Product(int num, String id, String name, float price, int qty) { number-num; prodID=id; prodName=name; prodPrice-price; prodonHand-qty; } class Salesdetails { public String prodcode; public String prodname; public double unit; public int qty; public Salesdetails(String pcode, String pname, double ucost, int q) { this.prodcode = pcode; this.qty = q; this.prodname = pname; this.unit = ucost; swen2005 products number: int(11) prodid: varchar(20) prodname : varchar(30) #price: decimal(10,2) #onhand int(11) public class Pos { public String salesDate; public int sales Number; public String prodcode; public String prodname; public double salesTotal; public double unit; public int qty; public Pos (String sd,int sn, String pcode, String pname,double ucost, int q, double st) this.salesDate = sd; this.salesNumber - sn; this.prodcode - pcode; this.qty = 9; this.prodname - pname; this.unit = ucost; this.salesTotal = st;
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY