花了一个小时做好了

来源:12-1 综合练习

只想敲个代码

2019-03-03 16:55

父类
package com.liuxiaodao;
public class Vehicle {	
public int carNumber;//车序列	
public String carName; //车名字	
public int rent;//租金	
public int personCapacity;//载客量	
public double freightVolume;//载货量
public String getCarName() {		
return carName;	}	
public void setCarName(String carName) {		
this.carName = carName;	}	
public int getRent() {		
return rent;	}	
public void setRent(int rent) {		
this.rent = rent;	}	
public int getCarNumber() {		
return carNumber;	}	
public void setCarNumber(int carNumber) {		
this.carNumber = carNumber;	}	
public int getPersonCapacity() {		
return personCapacity;	}	
public void setPersonCapacity(int personCapacity) {		
this.personCapacity = personCapacity;	}	
public double getFreightVolume() {		
return freightVolume;	}	
public void setFreightVolume(double freightVolume) {		
this.freightVolume = freightVolume;	}	
public void show() {		
// TODO Auto-generated method stub			}	
}

Passengercar类
package com.liuxiaodao;
public class Passengercar extends Vehicle {	
public Passengercar(int carNumber,String carName,int rent,int personCapacity) {		
this.setCarNumber(carNumber);		
this.setCarName(carName);		
this.setRent(rent);		
this.setPersonCapacity(personCapacity);	}		
public void show() {		
System.out.println( " "+carNumber + "\t"+carName +"\t" + rent +"一天" + "\t\t载客量" + personCapacity+"人");	}
}

 Pika类
public class Pika extends Vehicle{	
public Pika(int carNumber,String carName,int rent,int personCapacity,double freightVolume) {		
this.setCarName(carName);		
this.setCarNumber(carNumber);		
this.setRent(rent);		
this.setPersonCapacity(personCapacity);		
this.setFreightVolume(freightVolume);}	
public void show() {		
System.out.println(" "+carNumber + "\t"+carName +"\t" + rent + "一天" + "\t\t载客量" + personCapacity+ "人"+ "载货量" +freightVolume+"吨");	}}


Truck类

public class Truck extends Vehicle {

public Truck(int carNumber,String carName,int rent,double freightVolume) {

this.setCarName(carName);

this.setCarNumber(carNumber);

this.setRent(rent);

this.setFreightVolume(freightVolume);

}

public void show() {

System.out.println(" "+carNumber +"\t"+ carName +"\t" + rent +"一天" + "\t\t载货量" + freightVolume+"吨");

}

}

Text类

public class Text {


public static void main(String[] args) {

// TODO Auto-generated method stub

Vehicle a = new Passengercar(1,"奥迪A4",500,4);

Vehicle b = new Passengercar(2,"宝马3系",400,4);

Vehicle c = new Pika(3,"皮卡雪6",450,4,2);

Vehicle d = new Passengercar(4,"金龙",800,20);

Vehicle e = new Truck(5,"松花江",400,4);

Vehicle f = new Truck(6,"依维柯",1000,20);

Vehicle vehicle[] = {a,b,c,d,e,f,};

System.out.println("欢迎使用小叨租车系统:");

System.out.println("你是否需要租车:1是 0否");

Scanner input = new Scanner(System.in);//使用Scanner来实现用户输入

int in =input.nextInt();

if(in == 1) {

System.out.println("您可租车的类型及其价目表:");

System.out.println("编 号"+"\t  类 型"+"\t日租金/天"+"\t\t容量");

a.show();

b.show();

c.show();

d.show();

e.show();

f.show();

System.out.println("请输入您租车的数量:");

Scanner input2 = new Scanner(System.in);

int num = input2.nextInt();

System.out.println("请输入您要借租的天数");

Scanner input3 = new Scanner(System.in);

int day =input3.nextInt();

for(int i =0;i<num;i++) {

System.out.println("请输入第"+ (i+1) +"辆车的序号");

Scanner input4 = new Scanner(System.in);

int xuhao = input4.nextInt();

vehicle[i] =vehicle[xuhao-1];

}

System.out.println("统计完成\n您的账单如下:");

System.out.println("可以载人的有:");

for(int n=0;n<num;n++) {

if(vehicle[n].personCapacity != 0) {

System.out.println(vehicle[n].carName);

}

}

System.out.println("可以载货的有:");

for(int m=0;m<num;m++) {

if(vehicle[m].freightVolume !=0) {

System.out.println(vehicle[m].carName);

}

}

//统计账单金额数目

int sumPerson = 0;

double sumFreight = 0;

double sumRent = 0;

for(int x=0;x<num;x++) {

sumPerson =vehicle[x].personCapacity+sumPerson;

sumFreight = vehicle[x].freightVolume+sumFreight;

sumRent = vehicle[x].rent+sumRent;

}

System.out.println("总载客量:"+sumPerson);

System.out.println("总载货量:"+sumFreight);

System.out.println("总价格:"+sumRent*day);

}else if(in == 0) {

System.out.println("再见,欢迎您下次使用!");

}

}


}

https://img3.mukewang.com/5c7b96620001771505860663.jpg

写回答 关注

8回答

  • xuhao47957
    2019-03-30 18:40:34

    vehicle[i] =vehicle[xuhao-1];

    这段是错,会覆盖掉原有保存的汽车数据

    应该再创建一个数组

    all[] cars = new all[num];

    然后在下面把vehicle[i] =vehicle[xuhao-1];替换成cars[i] = all[xuhao-1];

    才没有问题

  • 慕无忌7406412
    2019-03-26 21:16:37

    你并不需要每次都去new一个Scanner对象,就用第一次实例化的哪个对象给变量赋值就可以了。如:

    int a =input.nextInt();

    int b=input.nextInt();

    等等都是可以的。

    只想敲个代码

    嗯 是的

    2019-03-26 21:32:27

    共 1 条回复 >

  • 西与西寻
    2019-03-21 09:22:23

    父类无需get/setter方法也可完成,没有私有变量

  • 鹿目圆Madoka
    2019-03-14 23:11:34

    这个输入的序号超过数组长度不行啊 会报错

  • 慕仰6381899
    2019-03-11 20:57:57

    vehicle[i] =vehicle[xuhao-1];

    这样真的没有问题吗?

  • 慕无忌7441607
    2019-03-08 22:05:11

    请问大神,你完成这个项目的思路是怎样的,是和老师说的一样吗?如果不是你的思路是怎样的?请指教。我想不到这么多代码,写着写着就乱了

  • 慕瓜5559391
    2019-03-04 23:19:21

    public Passengercar(int carNumber,String carName,int rent,int personCapacity) {      

    this.setCarNumber(carNumber);      

    this.setCarName(carName);      

    this.setRent(rent);    

    this.setPersonCapacity(personCapacity); } 

    兄弟,这个是重载吗?还是啥,以前的课有讲吗

    只想敲个代码

    这和this.CarNumber = carNumber 的效果是一样的,只是写法不同 直接赋值也可以,看个人习惯

    2019-03-05 23:04:32

    共 1 条回复 >

  • 慕后端3187882
    2019-03-04 20:35:50

    tqltql。

Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530099 学习 · 6086 问题

查看课程

相似问题