//定义一个父类Car类,包含子类所需变量和方法
public abstract class Car {
int number; //租用某一车型的数量
int price,sum; //某一车型的一辆车租金与总租金
int goods,burden; //某一车型的一辆车载货量与总载货量
int human,capacity; //某一车型的限坐人数与总限坐人数
int day; //租用天数
public abstract int total(); //该方法用于计算租赁的所有车所需总租金
}
/*
*皮卡车类
*继承于Car类,实现Goods和Guest接口,并重写父类和接口中的方法
*有一个有参构造函数用于获取某种车数量和租用天数
*该类车需要计算载人量、载货量与总租金
*/
class Pickup extends Car implements Goods,Guest{
public Pickup(int number,int day){
this.number = number;
this.day = day;
}
@Override
public int capacity() { //计算总的载人量
human = 4; //皮卡车限载4人
capacity = human * number;
return capacity;
}
@Override
public int burden() { //计算总的载货量
goods = 2; //皮卡车限载货物量2吨
burden = goods * number;
return burden;
}
@Override
public int total() { //计算租赁该车总租金
price = 450;
sum = price * number * day;
return sum;
}
}
/*
*小货车类
*继承于Car类,实现Goods接口,并重写父类和接口中的方法
*有一个有参构造函数用于获取某种车数量和租用天数
*该类车需要计算载货量与总租金
*/
class STruck extends Car implements Goods{
public STruck(int number,int day){
this.number = number;
this.day = day;
}
@Override
public int burden() { //计算总载货量
goods = 4; //小货车限载货量4吨
burden = goods * number;
return burden;
}
@Override
public int total() { //计算租赁该车总租金
price = 400;
sum = price * number * day;
return sum;
}
}
/*
*大货车类
*继承于Car类,实现Goods接口,并重写父类和接口中的方法
*有一个有参构造函数用于获取某种车数量和租用天数
*该类车需要计算载货量与总租金
*/
class Truck extends Car implements Goods{
public Truck(int number,int day){
this.number = number;
this.day = day;
}
@Override
public int burden() { //计算总载货量
goods = 20; //大货车限载货量20吨
burden = goods * number;
return burden;
}
@Override
public int total() { //计算租赁该车总租金
price = 1200;
sum = price * number * day;
return sum;
}
}
/*
*小客车类
*继承于Car类,实现Guest接口,并重写父类和接口中的方法
*有一个有参构造函数用于获取某种车数量和租用天数
*该类车需要计算载人量与总租金
*/
class SCoach extends Car implements Guest{
public SCoach(int number, int day){
this.number = number;
this.day = day;
}
@Override
public int capacity() { //计算总载人量
human = 18; //小客车限载12人
capacity = human * number;
return capacity;
}
@Override
public int total() { //计算租赁该车总租金
price = 900;
sum = price * number * day;
return sum;
}
}
/*
*大客车类
*继承于Car类,实现Guest接口,并重写父类和接口中的方法
*有一个有参构造函数用于获取某种车数量和租用天数
*该类车需要计算载人量与总租金
*/
class Coach extends Car implements Guest{
public Coach(int number,int day){
this.number = number;
this.day = day;
}
@Override
public int capacity() { //计算总载人量
human = 30; //客车限载30人
capacity = human * number;
return capacity;
}
@Override
public int total() { //计算租赁该车总租金
price = 1600;
sum = price * number * day;
return sum;
}
}
volume.java
/*
* 声明两个接口Goods和Guest
* 其内包含的方法burden()和capacity()
* 两种方法分别用于计算租用某一车型的总载货量和总载人量
*/
interface Goods {
public abstract int burden();
}
interface Guest{
public abstract int capacity();
}
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String c;
/*
* 数组用于保存每种车的租用情况
* 行分别表示:五种车型
* 列分别表示:租车数量、总租金、总载人量、总载货量
*/
int[][] sum = new int[5][4];
int n, number, day;
int human, money,goods;
int capacity = 0, total = 0, burden = 0;
do {
//do-while语句用于循环,只要询问是否租车时输入内容不为“n”则不会退出
System.out.println("欢迎使用答答租车系统!" + "\n" + "是否需要租车?(是(y)否(n)");
Scanner scan = new Scanner(System.in);
c = scan.nextLine();
if (c.equals("n")){
System.out.println("谢谢使用!");
System.exit(0);
}
else if (!(c.equals("y"))){
//若输入内容既不为n也不为y则提示重新输入
System.out.println("请输入y或n!");
continue;
}
System.out.println("------------------------------");
System.out.println("\t租赁车辆价目表");
System.out.println("------------------------------");
System.out.println("序号 汽车名称 租金 容量");
System.out.println("1 安源 900元/天 载人:18人" + "\n" + "2 皮卡雪6 450元/天 载人4人 载货:2吨");
System.out.println("3 金龙 1600元/天 载人:30人" + "\n" + "4 松花江 400/天 载货:4吨");
System.out.println("5 依维柯 1200元/天 载货:20吨");
System.out.println("------------------------------\n");
do {
//该do-while循环用于租车操作,根据输入的车型序号进行租车,输入0时结束租车
System.out.println("请输入你要租的车型序号(结束输入0):");
n = scan.nextInt();
if (n == 0)
break;
else if (n > 5 || n < 0) {
//若输入的序号不为0-5则提示重新输入
System.out.println("请输入序号或0!");
continue;
}
System.out.println("请输入你要租的数量");
number = scan.nextInt();
System.out.println("请输入你要租的天数");
day = scan.nextInt();
switch (n) {
case 1:
//若选择一号车型则定义一个小客车类型的对象
SCoach sco = new SCoach(number, day);
human = sco.capacity(); //human变量用于临时存储该车型总载人量
money = sco.total(); //money变量用于临时存储该车型总租金
capacity += human; //将当前车型总载人量加入最终总载人量
total += money; //将当前车型总租金加入最终租金
sum[0][0] += number; //将租车数加入该车型租用数
sum[0][1] += money; //将租金加入该车型租金
sum[0][2] += human; //将载人量加入该车型总载人量,下同
break;
case 2:
Pickup pic = new Pickup(number, day);
human = pic.capacity();
goods = pic.burden();
money = pic.total();
capacity += human;
burden += goods;
total += money;
sum[1][0] += number;
sum[1][2] += money;
sum[1][2] += human;
sum[1][3] += goods;
break;
case 3:
Coach co = new Coach(number, day);
human = co.capacity();
money = co.total();
capacity += human;
total += money;
sum[2][0] += number;
sum[2][3] += money;
sum[2][2] += human;
break;
case 4:
STruck str = new STruck(number, day);
goods = str.burden();
money = str.total();
burden += goods;
total += money;
sum[3][0] += number;
sum[3][4] += money;
sum[3][3] += goods;
break;
case 5:
Truck tr = new Truck(number, day);
goods = tr.burden();
money = tr.total();
burden += goods;
total += money;
sum[4][0] += number;
sum[4][5] += money;
sum[4][3] += goods;
break;
}
} while (n != 0);
System.out.println("-------------------------------------------------");
System.out.println("\t\t |租赁车辆清单|");
System.out.println("-------------------------------------------------");
System.out.println("汽车名称 数量(辆) 总租金(元) 总载人量(人) 总载货量(吨)");
if(sum[0][0] != 0)
System.out.println("奥迪A4 " + sum[0][0] + "\t" + sum[0][6] + "\t " + sum[0][2] + "\t\t" +sum[0][3]);
if(sum[1][0] != 0)
System.out.println("皮卡雪6 " + sum[1][0] + "\t" + sum[1][7] + "\t " + sum[1][2] + "\t\t" +sum[1][3]);
if(sum[2][0] != 0)
System.out.println("金龙 " + sum[2][0] + "\t" + sum[2][8] + "\t " + sum[2][2] + "\t\t" +sum[2][3]);
if(sum[3][0] != 0)
System.out.println("松花江 " + sum[3][0] + "\t" + sum[3][9] + "\t " + sum[3][2] + "\t\t" +sum[3][3]);
if(sum[4][0] != 0)
System.out.println("依维柯 " + sum[4][0] + "\t" + sum[4][10] + "\t " + sum[4][2] + "\t\t" +sum[4][3]);
System.out.println("总租金为:" + total + "元\n" + "总载人量为:" + capacity + "人\n"
+ "总载货量为" + burden + "吨");
System.out.println("-------------------------------------------------\n");
} while (!c.equals("y"));
}
}
运行效果:
热门评论
没有租车数量限制吗,无限制的new