看了很多份代碼,終于寫完了
package?com.imooc.car_rental; ?/*?? ?*?汽車父類?? ?*/ ?public?class?Car?{ ? private?String?name;//車名 ? private?float?rental;//租金 ? public?Car(String?name,float?rental){ ? this.name=name; ? this.rental=rental; ? } ? public?String?getName()?{ ? return?name; ? } ? public?float?getRental()?{ ? return?rental; ? }
package com.imooc.car_rental;
/*
?* 轎車類
?*/
public class Sedan extends Car {
private int seatingCapacity;//載客人數(shù)
public Sedan(String name,float rental,int seatingCapacity){
super(name,rental);
this.seatingCapacity=seatingCapacity;
}
public int getSeatingCapacity(){
return seatingCapacity;
}
}
package com.imooc.car_rental;
/*
?* 貨車類
?*/
public class Truck extends Car {
private float cargoCapacity;//載貨量
public Truck(String name, float rental,float cargoCapacity) {
super(name, rental);
this.cargoCapacity=cargoCapacity;
// TODO Auto-generated constructor stub
}
public float getCargoCapacity(){
return cargoCapacity;
}
}
package?com.imooc.car_rental; /*? *??皮卡類 */ public?class?Pickup?extends?Car?{ private?int?seatingCapacity;//載客人數(shù) private?float?cargoCapacity;//載貨量 public?Pickup(String?name,?float?rental,int?seatingCapacity,float?cargoCapacity)?{ super(name,?rental); this.seatingCapacity=seatingCapacity; this.cargoCapacity=cargoCapacity; //?TODO?Auto-generated?constructor?stub } public?int?getSeatingCapacity(){ return?seatingCapacity; } public?float?getCargoCapacity(){ return?cargoCapacity; } }
package com.imooc.car_rental;
import java.util.Scanner;
/*
?* 租車系統(tǒng)
?*/
public class CarRentalSystem {
//菜單
public void menu(){
//創(chuàng)建Scanner對象
Scanner scan=new Scanner(System.in);
System.out.println("歡迎使用答答租車系統(tǒng):");
System.out.println("您是否要租車:1是? 0否");
int num=scan.nextInt();
if(num==1){
//進(jìn)入租車菜單
carRentalMenu();
}else{
System.out.println("為您退出答答租車");
System.exit(0);
}
}
//汽車列表
public void carRentalMenu(){
//創(chuàng)建Scanner對象
Scanner scan=new Scanner(System.in);
System.out.println("您可租車的類型及其價目表:");
Car[] cars={new Sedan("奧迪A4", 500, 4),
new Sedan("馬自達(dá)6", 400, 4),
new Pickup("皮卡雪6",450,4,2),
new Sedan("金龍 ",800,20),
new Truck("松花江",400,4),
new Truck("依維柯",1000,20)};
System.out.println("序號\t名稱\t\t租金\t\t\t容量");
//輸出汽車列表
for(int i=0;i<cars.length;i++){
//判斷汽車類型
if(cars[i] instanceof Sedan){
Sedan car=(Sedan)cars[i];
System.out.println((i+1)+"\t"+car.getName()+"\t"+car.getRental()+"元/天\t載人:"+car.getSeatingCapacity()+"人");
}else if(cars[i] instanceof Pickup){
Pickup car=(Pickup)cars[i];
System.out.println((i+1)+"\t"+car.getName()+"\t"+car.getRental()+"元/天\t載人:"+car.getSeatingCapacity()+"人"+"載貨:"+car.getCargoCapacity()+"噸");
}else if(cars[i] instanceof Truck){
Truck car=(Truck) cars[i];
System.out.println((i+1)+"\t"+car.getName()+"\t"+car.getRental()+"元/天\t載貨:"+car.getCargoCapacity()+"噸");
}
}
/*for(Car car:cars){//需要重寫toString()方法
System.out.println(car.toString());
}*/
System.out.println("請輸入您要租汽車車的數(shù)量");
int rent_num=scan.nextInt();
int id;//定義編號
Car[] rent_cars=new Car[rent_num];//定義數(shù)組保存租出的車
//定義總租金、總載人量、總載貨量變量
float totalRent=0;
int totalSeating=0;
float totalCargo=0;
for(int i=0;i<rent_num;i++){
System.out.println("請輸入第"+(i+1)+"輛車的編號");
id=scan.nextInt();
rent_cars[i]=cars[id-1];
//判斷汽車類型
if(cars[id-1] instanceof Sedan){
Sedan car=(Sedan) cars[id-1];
totalRent+=car.getRental();
totalSeating+=car.getSeatingCapacity();
}else if(cars[id-1] instanceof Pickup){
Pickup car=(Pickup)cars[id-1];
totalRent+=car.getRental();
totalSeating+=car.getSeatingCapacity();
totalCargo+=car.getCargoCapacity();
}else if(cars[id-1] instanceof Truck){
Truck car=(Truck) cars[id-1];
totalRent+=car.getRental();
totalCargo+=car.getCargoCapacity();
}
}
System.out.println("請選擇要租用的天數(shù)");
int rent_day=scan.nextInt();
totalRent*=rent_day;//計(jì)算總金額
System.out.println("**************************");
System.out.println("您的賬單:");
System.out.println("***可載人的車有:");
for(int i=0;i<rent_cars.length;i++){
if(!(rent_cars[i] instanceof Truck)){
System.out.print(rent_cars[i].getName()+"\t");
}
}
System.out.println("共載人:"+totalSeating+"人");
System.out.println("***可載貨的車有:");
for(int i=0;i<rent_cars.length;i++){
if(!(rent_cars[i] instanceof Sedan)){
System.out.print(rent_cars[i].getName()+"\t");
}
}
System.out.println("共載貨:"+totalCargo+"噸");
System.out.println("***租車總金額為:"+totalRent+"元");
}
}
/*測試類*/
package com.imooc.car_rental;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CarRentalSystem car=new CarRentalSystem();
car.menu();
}
}
2021-02-22
牛牪犇