java二嗨租车项目_Java入门第二季6-1租车项目代码
一共有5個類
Car類 //作為父類
package com.imooc;
public class Car {
public int ID;
public String name;
public int rent;
public int PersonCapacity;
public int GoodsCapacity;
}
2.passengerCar //客車
public class passengerCar extends Car{
public passengerCar(int newID,String newName,int newRent,int newPersonCapacity){
this.ID = newID;
this.name = newName;
this.rent = newRent;
this.PersonCapacity = newPersonCapacity;
}
}
3.Pickup //皮卡
public class Pickup extends Car{
public Pickup(int newID,String newName,int newRent,int newPersonCapacity,int newGoodsCapacity){
this.ID = newID;
this.name = newName;
this.rent = newRent;
this.PersonCapacity = newPersonCapacity;
this.GoodsCapacity = newGoodsCapacity;
}
}
4.Trunk //貨車
public class Trunk extends Car{
public Trunk(int newID,String newName,int newRent,int newGoodsCapacity){
this.ID = newID;
this.name = newName;
this.rent = newRent;
this.GoodsCapacity = newGoodsCapacity;
}
}
5.StartMain // 主函數
package com.imooc;
import java.util.*;
public class StartMain {
public static void main(String[] args){
Car[] AllCar = {new passengerCar(1,"奧迪A4",500,4),new passengerCar(2,"馬自達6",400,4),new Pickup(3,"皮卡雪",450,4,2),new passengerCar(4,"金龍",800,20),new Trunk(5,"松花江",400,4),new Trunk(6,"依維柯",1000,20)};
System.out.println("歡迎來到租車系統");
System.out.println("您是否要租車:1是 0否");
Scanner input = new Scanner(System.in);
int Acess = input.nextInt();
if(Acess!=1)
return;
System.out.println("您可租車的類型及其價目表:");
System.out.println("序號 汽車名稱 租金 容量 ");
for(int i=0;i
if(AllCar[i] instanceof passengerCar) {
System.out.printf("%-1d. %-6s%-4d元/天 載人:%-1d人%n",AllCar[i].ID,AllCar[i].name,AllCar[i].rent,AllCar[i].PersonCapacity);
}
else if(AllCar[i] instanceof Trunk){
System.out.printf("%-1d. %-6s%-4d元/天 載貨:%-1d噸%n",AllCar[i].ID,AllCar[i].name,AllCar[i].rent,AllCar[i].GoodsCapacity);
}
else{
System.out.printf("%-1d. %-6s%-4d元/天 載人:%-1d人 載貨:%-1d噸%n",AllCar[i].ID,AllCar[i].name,AllCar[i].rent,AllCar[i].PersonCapacity,AllCar[i].GoodsCapacity);
}
}
System.out.println("請輸入您要租汽車的數量:");
int Num = input.nextInt();
//創建一個Car類,存儲被租的汽車對象
Car[] odered = new Car[Num];
//輸入汽車的序號
for(int i = 0;i < Num;i++) {
System.out.println("請輸入第"+ (i+1) +"輛車的序號");
//汽車序號
int id = input.nextInt();
//通過遍歷AllCar里所有汽車的ID,把用戶選擇的車對象放進odered數組
for(int j = 0;j
if(AllCar[j].ID == id){
odered[i]=AllCar[j];
break;
}
}
}
System.out.println("請輸入租車天數");
int days = input.nextInt();
System.out.println("您的賬單:");
System.out.println("***可載人的車有:");
int PassengerNum=0;
int TotalPrice=0;
//這里的算價錢還有問題,皮卡算了兩次價格,待修改。
//隨便修改了一下,第二次屏蔽了皮卡的租金計算,還待優化
for(int i =0;i
if(odered[i] instanceof passengerCar || odered[i] instanceof Pickup){
System.out.println(odered[i].name);
PassengerNum += odered[i].PersonCapacity;
TotalPrice += odered[i].rent;
// System.out.println("第"+(i+1)+"臺車的價錢為:"+odered[i].rent);
}
}
System.out.println("共載人:"+ PassengerNum +"人");
System.out.println("載貨的汽車有:");
int GoodsNum=0;
for(int i = 0;i
if(odered[i] instanceof Trunk || odered[i] instanceof Pickup){
System.out.println(odered[i].name);
GoodsNum +=odered[i].GoodsCapacity;
if(odered[i] instanceof Trunk) {
TotalPrice += odered[i].rent;
}
// System.out.println("第"+(i+1)+"臺車的價錢為:"+odered[i].rent);
}
}
System.out.println("共載貨:"+GoodsNum+"噸");
System.out.printf("***租車總價格:"+(TotalPrice*3)+"元");
}
}
總結
以上是生活随笔為你收集整理的java二嗨租车项目_Java入门第二季6-1租车项目代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java星座查询系统_星座查询示例代码
- 下一篇: java秒杀时间与服务器时间_Javas