记录车辆购置税
package homework;
//实体类
public class Car {
String IdCard = "";//车主身份证号码
String CarId = "";//车辆识别代码
double Displacement;//车辆排量
double Officialprice=0;//官方指导价
double Invoiceprice=0;//发票价格
double Taxcounting=0; //计税
int Purchasetax=0; //购置税
public String getIdCard() {
return IdCard;
}
public void setIdCard(String idCard) {
IdCard = idCard;
}
public String getCarId() {
return CarId;
}
public void setCarId(String carId) {
CarId = carId;
}
public double getDisplacement() {
return Displacement;
}
public void setDisplacement(double displacement) {
Displacement = displacement;
}
public double getOfficialprice() {
return Officialprice;
}
public void setOfficialprice(double officialprice) {
Officialprice = officialprice;
}
public double getInvoiceprice() {
return Invoiceprice;
}
public void setInvoiceprice(double invoiceprice) {
Invoiceprice = invoiceprice;
}
public double getTaxcounting() {
return Taxcounting;
}
public void setTaxcounting(double taxcounting) {
Taxcounting = taxcounting;
}
public int getPurchasetax() {
return Purchasetax;
}
public void setPurchasetax(int purchasetax) {
Purchasetax = purchasetax;
}
}
package homework;
//接口
public interface Cardao {
int save(Car car);
}
package homework;
//接口实现类
public class Cardaoimpl extends Basedao implements Cardao {
@Override
public int save(Car car) {
// TODO 自动生成的方法存根
String sql = "INSERT INTO car (Owneridentitycard,Vehicleidentificationcode,displacement,Officialguidanceprice,Invoiceprice,Purchasetax)VALUES(?,?,?,?,?,?)";
Object[]param= {car.getIdCard(),car.getCarId(),car.getDisplacement(),car.getOfficialprice(),car.getInvoiceprice(),car.getPurchasetax()};
int row=this.exceuteUpdate(sql, param);
return row;
}
}
package homework;
//工具类
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Basedao {
private String driver = "com.mysql.jdbc.Driver";//数据库驱动字符
private String url = "jdbc:mysql://localhost:3306/aaa?characterEncoding=utf-8";//数据库链接地址
private String useName= "root";//数据库用户名
private String paswoord= "1234";//数据库密码
Connection conn=null;
//链接数据库
public Connection getConnection() {
if (conn==null) {
try {
Class.forName(driver);
conn=DriverManager.getConnection(url, useName,paswoord);
} catch (SQLException | ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
return conn;
}
//关闭数据库链接
public void closse(Connection conn, Statement stmt,
ResultSet rs) {
//关闭结果集
if(rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//关闭Statement对象
if (stmt!=null) {
try {
stmt.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
//关闭数据库链接对象
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public int exceuteUpdate (String sql, Object[] param) {
PreparedStatement pstmt = null;
int num = 0;
conn = getConnection();
try {
pstmt = conn.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
//为预编译sql设置参数
pstmt.setObject(i + 1, param[i]);
}
}
num = pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally{
closse(conn,pstmt,null);
}
return num;
}
}
package homework;
//程序入口
import java.util.Scanner;
public class Dome4 {
public static void main(String[] args) {
Cardao cardao =new Cardaoimpl();
Car car = new Car();
String IdCard = "";//车主身份证号码
String CarId = "";//车辆识别代码
double Displacement;//车辆排量
double Officialprice=0;//官方指导价
double Invoiceprice=0;//发票价格
double Taxcounting=0; //计税
int Purchasetax=0; //购置税
Scanner in = new Scanner(System.in);
System.out.println("记录车辆购置税,请按提示录入相关信息:");
System.out.print("请输入车主身份证号码(18位):");
IdCard=in.next();
if (IdCard.length()!=18) {
System.out.println("您的输入有误,请重新输入");
return;
}
System.out.print("请输入车辆识别代码(17位):");
CarId = in.next();
if (CarId.length()!=17) {
System.out.println("您的输入有误,请重新输入");
return;
}
System.out.print("请输入车辆排量:");
Displacement= in.nextDouble();
System.out.print("请输入官方指导价:");
Officialprice= in.nextInt();
System.out.print("请输入发票价格:");
Invoiceprice= in.nextInt();
Taxcounting=Invoiceprice/(1+0.17);
if (Displacement<=1.6) {
Purchasetax=(int) (Taxcounting*0.075);
}else {
Purchasetax=(int) (Taxcounting*0.1);
}
car.setIdCard(IdCard);
car.setCarId(CarId);
car.setDisplacement(Displacement);
car.setOfficialprice(Officialprice);
car.setInvoiceprice(Invoiceprice);
car.setPurchasetax(Purchasetax);
cardao.save(car);
System.out.println("数据保存成功,车辆购置税为"+Purchasetax);
}
}