测试类
package coom.car_rental.java;
import java.util.Scanner;
/*
* 这是测试主类
*/
public class CarRental implements cars{
public static void main(String[] args) {
// TODO 自动生成的方法存根
System.out.println("欢迎使用达达租车系统!");
System.out.println("请问您是否需要租车? 0-否 1-是 ");
Scanner sc = new Scanner(System.in);
int op1 = sc.nextInt();
switch (op1) {
case 0:
System.out.println("欢迎您使用达达租车系统!" + "\n" +
"期待您的再次使用!" + "\n" + "再见!");
System.exit(0);
case 1:
System.out.println("您可租用车的数量及价格表");
System.out.println("序号"+ "\t车名" + "\t车的载客量(人)" + "\t车的载货量(吨)"
+ "\t车的价格(天/元)");
//循环输出车的信息
for(int i = 0;i < cars.length;i ++) {
System.out.println((i + 1) +"\t"+ cars[i]);
}
break;
default:
System.out.println("输入错误!");
break;
}
double price = 0;//总的价格
int allpeople = 0;//总的人数
int allloads = 0;//总的载货量
System.out.println("请输入你需要租车的数量");
Scanner snb = new Scanner(System.in);
int cnb = snb.nextInt();
Car[] rentCar = new Car[cnb];
for(int i=0;i < cnb;i ++) {
System.out.println("请输入您第" + (i + 1) +"的租车序号" );
int number = snb.nextInt();
rentCar[i] =cars[number - 1];
}
System.out.println("请输入您的租车天数");
int dayNumber = snb.nextInt();
System.out.println("\t***************您的账单如下***********");
System.out.println("您选的载人客车有:");
for(int i = 0;i < cnb;i ++) {
if(rentCar[i].busload != 0) {
System.out.println(rentCar[i].name + "\t" + rentCar[i].busload);
}
allpeople = allpeople + rentCar[i].busload;
}
System.out.println("一共有" + allpeople + "人");
System.out.println("您的载货汽车有:");
for(int i = 0;i < cnb;i ++) {
if(rentCar[i].burden != 0) {
System.out.println(rentCar[i].name + "\t" + rentCar[i].burden);
}
allloads = allloads + rentCar[i].burden;
}
System.out.println("一共载货" + allloads + "吨" );
for(int i =0;i < cnb;i ++) {
price = price + rentCar[i].dailyRent * dayNumber;
}
System.out.println("您总的价格是:" + price);
}
}
Car类
package coom.car_rental.java;
public abstract class Car {
public String name;//车的名字
public int carNumber;//租车数量
public int busload;//载客量
public int burden;//载货量
public double dailyRent;//日租金
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCarNumber() {
return carNumber;
}
public void setCarNumber(int carNumber) {
this.carNumber = carNumber;
}
public int getBusload() {
return busload;
}
public void setBusload(int busload) {
this.busload = busload;
}
public int getBurden() {
return burden;
}
public void setBurden(int burden2) {
this.burden = burden2;
}
public double getDailyRent() {
return dailyRent;
}
public void setDailyRent(double dailyRent) {
this.dailyRent = dailyRent;
}
}
车库类
package coom.car_rental.java;
public interface cars {
Car[] cars = {
new PassengerCar("奥迪A4", 4, 500),
new PassengerCar("马自达6", 4, 400),
new PickupTruck("皮卡雪6", 4, 2, 450),
new PassengerCar("金龙", 20, 800),
new Truck("松花江", 4, 400),
new Truck("依维柯", 20, 1000)};
}
卡车类
package coom.car_rental.java;
/*
* 这是货车类,载货的
*/
public class Truck extends Car {
public Truck(String name, int burden, double dailyRent) {
// TODO 自动生成的构造函数存根
this.setName(name);
this.setBurden(burden);
this.setDailyRent(dailyRent);
}
public String toString() {
return this.getName() + "\t" +this.getBurden() + "\t\t" + this.getDailyRent();
}
}
客车类
package coom.car_rental.java;
/*
* 这是客车类,载人的
*/
public class PassengerCar extends Car {
public PassengerCar(String name, int busload,double dailyRent) {
// TODO 自动生成的构造函数存根
this.setName(name);
this.setBusload(busload);
this.setDailyRent(dailyRent);
}
public String toString() {
return this.getName() +"\t" + this.getBusload() + "\t\t\t\t" + this.getDailyRent();
}
}
皮卡类
package coom.car_rental.java;
/*
* 这是皮卡车类,既可以载人也可以载货
*/
public class PickupTruck extends Car {
public PickupTruck(String name, int busload, int burden, double dailyRent) {
// TODO 自动生成的构造函数存根
this.setName(name);
this.setBusload(busload);
this.setBurden(burden);
this.setDailyRent(dailyRent);
}
public String toString () {
return this.getName() + "\t" + this.getBusload() + "\t\t"
+ this.getBurden() + "\t\t" + this.getDailyRent();
}
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦