-
文件图
- 共有的抽象类Common
package 租车系统;
public abstract class Common {
public int num; //汽车序号
public String name; //汽车名字
public int zaiKe; //载客量
public int zaiHuo; //载货量
public int money; //一天租金
public void display(){
//输出单辆汽车价目表
System.out.print(num+". "+name+" "+money+"元/天 ");
if(money<1000)
System.out.print(" ");
if(zaiKe>0){
System.out.print("载人:"+zaiKe+"人");
}
if(zaiHuo>0){
if(zaiKe>0){
System.out.print(",");
}
System.out.print("载货:"+zaiHuo+"吨");
}
System.out.println();
}
}
- 汽车1
package 租车系统;
public class Car1 extends Common {
Car1(){
num = 1;
name = "奥迪A4 ";
money = 500;
zaiKe = 4;
zaiHuo = 0;
}
}
- 汽车2
package 租车系统;
public class Car2 extends Common {
Car2(){
num = 2;
name = "马自达6 ";
money = 400;
zaiKe = 4;
zaiHuo = 0;
}
}
- 汽车3
package 租车系统;
public class Car3 extends Common {
Car3(){
num = 3;
name = "皮卡雪6 ";
money = 450;
zaiKe = 4;
zaiHuo = 2;
}
}
- 汽车4
package 租车系统;
public class Car4 extends Common {
Car4(){
num = 4;
name = "金龙 ";
money = 800;
zaiKe = 20;
zaiHuo = 0;
}
}
- 汽车5
package 租车系统;
public class Car5 extends Common {
Car5(){
num = 5;
name = "松花江 ";
money = 400;
zaiKe = 0;
zaiHuo = 4;
}
}
- 汽车6
package 租车系统;
public class Car6 extends Common {
Car6(){
num = 6;
name = "依维柯 ";
money = 1000;
zaiKe = 0;
zaiHuo = 20;
}
}
- Main函数
package 租车系统;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Common c1 = new Car1();
Common c2 = new Car2();
Common c3 = new Car3();
Common c4 = new Car4();
Common c5 = new Car5();
Common c6 = new Car6();
System.out.println("欢迎使用答答租车系统:");
System.out.println("您是否要租车:1是 0否");
Scanner input = new Scanner(System.in);
int i = input.nextInt();
if(i == 0){
System.exit(-1); //输入0退出程序
}
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号 汽车名称 租金 容量");
c1.display();
c2.display();
c3.display();
c4.display();
c5.display();
c6.display();
System.out.println("请输入您要租汽车的数量:");
int j = input.nextInt(); //j为租汽车的数量
if(j<=0){
System.out.println("数字不能小于等于0");
System.exit(-1);
}
String[] m = new String [j]; //m为可载人的车
String[] n = new String[j]; //n为可载货的车
int sumRen = 0; //sumRen保存总载客量
int sumHuo = 0; //sumHuo保存总载货量
int sumMoney = 0; //sumMoney保存总价格
int num; //num为选择的车辆序号
for(i=0;i<j;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号");
num = input.nextInt();
switch(num){
//计算车型,载客量,载货量,总价格
case 1:{
m[i] = c1.name;
sumRen += c1.zaiKe;
sumHuo += c1.zaiHuo;
sumMoney += c1.money;
break;
}
case 2:{
m[i] = c2.name;
sumRen += c2.zaiKe;
sumHuo += c2.zaiHuo;
sumMoney += c2.money;
break;
}
case 3:{
m[i] = c3.name;
n[i] = c3.name;
sumRen += c3.zaiKe;
sumHuo += c3.zaiHuo;
sumMoney += c3.money;
break;
}
case 4:{
m[i] = c4.name;
sumRen += c4.zaiKe;
sumHuo += c4.zaiHuo;
sumMoney += c4.money;
break;
}
case 5:{
n[i] = c5.name;
sumRen += c5.zaiKe;
sumHuo += c5.zaiHuo;
sumMoney += c5.money;
break;
}
case 6:{
n[i] = c6.name;
sumRen += c6.zaiKe;
sumHuo += c6.zaiHuo;
sumMoney += c6.money;
break;
}
}
}
System.out.println("请输入租车天数");
int day = input.nextInt();
sumMoney *= day;
System.out.println("您的账单:");
System.out.println("可载人的车有:");
for(i=0;i<j;i++){
//输出可载人的车
if(m[i]!=null)
System.out.print(m[i]+" ");
}
System.out.println("共载人:"+sumRen+"人");
System.out.println("可载货的车有:");
for(i=0;i<j;i++){
//输出可载货的车
if(n[i]!=null)
System.out.print(n[i]+" ");
}
System.out.println("共载货:"+sumHuo+"吨");
System.out.println("租车的总价格:"+sumMoney+"元");
}
}
- 程序运行效果