自己写的答答租车的系统的代码

来源:12-1 综合练习

慕粉3432276

2016-06-19 16:07

父类
package DaDa;

public abstract class Car {
	private String carName;
	private int price;
	private int peopleNum;
	private double weight;
	public String getCarName() {
		return carName;
	}
	public void setCarName(String carName) {
		this.carName = carName;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public int getPeopleNum() {
		return peopleNum;
	}
	public void setPeopleNum(int peopleNum) {
		this.peopleNum = peopleNum;
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
	
}

子类——1

package DaDa;


public class passengerCar extends Car {

public passengerCar(String carName,int price,int peopleNum,double weight){

this.setCarName(carName);

this.setPrice(price);

this.setPeopleNum(peopleNum);

this.setWeight(weight);

}

}

子类——2

package DaDa;


public class pickUp extends Car {

public pickUp(String carName,int price,int peopleNum,double weight){

this.setCarName(carName);

this.setPrice(price);

this.setPeopleNum(peopleNum);

this.setWeight(weight);

}

}

子类——3

package DaDa;


public class truck extends Car {

public truck(String carName,int price,int peopleNum,double weight){

this.setCarName(carName);

this.setPrice(price);

this.setPeopleNum(peopleNum);

this.setWeight(weight);

}

}

main()测试类

ackage DaDa;

import java.util.*;

public class DaDaTaxi {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner in=new Scanner(System.in);

Car[] carList={new passengerCar("奥迪A4",500,4,0),new passengerCar("马自达6",400,4,0),new pickUp("皮卡雪",450,4,2),new passengerCar("金龙",800,40,0),new truck("松花江",400,0,4),new truck("柯唯一",1000,0,20)};

System.out.println("请问是否需要租车:1,租车/0,不租车");

int input=in.nextInt();

if(input==1){

System.out.println("您可以租赁的车辆及相关信息:");

System.out.println("序号\t汽车名称\t租金\t载客量\t载货量");

for(int i=0;i<carList.length;i++){

System.out.println((i+1)+"\t"+carList[i].getCarName()+"\t"+carList[i].getPrice()+"元/天"+"\t"+carList[i].getPeopleNum()+"人"+"\t"+carList[i].getWeight()+"吨");

}

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

int num=in.nextInt();

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

int days=in.nextInt();

int allMoney=0;

int allPeople=0;

double allWeight=0;

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

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

int nums=in.nextInt();

int money=carList[nums-1].getPrice()*days;

allMoney+=money;

int people=carList[nums-1].getPeopleNum();

allPeople+=people;

double weight=carList[nums-1].getWeight();

allWeight+=weight;

}

System.out.println("总金额为:"+allMoney);

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

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

}else{

System.out.println("信息错误");

System.exit(0);

}

}


}


写回答 关注

2回答

  • 恋亭上邪
    2016-06-21 00:44:35
    已采纳

    赞一个

    慕粉3432...

    非常感谢!

    2016-06-23 22:36:51

    共 1 条回复 >

  • 小幕1314
    2016-07-26 18:55:22

    父类里面用了封装是吗,我看到有的没有用,封装那一章没怎么看懂,给赞一个

Java入门第二季 升级版

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

530655 学习 · 6091 问题

查看课程

相似问题