手记

答答租车系统,通俗易懂

//父类

/*
里面还有一些判断输入信息的代码没写上,可自行添加使程序更加完善,还可以添加一些租客的信息
*/
package Rentalcar;
public abstract class Cars {
	private int no;
	private String name;
	private int rent;
	private int carrypeople;
	private int carrygoods;

	public  Cars(int  no,String name,int rent,int carrypeople,int carrygoods) {
		this.no=no;
		this.name=name;
		this.rent=rent;
		this.carrypeople=carrypeople;
		this.carrygoods=carrygoods;
	}


	public String getName() {
		return name;
	}

	public int getRent() {
		return rent;
	}

	public int getCarrypeople() {
		return carrypeople;
	}

	public int getCarrygoods() {
		return carrygoods;
	}
	
	public int getNo() {
		return no;
	}

	public abstract void Print();

}
//子类 PersonCars
package Rentalcar;

public class PersonCars extends Cars {
public PersonCars(int no, String name, int rent, int carrypeople, int carrygoods) {
		super(no, name, rent, carrypeople, 0);	
	}

	public void Print() {
		System.out.println(super.getNo()+".    "+super.getName()+"\t"+super.getRent()+"/天\t    载人:"
				           +super.getCarrypeople()+"人");
	}
	
}
//子类Trunk
package Rentalcar;

public class Trunk extends Cars {

	public Trunk(int no,String name, int rent,  int carrygoods) {
		super(no,name, rent,0, carrygoods);
		
	}

	public void Print() {
		System.out.println(super.getNo()+".    "+super.getName()+"\t"+super.getRent()+"/天\t    载货:"
		           +super.getCarrygoods()+"吨");
		
	}

}

//子类PiKa
package Rentalcar;

public class PiKa extends Cars {

	public PiKa(int no ,String name, int rent, int carrypeople, int carrygoods) {
		super(no ,name, rent, carrypeople, carrygoods);
		
	}
	public void Print() {
		System.out.println(super.getNo()+".    "+super.getName()+"\t"+super.getRent()+"/天\t    载人:"
		           +super.getCarrypeople()+"人  载货:"+super.getCarrygoods()+"吨");
	}

}

//测试
package Rentalcar;
import java.util.*;
public class Test {

	public static void main(String[] args) {
		int sumMoney=0;//总租金
		int sumPeople=0;//总载人
		int sumGoods=0;//总载货
	
	Cars carForRent[]= {new PersonCars(1,"奥迪A4",500,4, 0),
						new PersonCars(2,"马自达6",400,4,0),
						new PersonCars(3,"金龙",800,20,0),
						new Trunk(4,"松花江",400,4),
						new Trunk(5,"依维柯",1000,20),
						new PiKa(6,"皮卡雪",450,4,4)
						};
     
	System.out.println("欢迎使用答答租车系统:");
	System.out.println("您是否要租车:\n     提示:租车输入1  不租车输入0");
	Scanner input=new Scanner(System.in);
	System.out.print("请输入:");
	String num=input.next();
	
	if(num.equals("1")) {
		System.out.println("您可租车的类型及其价目表:");
		System.out.println("序号    汽车名称\t租金\t\t容量");
		for(int i=0;i<carForRent.length;i++)
			carForRent[i].Print();
		System.out.print("请输入您要租车的数量: ");
		int  num2=input.nextInt();
		int Array[]=new int [num2];
		for(int i=0;i<num2;i++) {
			System.out.print("请输入第"+(i+1)+"辆车的序号:  ");
			int  num3=input.nextInt();
			num3-=1;
			Array[i]=num3;
		}
		System.out.print("请输入租车天数:");
		int  days=input.nextInt();
		for(int i=0;i<Array.length;i++) {
			sumPeople+=carForRent[Array[i]].getCarrypeople();//保存总载人数
			sumGoods+=carForRent[Array[i]].getCarrygoods();//保存总载货数
			sumMoney+=carForRent[Array[i]].getRent();//保存总租金
		}
		sumMoney*=days;//总租金*总天数
		System.out.println("");
		System.out.println("请稍等。。。。。\n您的账单:");
		
		//输出载人车和载人总数
		if(sumPeople!=0) {	
		System.out.println("***可载人的车有:");
		for(int i=0;i<Array.length;i++) {
			if(Array[i]+1==4||Array[i]+1==5)
				continue;
			System.out.print("     "+carForRent[Array[i]].getName()+"   ");
				
		}
		System.out.println("共载"+sumPeople+"人");
		}
		
		//输出载货车和载货总数
		if(sumGoods!=0) {	
		System.out.println("***可载货的车有:");
		for(int i=0;i<Array.length;i++) {
			if(Array[i]+1==1||Array[i]+1==2||Array[i]+1==3)
				continue;
			System.out.print("     "+carForRent[Array[i]].getName()+"   ");
			
		}
		System.out.println("共载货:"+sumGoods+"吨");
		}
		
		//输出总租金
		System.out.println("***租车总价格:\n   "+sumMoney+"元");
	}else {
		System.out.println("\n虽然只是心动了一下,期待您的下次光临");
		
	}
	
	}

}

4人推荐
随时随地看视频
慕课网APP