1. 程式人生 > >資料庫程式設計第七章課後作業

資料庫程式設計第七章課後作業

記錄車輛購置稅

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);
	}

}