自己的刚写的代码 写的不好 但是可以给幕友作为借鉴
好久没有敲代码了 变得生疏和脑子不够用了 用慕课来重温下知识点还是很好的 这是我在看了老师解题所给出的部分代码后自己补上的 每个类都用横线分隔开了 主要由5个类组成 一个父类Car 三个子类Truck、PickUp、PassengerCar 和一个主函数类Main组成 本人写的代码很烂 写在这里只是给更多幕友一个参考的地方 也说不上参考就是给大家随便看下
-------------------------
package com.demo_h;
public class Car {
protected String name;
protected double rent;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getRent() {
return rent;
}
public void setRent(double rent) {
this.rent = rent;
}
}
-----------------------------------
package com.demo_h;
public class Truck extends Car {
private double cargoCapacity;
public Truck(String name,double rent,double cargoCapacity)
{
this.name = name;
this.rent = rent;
this.setCargoCapacity(cargoCapacity);
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
}
--------------------------------
package com.demo_h;
public class PickUp extends Car {
private double peopleCapacity;
private double cargoCapacity;
public PickUp(String name,double rent,double peopleCapacity,double cargoCapacity)
{
this.name = name;
this.rent = rent;
this.setPeopleCapacity(peopleCapacity);
this.setCargoCapacity(cargoCapacity);
}
public void setCargoCapacity(double cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
public double getCargoCapacity() {
return cargoCapacity;
}
public void setPeopleCapacity(double peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
public double getPeopleCapacity() {
return peopleCapacity;
}
}
---------------------------------
package com.demo_h;
public class PassengerCar extends Car {
private double peopleCapacity;
public PassengerCar(String name,double rent,double peopleCapacity)
{
this.name = name;
this.rent = rent;
this.peopleCapacity = peopleCapacity;
}
public double getPeopleCapacity() {
return peopleCapacity;
}
public void setPeopleCapacity(double peopleCapacity) {
this.peopleCapacity = peopleCapacity;
}
}
------------------------------------
package com.demo_h;
import java.util.Scanner;
public class Main {
/*
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//用父类数组指向子类实例化对象,并且用构造方法赋值
Car[] carsForRent = {new PassengerCar("奥迪", 500, 4),new PassengerCar("马自达", 400, 4),
new PassengerCar("金龙", 800, 20),new PickUp("皮卡雪", 450, 4, 2),
new Truck("松花江", 400, 4),new Truck("依维柯", 1000, 20)};
System.out.println("****欢迎来到答答租车系统****");
System.out.println("你是否要租车:1是 0否");
Scanner scan = new Scanner(System.in);
String input = scan.next();
if(input.equals("1"))
{
System.out.println("你可租车的类型及其价目表:");
System.out.println("序号\t汽车名称\t租金\t容量");
int i = 1;
//显示所有的车型
for(Car currentCar:carsForRent)//foreach循环显示数组内容
{
if(currentCar instanceof PassengerCar)//判断是否指向当前子类对象,然后进行向下转型
{
System.out.println(i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()
+"元/天\t载人"+(int)((PassengerCar) currentCar).getPeopleCapacity()+"人");
i++;
}
if(currentCar instanceof Truck)
{
System.out.println(i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()
+"元/天\t载货"+((Truck) currentCar).getCargoCapacity()+"吨");
i++;
}
if(currentCar instanceof PickUp)
{
System.out.println(i+"\t"+currentCar.getName()+"\t"+currentCar.getRent()
+"元/天\t载人"+(int)((PickUp) currentCar).getPeopleCapacity()+"人.载货"+
((PickUp) currentCar).getCargoCapacity()+"吨");
i++;
}
}
System.out.println("请输入您要汽车的数量");
int carNum = scan.nextInt();
int accout = 0;
int peopleNum = 0;
int cargoNum = 0;
Car[] carsForRent01 = new Car[carNum]; //此数组方便为打印车辆名字而建
if(carNum>0&&carNum<7)//限制汽车数量
{
for(int a=0;a<carNum;a++)
{
System.out.println("请输入第"+(a+1)+"辆车的序号");
int num = scan.nextInt();
switch (num) { //用switch语句实现选择以及对金额、载客量和数组的赋值
case 1:
accout = accout+500;
peopleNum = peopleNum+4;
carsForRent01[a] = carsForRent[0];
break;
case 2:
accout = accout+400;
peopleNum = peopleNum+4;
carsForRent01[a] = carsForRent[1];
break;
case 3:
accout = accout+450;
peopleNum = peopleNum+4;
cargoNum = cargoNum+2;
carsForRent01[a] = carsForRent[2];
break;
case 4:
accout = accout+800;
peopleNum = peopleNum+20;
carsForRent01[a] = carsForRent[3];
break;
case 5:
accout = accout+400;
cargoNum = cargoNum+4;
carsForRent01[a] = carsForRent[4];
break;
case 6:
accout = accout+1000;
cargoNum = cargoNum+20;
carsForRent01[a] = carsForRent[5];
break;
default:
System.err.println("输入车辆序号不符合标准!");
break;
}
}
}
else
{
System.out.println("输入汽车数量过大或则不符合标准!");
}
System.out.println("*****你的账单*****");
System.out.println("---可载客的车有---");
for(Car currentCar:carsForRent01) //输出可载客车的名字 注意进行对象比较
{
if(currentCar instanceof PassengerCar)
{
System.out.print(currentCar.getName()+" ");
}
}
System.out.println("共载人:"+peopleNum+"人");
System.out.println("---可载货的车有---");
for(Car currentCar:carsForRent01)
{
if((currentCar instanceof Truck)||(currentCar instanceof PickUp))
{
System.out.print(currentCar.getName()+" ");
}
}
System.out.println("共载货:"+cargoNum+"吨");
System.out.println("租车总价格:"+accout+"¥");
}
else
{
System.out.println("退出系统 欢饮下次光临!");
System.exit(-1);
}
}
}