第七章 DAO模式——课后作业:

1.开发一个程序,用于记录车辆购置税。要求如下。

>由控制台录入数据,提交后保存到MySQL数据库。

>需要保存的信息:车主身份证号码(18位),车辆识别代码(17位),车辆排量,官方指导价,发票价格,缴纳车辆购置税金额。

>目前车辆购置税征收办法如下。

1.车辆购置税征收额根据计税价格计算,计税价格计算公式如下。

              计税价格=购车发票价格 / (1+17%)

2.排量在1.6L及以下的车型的计算公式如下。

              车辆购置税=计税价格*7.5%

3.排量在1.6L及以上的车型的计算公式如下。

              车辆购置税=计税价格*10%

package dome_kehozuoye;
/**
 * 实体类
 * @author 
 *
 */
public class Car {
    private String identity;//车主身份证号码
    private String heading;//车辆识别码
    private double emissions;//车辆排量
    private double price;//官方指导价
    private double invoice;//发票价格
    private double purchase;//缴纳车辆购税价
    public String getIdentity() {
        return identity;
    }
    public void setIdentity(String identity) {
        this.identity = identity;
    }
    public String getHeading() {
        return heading;
    }
    public void setHeading(String heading) {
        this.heading = heading;
    }
    public double getEmissions() {
        return emissions;
    }
    public void setEmissions(double emissions) {
        this.emissions = emissions;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public double getInvoice() {
        return invoice;
    }
    public void setInvoice(double invoice) {
        this.invoice = invoice;
    }
    public double getPurchase() {
        return purchase;
    }
    public void setPurchase(double purchase) {
        this.purchase = purchase;
    }

}
 

package dome_kehozuoye;
/*
 * 接口
 */
public interface CarDao {
    int save(Car car); //添加
}
 

package dome_kehozuoye;
/*
 * 驱动
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {
    private String driver="com.mysql.jdbc.Driver";//数据库驱动字符串
    private String url="jdbc:mysql://localhost:3306/inquire?characterEncoding=utf-8";
    private String user="root";//数据库名
    private String password="123";
    Connection conn=null;
     //1.打开数据库
    public Connection getConnection() {
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            conn=DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }
    //2.关闭
    public  void closeAll(Connection conn,PreparedStatement stmt,ResultSet rs) {
      if(rs!=null) {
          try {
            rs.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          if(stmt!=null) {
              try {
                stmt.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
              if(conn!=null) {
                  try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
              }
          }
      }
    }
    public int executeUpdate(String sql,Object[] param) {
        int num=0;//影响行数
        conn=this.getConnection();
        PreparedStatement stmt =null;
        try {
            stmt =conn.prepareStatement(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(param!=null) {
            for (int i = 0; i < param.length; i++) {
                try {
                    stmt.setObject(i+1, param[i]);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        try {
            return num=stmt.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            closeAll(conn, stmt, null);
        }
        return num;
    }
}
 

package dome_kehozuoye;
/**
 * 实现继承BaseDao接口CarDao类
 * @author 
 *
 */
public class CarDaoImpl extends BaseDao implements CarDao{

    @Override
    //保存信息
    public int save(Car car) {
        // TODO Auto-generated method stub
        String sql = "insert into Car(identity,heading,emissions,price,invoice,purchase) values(?,?,?,?,?,?)";
        Object[] param = {car.getIdentity(),car.getHeading(),car.getEmissions(),car.getPrice(),car.getInvoice(),car.getPurchase()};
        int result = executeUpdate(sql, param);
        return result;
    }

}
 

package dome_kehozuoye;

import java.util.Scanner;
/*
 * 测试类
 */
public class Test {
    public static void main(String[] args) {
        Car car = new Car();  //调用实体类
        CarDao cardao = new CarDaoImpl();//调用实现接口
        Scanner scanner = new Scanner(System.in);//键盘输入
        System.out.println("记录车辆购置税,请按提示录入相关信息:");
        System.out.print("请输入车主省份证号码(18位):");
        String identity =scanner.nextLine();
        while(identity.length()!=18) {
            System.out.println("对不起!输入有误,请重新输入:");
            identity =scanner.nextLine();
        }
        System.out.print("请输入车辆识别码(17位):");
        String heading = scanner.nextLine();
        while(heading.length()!=17) {
            System.out.print("对不起!输入有误,请重新输入:");
            heading =scanner.nextLine();
        }
        System.out.print("请输入车辆排量:");
        double emissions =scanner.nextDouble();
        System.out.print("请输入官方指导价:");
        double price =scanner.nextDouble();
        System.out.print("请输入发票价格:");
        double invoice =scanner.nextDouble();
        double taxable =0.0;
        double purchase = 0.0;
        taxable = invoice/(1+0.17);
        if(emissions<1.6) {
            purchase = taxable*0.075;
        }else {
            purchase = taxable*0.1;
        }
        car.setEmissions(emissions);//传参
        car.setHeading(heading);
        car.setIdentity(identity);
        car.setInvoice(invoice);
        car.setPrice(price);
        car.setPurchase(purchase);
        cardao.save(car);
        System.out.println("数据保存成功,车辆购置税为"+purchase);//打印购置税
    }
}
 

猜你喜欢

转载自blog.csdn.net/gz98411/article/details/81743027