你的位置:首页 > Java教程

[Java教程]简单的租车系统


完成了一个简单的租车系统。实现简单的租车功能,用户选择租车序号和每辆车所租的天数,计算出租车所需要的费用。

/* * 使用gbk编码 */package adad;import java.util.Scanner;import javax.print.DocFlavor.INPUT_STREAM;import org.omg.CORBA.PUBLIC_MEMBER;/* * 创建汽车类 */abstract class car{	public void man(){		System.out.println("");	}	public void huo() {		System.out.println("");	}	public abstract void Jisuan(int day);} /* * 创建一个汽车类的子类 */class aodi extends car{	int xuhao=1;  //设置第一辆车的序号	String name="奥迪A4";	int price=500; //设置汽车的租金	int rongliang=4; //设置汽车的容量	double money;	public void man(){		System.out.println(xuhao+" "+name+"  "+price+"元/天"+"  载人:"+rongliang+"人");	}	public void Jisuan(int day) {		money =price*day;	}}class mazida extends car{	int xuhao=2;	String name="马自达6";	int price=400;	int rongliang=4;	double money;	public void man(){		System.out.println(xuhao+" "+name+" "+price+"元/天"+"  载人:"+rongliang+"人");	}	public void Jisuan(int day) {		 money =price*day;	}}class pika extends car{	int xuhao=3;	String name="皮卡";	int price=400;	int rongliang=4;	int huoliang=2;	double money;	public void man(){		System.out.println(xuhao+" "+name+"       "+price+"元/天"+"  载人:"+rongliang+"人"+" 载货:"+huoliang+"吨");	}	public void Jisuan(int day) {		 money =price*day;	}}class jinlong extends car{		int xuhao=4;		String name="金龙";		int price=800;		int rongliang=20;		double money;		public void man(){			System.out.println(xuhao+" "+name+"       "+price+"元/天"+"  载人:"+rongliang+"人");		}		public void Jisuan(int day) {		 money =price*day;		}	}		class songhuajiang extends car{		int xuhao=5;		String name="松花江";		int price=400;		int huoliang=4;		double money;		public void huo(){			System.out.println(xuhao+" "+name+"    "+price+"元/天"+"  载货:"+huoliang+"吨");		}	public void Jisuan(int day) {		 money =price*day;	}}class yiweike extends car{	int xuhao=6;	String name="依维柯";	int price=1000;	int huoliang=20;	double money;	public void huo(){		System.out.println(xuhao+" "+name+"    "+price+"元/天"+" 载货:"+huoliang+"吨");	}public void Jisuan(int day) {	 money =price*day;}}public class dadadad {	public static void main(String[] args) {		aodi ao=new aodi();mazida ma=new mazida();pika pi=new pika();jinlong jin=new jinlong();songhuajiang song=new songhuajiang();yiweike yi=new yiweike();		Scanner scan=new Scanner(System.in);		System.out.println("欢迎使用租车系统:");		System.out.println("您是否租车:1是 0不是");		int sum=scan.nextInt();		if (sum==1) {			System.out.println("您可租车的类型及价目:");			System.out.println("序号  "+"汽车名称  "+"  租金    "+" 容量");			ao.man();			ma.man();			pi.man();			jin.man();			song.huo();			yi.huo();		}else {			System.out.println("谢谢!");			System.exit(0);		}		System.out.println("请输入需要租车的数量:");		int much=scan.nextInt();		for(int i=1;i<=much;i++){			System.out.println("请输入第"+i+"辆车的编号:");			int Id=scan.nextInt();			System.out.println("请输入第"+i+"辆车的租车天数:");			int day=scan.nextInt();			switch (Id) {			case 1:				ao.Jisuan(day);				break;			case 2:				ma.Jisuan(day);				break;			case 3:				pi.Jisuan(day);				break;			case 4:				jin.Jisuan(day);				break;			case 5:				song.Jisuan(day);				break;			case 6:				yi.Jisuan(day);				break;			}						}		double money=ao.money+ma.money+pi.money+jin.money+song.money+yi.money;		System.out.println("***********************");		System.out.println("租车总价为:"+money);	}}

 实现结果如下: