测试类:
package com.carcrental;
import java.util.Scanner;
public class Test {
private static final String NULL = null;
private static Scanner input;
public static void main(String[] args) {
// TODO Auto-generated method stub
for(;;){
System.out.println("欢迎使用答答租车系统!");
System.out.println("您是否需要租车:1是 0否");
input = new Scanner(System.in);
int i=input.nextInt();
if(i==1){
System.out.println("您可租车的类型及其价目表:");
Car[] carForRent={new PersonCar(1,"奥迪A4",500,4),new PersonCar(2,"马自达6",400,4),
new Pickup(3,"皮卡雪6",450,4,2),new PersonCar(4," 金龙 ",800,20),
new Truck(5,"松花江",400,4),new Truck(6,"依维柯",1000,20)};
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"容量");
//遍历Car类
for(Car car : carForRent){
if(car instanceof PersonCar){
//若它是客车类型的实例,则打印出客车应该有的属性值,这里注意强转,否则不会出现
//passengerCar类的CarCapacity属性
System.out.println(car.getCarNumber()+"\t"+car.getCarName()+"\t"+car.getCarPrice()
+"/天"+"\t"+"载人:"+((PersonCar)car).getCarCapacity()+"人");
}
if(car instanceof Pickup){
System.out.println(car.getCarNumber()+"\t"+car.getCarName()+"\t"+car.getCarPrice()
+"/天"+"\t"+"载人:"+((Pickup)car).getCarCapacity()+"人"+"\t"
+"载货:"+((Pickup)car).getCarrayingCapacity()+"吨");
}
if(car instanceof Truck){
System.out.println(car.getCarNumber()+"\t"+car.getCarName()+"\t"+car.getCarPrice()
+"/天"+"\t"+"载货:"+((Truck)car).getCarryingCapacity()+"吨");
}
}
System.out.print("请输入你租车的数量:");
int totalNum=input.nextInt(); //用于接收汽车的总数量
String[] name=new String[totalNum];
String[] name2=new String[totalNum];
int capacitySum=0;//总人数
int ccapacitySum=0;//总载货量
int sum=0;//用于接收租车的总价格
for(int i1=0;i1<totalNum;i1++){
System.out.print("请输入第"+(i1+1)+"辆车的序号:");
int num=input.nextInt();
System.out.print("请输入租这辆车的天数:");
int day=input.nextInt();
switch(num){
case 1:sum=sum+carForRent[num-1].getCarPrice()*day;
capacitySum=capacitySum+4;
break;
case 2:sum=sum+carForRent[num-1].getCarPrice()*day;
capacitySum=capacitySum+4;
break;
case 3:sum=sum+carForRent[num-1].getCarPrice()*day;
capacitySum=capacitySum+4;
ccapacitySum=ccapacitySum+2;
break;
case 4:sum=sum+carForRent[num-1].getCarPrice()*day;
capacitySum=capacitySum+20;
break;
case 5:sum=sum+carForRent[num-1].getCarPrice()*day;
ccapacitySum=ccapacitySum+4;
break;
case 6:sum=sum+carForRent[num-1].getCarPrice()*day;
ccapacitySum=ccapacitySum+20;
break;
}
if(carForRent[num-1] instanceof PersonCar){
name[i1]=carForRent[num-1].getCarName();
}else if(carForRent[num-1] instanceof Pickup){
name[i1]=carForRent[num-1].getCarName();
name2[i1]=carForRent[num-1].getCarName();
}else if(carForRent[num-1] instanceof Truck){
name2[i1]=carForRent[num-1].getCarName();
}
}
System.out.println("载人的车有:");
for(String n:name){
if(n==NULL){
continue;
}
System.out.print(n);
System.out.print("\t");
}
System.out.println("***可以乘坐人数:"+capacitySum+"人");
for(String n2:name2){
if(n2==NULL){
continue;
}
System.out.print(n2);
System.out.print("\t");
}
System.out.println("***可载货:"+ccapacitySum+"吨");
System.out.println("***你一共需要支付:"+sum);
}else if(i==0){
System.out.println("欢迎下次使用!");break;
}
else{
System.out.println("你输入有误!请重新输入!");
}
System.out.println("\n");
System.out.println("\n");
}
}
/*if(i==1){
System.out.println("序号 汽车名称 租金 容量");
System.out.println(1+"\t"+p1.getCarName()+"\t"+p1.getCarPrice()+"元/天"+"\t"+"载人:"+p1.getCarCapacity()+"人");
System.out.println(2+"\t"+p2.getCarName()+"\t"+p2.getCarPrice()+"元/天"+"\t"+"载人:"+p2.getCarCapacity()+"人");
System.out.println(3+"\t"+p3.getCarName()+"\t"+p3.getCarPrice()+"元/天"+"\t"+"载人:"+p3.getCarCapacity()+"人;"+"载货:"+p3.getCarrayingCapacity()+"吨");
System.out.println(4+"\t"+p4.getCarName()+"\t"+p4.getCarPrice()+"元/天"+"\t"+"载人:"+p4.getCarCapacity()+"人");
System.out.println(5+"\t"+p5.getCarName()+"\t"+p5.getCarPrice()+"元/天"+"\t"+"载货:"+p5.getCarryingCapacity()+"吨");
System.out.println(6+"\t"+p6.getCarName()+"\t"+p6.getCarPrice()+"元/天 载货:"+p6.getCarryingCapacity()+"吨");
}
System.out.print("请输入你租车的数量:");
int a=input.nextInt();
int sum=0;
for(int b=0;b<a;b++){
System.out.print("请输入你想租的车的序号:");
int c=input.nextInt();
System.out.print("请输入租这辆车的天数:");
int day=input.nextInt();
switch(c){
case 1:sum=sum+p1.getCarPrice()*day;break;
case 2:sum=sum+p2.getCarPrice()*day;break;
case 3:sum=sum+p3.getCarPrice()*day;break;
case 4:sum=sum+p4.getCarPrice()*day;break;
case 5:sum=sum+p5.getCarPrice()*day;break;
case 6:sum=sum+p6.getCarPrice()*day;break;
}
}
System.out.println("你一共需要支付:"+sum);*/
}
Car类:
package com.carcrental;
//所有汽车的父类
public class Car {
private int carNumber;
private String carName;
private int carPrice;
public Car(int carNumber,String carName,int carPrice){
this.carNumber=carNumber;
this.carName=carName;
this.carPrice=carPrice;
}
public int getCarNumber() {
return carNumber;
}
public void setCarNumber(int carNumber) {
this.carNumber = carNumber;
}
public String getCarName() {
return carName;
}
public void setCarName(String carName) {
this.carName = carName;
}
public int getCarPrice() {
return carPrice;
}
public void setCarPrice(int carPrice) {
this.carPrice = carPrice;
}
}
载人车类:
package com.carcrental;
public class PersonCar extends Car{
private int carCapacity;
public PersonCar(int carNumber,String carName,int carPrice,int carCapacity){
super(carNumber,carName,carPrice);
/*this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarPrice(carPrice);*/
this.carCapacity=carCapacity;
}
public int getCarCapacity() {
return carCapacity;
}
public void setCarCapacity(int carCapacity) {
this.carCapacity = carCapacity;
}
}
皮卡车:
package com.carcrental;
//皮卡类,继承了父类Car
public class Pickup extends Car{
private int carCapacity;
private int carrayingCapacity;
//构造函数
public Pickup(int carNumber,String carName,int carPrice,int carCapacity,int carrayingCapacity){
super(carNumber,carName,carPrice);
/*this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarPrice(carPrice);*/
this.carCapacity=carCapacity;
this.carrayingCapacity=carrayingCapacity;
}
//封装过后必须有出口
public int getCarCapacity() {
return carCapacity;
}
public void setCarCapacity(int carCapacity) {
this.carCapacity = carCapacity;
}
public int getCarrayingCapacity() {
return carrayingCapacity;
}
public void setCarrayingCapacity(int carrayingCapacity) {
this.carrayingCapacity = carrayingCapacity;
}
}
货车:
package com.carcrental;
public class Truck extends Car{
private int carryingCapacity;
public Truck(int carNumber,String carName,int carPrice,int carryingCapacity){
super(carNumber,carName,carPrice);
/*this.setCarNumber(carNumber);
this.setCarName(carName);
this.setCarPrice(carPrice);*/
this.carryingCapacity=carryingCapacity;
}
public int getCarryingCapacity() {
return carryingCapacity;
}
public void setCarryingCapacity(int carryingCapacity) {
this.carryingCapacity = carryingCapacity;
}
}
是Java语言写的吧,果然是很长。
phper表示看不了这么臃肿的代码。精简为此代码的1/4,应该不是难事。
赞,写了个这么长的代码,可以借鉴借鉴
感谢楼主
非常感谢
我怎么看了一遍视频到现在还是编不出这样的diamante
非常感谢楼主分享学习的代码
我写不出来 谢谢楼主发出来参考
这种基础练习代码有什么可改进的,真正的实际开发不会出现这样的代码,我在公司看了一天的代码,早看吐了,没心情再看你这么长的代码。。。
要我们看什么?是有bug吗?
赞一个