Bài toán: Xây dựng Lớp Trạm Thu Phí.
Kiểm tra, ghi chép xe qua trạm và tính phí.
Bài 1:
import java.util.Date;
import java.sql.*;
public class GarbTruck{
double phiTheoGio= 120000.0;
int loadingTime = 8;
int MAX = 10000;
double chiPhi1h = 120000.0;
int timeBairac = 30;
double tienBai = 57000.0;
int[] garb;
//Ham dung===========
public GarbTruck(int[] garb) {
this.garb = garb;
}
public double TotalFee() {
return this.phiTaiRac() + this.phiVanChuyen() + this.phiDoRac();
}
public double phiTaiRac() {
return phiTheoGio * (this.timeTaiRac() / 60.0);
}
public int timeTaiRac() {
return loadingTime * this.soLanTai();
}
public int soLanTai() {
return this.garb.length;
}
public double phiVanChuyen() {
return chiPhi1h * (this.timeVanChuyen() / 60.0);
}
public int timeVanChuyen() {
return timeBairac* this.soLanVanChuyen();
}
public int soLanVanChuyen() {
int acc = 0;
int amountOnTruck = 0;
for (int weight : this.garb) {
if (this.overweight(amountOnTruck, weight)) { // quá tai
acc++;
amountOnTruck = weight;
}
else
amountOnTruck += weight;
}
return (amountOnTruck > 0) ? (acc + 1) :
}
private boolean overweight(int amountOnTruck, int weight) {
return amountOnTruck + weight > MAX;
}
public double phiDoRac() {
return tienBai * this.soLanVanChuyen();
}
public static void main(String[] args) {
int[] trashGarb = { 1765, 2808, 952, 4206, 3102,
3902, 1292, 3985, 8324, 1928, 4426, 397, 3277 };
System.out.println("Tong phi phai tra la "+ new GarbTruck(trashGarb).TotalFee());
}
}
Bài 2:
import java.util.Date;
import java.sql.*;
class XeQuaTram{
private String bsxe;
private Date tdqt;
private double lephi;
//----------------------------------------------
public XeQuaTram(String bso){
bsxe=bso;
tdqt=new Date();
if(bso.charAt(2)=='H') lephi = 3000;
}
//----------------------------------------------
public XeQuaTram(String bso,Date tdqt,double sotien){
bsxe=bso;
tdqt=tdqt;
lephi=sotien;
}
//----------------------------------------------
public String getBsxe(){
return bsxe;
}
//----------------------------------------------
public Date getTdqt(){
return tdqt;
}
//----------------------------------------------
public double getLephi(){
return lephi;
}
//----------------------------------------------
public String toString(){
return (bsxe+"\t"+tdqt);
}
//----------------------------------------------
public void saveDB() throws Exception{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("jdbc:odbc:TTP");
Statement stmt = con.createStatement();
String sql="INSERT INTO NhatKy ( bsxe, lephi ) values('"+bsxe+"',"+lephi+")";
System.out.println(sql);
int rc=stmt.executeUpdate(sql);
if(rc!=1) throw new Exception("Loi ghi du lieu");
con.close();
}
}
//==============================================
public class TramThuPhi{
XeQuaTram[]dsxe;
int sl=0;
double tonglp=0;
//----------------------------------------------
public TramThuPhi(){
dsxe=new XeQuaTram[100];
}
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
//----------------------------------------------
public void inbaocao (Date from, Date to){
System.out.println(" Danh sach xe qua tram tu " +from+ " den " +to);
System.out.println(" ");
for(int i=0;i<sl;i++){
Date ngay=dsxe[i].getTdqt();
if(ngay.after(to) && ngay.before(from))
System.out.println(" "+dsxe[i]);
}
System.out.println(" ");
}
//----------------------------------------------
//----------------------------------------------
public static void main(String x[]){
TramThuPhi t=new TramThuPhi();
try{
for(int i=0;i<x.length;i++)
{
//t.ghichep(new XeQuaTram(x[i]));
}
} catch(Exception e)
{
System.out.println(e);
}
t.inbaocao();
}
}