java—撰寫汽車租賃系統
題目要求:
1,汽車租賃資訊表如下:

2,類和屬性:

3,運行效果:

效果實作:


代碼實作:
1,車類:
package homework.exam;
public abstract class Vehicle {
private String num;
private String brand;
private double rent;
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getRent() {
return rent;
}
public void setRent(double rent) {
this.rent = rent;
}
public Vehicle() {
}
//含參構造
public Vehicle(String num, String brand, double rent) {
this.num = num;
this.brand = brand;
this.rent = rent;
}
@Override
public String toString() {
return "汽車{" +
"車牌號='" + num + '\'' +
", 品牌='" + brand + '\'' +
", 日租金=" + rent +
'}';
}
public abstract double totalmoney(int days , double rent);
public abstract boolean equals(Vehicle o);
}
2,汽車類:
package homework.exam;
public class Cars extends Vehicle{
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public Cars(String brand,String type) {
this.type = type;
}
public Cars(String num, String brand, double rent, String type) {
super(num, brand, rent);
this.type = type;
}
@Override
public String toString() {
return "Cars{" +
"type='" + type + '\'' +
'}';
}
//計算小汽車的總租金
@Override
public double totalmoney(int days, double rent) {
if (days>7){
return days*rent*0.9;
}else if (days>30){
return days*rent*0.8;
}else if (days>150){
return days*rent*0.7;
}
return days*rent;
}
//重寫equals方法
@Override
public boolean equals(Vehicle o) {
if (o instanceof Cars){
Cars cars= (Cars) o;
return this.getType().equals(cars.getType())&&this.getBrand().equals(o.getBrand());
}
return false;
}
}
3,客車類:
package homework.exam;
public class Bus extends Vehicle {
private String seat;
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
public Bus(String num, String brand, double rent, String seat) {
super(num, brand, rent);
this.seat = seat;
}
//計算客車的租金
@Override
public double totalmoney(int days, double rent) {
if (days>=3){
return days*rent*0.9;
}else if (days>=7){
return days*rent*0.8;
}else if (days>=30){
return days*rent*0.7;
}else if (days>=150){
return days*rent*0.6;
}
return days*rent;
}
//重寫equals方法
@Override
public boolean equals(Vehicle o) {
return false;
}
}
4,車輛管理類:
package homework.exam;
public class CarRent {
//創建汽車陣列,將汽車的資訊放在陣列中
public Cars[] carMake(){
Cars c1 = new Cars("京NY28588", "寶馬", 800, "x6");
Cars c2 = new Cars("京CNY3284", "寶馬", 600, "550i");
Cars c3 = new Cars("京NT37465", "別克", 300, "林蔭大道");
Cars c4 = new Cars("京NT96928", "別克", 600, "GL8");
Cars[] arr1 ={c1,c2,c3,c4};
return arr1;
}
//創建客車陣列,將汽車的資訊放在陣列中
public Bus[] busMake(){
Bus b1 = new Bus("京6566754", "金杯", 800, "16座");
Bus b2 = new Bus("京8696667", "金龍", 800, "16座");
Bus b3 = new Bus("京9696996", "金杯", 1500, "34座");
Bus b4 = new Bus("京8696998", "金龍", 1500, "34座");
Bus[] arr2={b1,b2,b3,b4};
return arr2;
}
}
5,業務服務類:
package homework.exam;
import java.util.Scanner;
public class CarService {
public void rentcar(){
System.out.println("**********歡迎光臨秋名山守望者汽車租賃公司**********");
Scanner sc = new Scanner(System.in);
System.out.println("1,轎車 2,客車");
System.out.print("請輸入您要租賃的汽車型別:");
int i = sc.nextInt();
CarRent carRent = new CarRent(); //創建車庫物件
Cars[] cars = carRent.carMake(); //拿到轎車陣列物件
Cars car=null;
Bus[] buses = carRent.busMake(); //拿到客車陣列物件
Bus bus=null;
//判斷用戶選擇的車型
if (i==1){
System.out.print("請選擇你要租賃的汽車品牌:(1,別克 2,寶馬)");
int i1 = sc.nextInt();
if (i1==1){
System.out.print("請輸入你要租賃的汽車型別:(1,林蔭大道 2,GL8 )");
}else {
System.out.print("請輸入你要租賃的汽車型別:(1,x6 2,550i )");
}
String i2 = sc.next();
//遍歷汽車陣列,拿到用戶選擇的汽車
for (int j = 0; j < cars.length; j++) {
if (cars[j].getType().equals(i2)){ //當選擇的車的型別與陣列中的匹配時
car=cars[j]; //將車賦值給car
break;
}
}
System.out.print("請輸入你要租賃的天數:");
int days = sc.nextInt();
System.out.print("分配給你的汽車牌號是:");
System.out.println(car.getNum()); //獲取汽車的車牌
double totalmoney =0; //呼叫total
totalmoney = car.totalmoney(days, car.getRent()); //計算用戶的租金
System.out.print("你需要支付的租賃分費用是:");
System.out.print(totalmoney);
}else if (i==2){
System.out.print("請選擇你要租賃的汽車品牌:(1,金龍 2,金杯)");
String i2 = sc.next();
System.out.print("請輸入你要租賃的汽車座位數:(1,16座 2,34座)");
String i3 = sc.next();
//遍歷客車陣列,拿到用戶選擇的客車
for (int j = 0; j < buses.length; j++) {
//當輸入的客車的車牌和座位與陣列中的相等,就選出用戶選擇的車
if (buses[j].getBrand().equals(i2)&&buses[j].getSeat().equals(i3)){
bus=buses[j]; //將選擇的車輛賦值給bus
break;
}
}
System.out.print("請輸入你要租賃的天數:");
int days = sc.nextInt();
System.out.print("分配給你的汽車牌號是:");
System.out.println();
System.out.println(bus.getNum()); //拿到用戶選擇的車牌號
double totalmoney = 0; //呼叫totalmoney方法
totalmoney=bus.totalmoney(days, bus.getRent()); //用用戶輸入的天數,來計算租金
System.out.print("你需要支付的租賃分費用是:");
System.out.print(totalmoney);
}
}
}
6,測驗類:
package homework.exam;
public class Test {
public static void main(String[] args) {
CarService cs = new CarService();
cs.rentcar();
}
}
控制臺輸入的內容,我選擇的是輸入字串型別,沒有按照效果圖上,如果你做的話,你可以用三元運算子來實作哦!
剛接到這個專案總覺得自己不行,但是不想認輸,就一點點嘗試,最后終于寫出來了,還是感覺小有成就吧,哈哈!你也來嘗試一下吧!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/237222.html
標籤:java
下一篇:求1000以內的水仙花數
