主程序:client.java
package views;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用滴滴租车系统:");
System.out.println("您是否要租车:1是0否");
Scanner input = new Scanner(System.in);
int Num;
if (input.nextInt() == 1) {
System.out.println("您可租用的车型及价目表:");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1. 奥迪A4 500元/天 载人:4人");
System.out.println("2. 马自达 400元/天 载人:4人");
System.out.println("3. 皮卡雪6 450元/天 载人:4人 载货:2吨");
System.out.println("4. 金龙 800元/天 载人:20人");
System.out.println("5. 松花江 400元/天 载货:40吨");
System.out.println("6. 依维柯 1000元/天 载货:20吨");
System.out.println("请输入您要租汽车的数量:");
Num = input.nextInt();
AVehicle[] vehicle = new AVehicle[Num];
for (int i = 0; i < vehicle.length; i++) {
System.out.println("请输入第" + (i + 1) + "车的序号:");
switch (input.nextInt()) {
case 1:
vehicle[i] = new Vehicle1();
break;
case 2:
vehicle[i] = new Vehicle2();
break;
case 3:
vehicle[i] = new Vehicle3();
break;
case 4:
vehicle[i] = new Vehicle4();
break;
case 5:
vehicle[i] = new Vehicle5();
break;
case 6:
vehicle[i] = new Vehicle6();
break;
default:
System.out.println("无此序号车辆");
input.close();
}
}
System.out.println("请输入租车天数:");
int Days = input.nextInt();
int manAvailableCount = 0;
int goodsAvailableCount = 0;
String manAvailableVehicle = "";
String goodsAvailableVehicle = "";
int capacityAll = 0;
float burdenAll = 0f;
float price = 0;
System.out.println("您的账单:");
for (int i = 0; i < vehicle.length; i++) {
if (vehicle[i].capacity != 0) {
manAvailableCount++;
manAvailableVehicle = manAvailableVehicle + vehicle[i].name + " ";
capacityAll = capacityAll + vehicle[i].capacity;
}
if (vehicle[i].burden != 0) {
goodsAvailableCount++;
goodsAvailableVehicle = goodsAvailableVehicle + vehicle[i].name + " ";
burdenAll = burdenAll + vehicle[i].burden;
}
price = price + vehicle[i].rent * Days;
}
System.out.println("*****可载人车有" + manAvailableCount + "辆:");
System.out.println(manAvailableVehicle.toString() + "共可载" + capacityAll + "人");
System.out.println("*****可载货车有" + goodsAvailableCount + "辆:");
System.out.println(goodsAvailableVehicle.toString() + "共可载" + burdenAll + "吨");
System.out.println("*****租车总价格:" + price + "元");
input.close();
} else {
input.close();
System.out.println("谢谢使用");
}
}
}
一个父类AVehicle.java
package views;
public class AVehicle {
public int NO;
public String name;
public float rent; // 租金
public int capacity; // 载人数
public float burden; // 载货量
public AVehicle(){
this.NO = 0;
this.name = "";
this.rent = 0f;
this.capacity = 0;
this.burden = 0f;
}
}
6个子类Vehicle1.java ...... Vehicle6.java类似。
Vehicle1.java
package views;
public class Vehicle1 extends AVehicle {
public Vehicle1() {
this.name = "奥迪A4";
this.NO = 1;
this.rent = 500f;
this.capacity = 4;
}
}
Vehicle2.java
package views;
public class Vehicle2 extends AVehicle {
public Vehicle2() {
this.NO = 2;
this.name = "马自达6";
this.rent = 400f; // 租金
this.capacity = 4; // 载人数
}
}
Vehicle3.java
package views;
public class Vehicle3 extends AVehicle {
public Vehicle3() {
this.NO = 3;
this.name = "皮卡雪6";
this.rent = 450f; // 租金
this.capacity = 4; // 载人数
this.burden = 2; // 载货量
}
}
Vehicle4.java
package views;
public class Vehicle4 extends AVehicle {
public Vehicle4() {
this.NO = 4;
this.name = "金龙";
this.rent = 800f; // 租金
this.capacity = 20; // 载人数
}
}
Vehicle5.java
package views;
public class Vehicle5 extends AVehicle {
public Vehicle5() {
this.NO = 5;
this.name = "松花江";
this.rent = 400f; // 租金
this.burden = 4f; // 载货量
}
}
Vehicle6.java
package views;
public class Vehicle6 extends AVehicle {
public Vehicle6() {
this.NO = 6;
this.name = "依维柯";
this.rent = 1000f; // 租金
this.burden = 20f; // 载货量
}
}
实际运行图:
热门评论
这个没有用继承多态,修改维护很费力。
这个没有用继承多态,修改维护很费力。
的确不错。