较为简单的程序,可能没有利用全面对对象的特性,但从初始功能上可以达到题目要求。
测试类,main方法所在:
package com.bibi.imooc;
import java.util.*;
public class LeaseCar {
private static Scanner num1;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎进入租车系统");
System.out.println("请问您是否要租车?输入1租车,输入2退出。");
Scanner input1=new Scanner(System.in);
int in=input1.nextInt();
if (in==1) {
System.out.println("您可租车的类型及其价目表:");
System.out.println("序号\t车型\t租金(元/天)\t载客、货量");
//创建车对象,并赋予其相应属性,并输出
Car car1=new Carriage();//车1
car1.getNumber(1, "奥迪A4", 500, 4, 0);
System.out.println(car1.toString());
Car car2=new Carriage();//车2
car2.getNumber(2, "马自达6", 400, 4, 0);
System.out.println(car2.toString());
Car car3=new PickUp();//车3
car3.getNumber(3, "皮卡雪", 450, 4, 2);
System.out.println(car3.toString());
Car car4=new Carriage();//车4
car4.getNumber(4, "金龙", 800, 20, 0);
System.out.println(car4.toString());
Car car5=new Truck();//车5
car5.getNumber(5, "松花江", 400, 0, 4);
System.out.println(car5.toString());
Car car6=new Truck();//车6
car6.getNumber(6, "依维河", 1000, 0, 20);
System.out.println(car6.toString());
//录入租车的数量、型号、天数
System.out.println("请输入车的租用数量");
Scanner input2=new Scanner(System.in);int in2=input2.nextInt();
int[] counts=new int[in2];int days=0;//counts代表车型数组;days代表租用天数
for(int i=0;i<in2;i++) {
System.out.println("请输入第"+ (i+1) +"辆车的序号");
Scanner input3=new Scanner(System.in);counts[i]=input3.nextInt();
}
System.out.println("请输入租车天数");
Scanner input4=new Scanner(System.in);days=input4.nextInt();
//统计各型号车的数量
int sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0;
for(int j=0;j<in2;j++) {
if (counts[j]==1) {sum1++;}
if (counts[j]==2) {sum2++;}
if (counts[j]==3) {sum3++;}
if (counts[j]==4) {sum4++;}
if (counts[j]==5) {sum5++;}
if (counts[j]==6) {sum6++;}
}
//统计总金额、总载客人数
System.out.println("您的总账单为:");
System.out.println("奥迪A4:"+sum1+"辆\t马自达6:"+sum2+"辆\t皮卡雪:"+sum3+"辆");
System.out.println("金龙:"+sum4+"辆\t松花江:"+sum5+"辆\t依维柯:"+sum6+"辆");
System.out.println("总载客数:"+(sum1*4+sum2*4+sum3*4+sum4*20));
System.out.println("总载货吨数:"+(sum3*2+sum5*4+sum6*20));
System.out.println("总金额数:"+(sumCount(sum1,days)*500+sumCount(sum2,days)*400+
sumCount(sum3,days)*450+sumCount(sum4,days)*800+sumCount(sum5,days)*400+
sumCount(sum6,days)*1000));
}else{System.exit(0);/*System.exit(int status)是中止当前虚拟机的运行
,也就是强制性的推出程序。那个参数是状态码,0表示正常退出程序,
其他值表示异常退出。*/}
}
//计算租车数量乘以天数,略有不足,未把金额变为参数3
public static int sumCount(int sum,int days) {
// TODO Auto-generated method stub
int sums=sum*days;
return sums;
}
父类Car:
public class Car {
int pasCapacity;//载客量Passenger Capacity、人
int freCapacity;//载货量Freight Capacity、吨
int rent;//日租金、元
int rentDay;//租用天数、天
int num;//车序号
String name;//车名
public void getNumber(int num,String name,int rent,int pasCapacity,int freCapacity) {
// TODO Auto-generated method stub
this.num=num;
this.name=name;
this.rent=rent;
this.pasCapacity=pasCapacity;
this.freCapacity=freCapacity;
}
@Override
public String toString() {
return num+".\t"+name+"\t"+rent+"\t载客量"+pasCapacity+"\t载货量"+freCapacity ;
}
}
子类Carriage:
public class Carriage extends Car {
@Override
public void getNumber(int num, String name, int rent, int pasCapacity, int freCapacity) {
// TODO Auto-generated method stub
super.getNumber(num, name, rent, pasCapacity, freCapacity);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return num+".\t"+name+"\t"+rent+"\t载客量"+pasCapacity;
}
子类Truck:
public class Truck extends Car {
@Override
public void getNumber(int num, String name, int rent, int pasCapacity, int freCapacity) {
// TODO Auto-generated method stub
super.getNumber(num, name, rent, pasCapacity, freCapacity);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return num+".\t"+name+"\t"+rent+"\t载货量"+freCapacity ;
}
}
子类PickUp:
public class PickUp extends Car {
@Override
public void getNumber(int num, String name, int rent, int pasCapacity, int freCapacity) {
// TODO Auto-generated method stub
super.getNumber(num, name, rent, pasCapacity, freCapacity);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return num+".\t"+name+"\t"+rent+"\t载客量"+pasCapacity+"\t载货量"+freCapacity ;
}
}