这是我写的,供大家参考参考,不对之处望指证哦
(1)车类
package com.dd.zuche;
public abstract class Car {
protected String name;//车名
protected int price;//价格
protected int passengerCapcity;//载客量
protected int goodsCapcity;//载货量
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getPassengerCapcity() {
return passengerCapcity;
}
public int getGoodsCapcity() {
return goodsCapcity;
}
}
(2)货车类
package com.dd.zuche;
public class Goods extends Car{
public Goods(String name,int price,int goodsCapcity)//构造方法
{
this.name=name;
this.price=price;
this.goodsCapcity=goodsCapcity;
}
}
(3)客车类
package com.dd.zuche;
public class passenger extends Car{ //客车类
public passenger(String name,int price,int passengeerCapcity)//构造方法
{
this.name=name;
this.price=price;
this.passengerCapcity=passengeerCapcity;
}
}
(4)皮卡类
package com.dd.zuche;
public class PiKa extends Car{
public PiKa(String name,int price,int passengeerCapcity,int goodsCapcity)//构造方法
{
this.name=name;
this.price=price;
this.passengerCapcity=passengeerCapcity;
this.goodsCapcity=goodsCapcity;
}
}
(5)入口主类
package com.dd.zuche;
import java.util.*;
public class Initial
{
public static void main(String[] args)
{
/**
* 汽车信息组
*
* */
Car[]cars={
new passenger("奥迪A4",500,4),
new passenger("马自达6", 400,4),
new PiKa("皮卡雪6", 450,4,2),
new passenger("金龙", 800,20),
new Goods("松花江", 400,4),
new Goods("依维柯", 1000,20),
};
System.out.println("欢迎使用达达租车系统:");
try{
System.out.println("您是否要租车:1是,0否");
Scanner input=new Scanner(System.in);
int confirm=input.nextInt();
if(confirm==1)
{
int []n;
int []k;
System.out.println("您可以租的车型及其价目表如下:");
System.out.println("序号\t汽车名称\t租金\t容量");
for(int i=0;i<cars.length;i++)
{
System.out.print(i+1+".\t");
Initial sys=new Initial();
sys.carMessage(cars[i]);
}
try{
System.out.println("请输入需要租车数量:");
int Nums=input.nextInt();
int Carquantity[]=new int[Nums];
for(int a=0;a<Nums;a++)
{
Carquantity[a]=1;
}
n=new int[Nums];
//k=new int[Nums];
for(int i=0;i<Nums;i++)
{
System.out.println("请输入第"+(i+1)+"辆车的序号:");
int Xuhao=input.nextInt();
n[i]=Xuhao-1;
//k[i]=Xuhao-1;
}
/*for(int i=0;i<Nums;i++)
{
for(int j=1;j<Nums;j++)
{
if(k[j]==n[i])
{
Carquantity[i]++;
}
}
}*/
System.out.println("请输入租车天数:");
int Days=input.nextInt();
System.out.println("您的账单:");
System.out.println("序号"+"\t"+" 您所租车的名称:"+"\t"+"数量"+"\t"+"载客量"+"\t"+"载货量"+"\t"+" 单价"+"\t"+"租期");
for(int i=0;i<Nums;i++)
{
int j=n[i];
System.out.println("第"+(i+1)+"辆车: "+"\t"+cars[j].getName()+"\t"+"\t"+1+"\t"+cars[j].getPassengerCapcity()
+"\t"+cars[j].getGoodsCapcity()+"\t"+cars[j].getPrice()+"/天"+"\t"+Days+"天");
}
System.out.println("合计"+"\t"+" "+"\t"+"总数量"+"\t"+"总载客量"+"\t"+"总载货量"+"\t"+"总金额");
int quantitySum=0;
int passengerSum=0;
int goodSum=0;
for(int i=0;i<Nums;i++)
{
if(Carquantity[i]>0)
{
int j=n[i];
quantitySum+=1;
passengerSum+=cars[j].getPassengerCapcity();
goodSum+=cars[j].getGoodsCapcity();
}
}
int p=0;
int pricesSum;
for(int i=0;i<Nums;i++)
{
if(Carquantity[i]>0)
{
int j=n[i];
p+=Carquantity[j]*cars[j].price;
}
}
pricesSum=p*Days;
System.out.println("\t"+"\t"+quantitySum+"\t"+passengerSum+"\t"+goodSum+"\t"+pricesSum);
System.out.println();
}catch(InputMismatchException e){
System.out .println("温馨提示:首先,请确定您输入的数是正整数;其次在选择车辆时请从可租车辆序号中进行选择!");
}
}
else if(confirm==0)
{
System.out.println("谢谢光临");
return;
}
else if(confirm!=1confirm!=0)
{
System.out.println("输入有误,请再次输入");
Initial sys1=new Initial();
sys1.chongfu();
}
}catch(InputMismatchException e){
System.out .println("温馨提示:请确定您输入的数是正整数1或者0!");
return;
}
}
private void chongfu() {//如果输入一直是非1和0,则进入重复;
Car[]cars={
new passenger("奥迪A4",500,4),
new passenger("马自达6", 400,4),
new PiKa("皮卡雪6", 450,4,2),
new passenger("金龙", 800,20),
new Goods("松花江", 400,4),
new Goods("依维柯", 1000,20),
};
System.out.println("欢迎使用达达租车系统:");
try{
System.out.println("您是否要租车:1是,0否");
Scanner input=new Scanner(System.in);
int confirm=input.nextInt();
if(confirm==1)
{
int []n;
int []k;
System.out.println("您可以租的车型及其价目表如下:");
System.out.println("序号\t汽车名称\t租金\t容量");
for(int i=0;i<cars.length;i++)
{
System.out.print(i+1+".\t");
Initial sys=new Initial();
sys.carMessage(cars[i]);
}
try{
System.out.println("请输入需要租车数量:");
int Nums=input.nextInt();
int Carquantity[]=new int[Nums];
for(int a=0;a<Nums;a++)
{
Carquantity[a]=1;
}
n=new int[Nums];
//k=new int[Nums];
for(int i=0;i<Nums;i++)
{
System.out.println("请输入第"+(i+1)+"辆车的序号:");
int Xuhao=input.nextInt();
n[i]=Xuhao-1;
//k[i]=Xuhao-1;
}
/*for(int i=0;i<Nums;i++)
{
for(int j=1;j<Nums;j++)
{
if(k[j]==n[i])
{
Carquantity[i]++;
}
}
}*/
System.out.println("请输入租车天数:");
int Days=input.nextInt();
System.out.println("您的账单:");
System.out.println("序号"+"\t"+" 您所租车的名称:"+"\t"+"数量"+"\t"+"载客量"+"\t"+"载货量"+"\t"+" 单价"+"\t"+"租期");
for(int i=0;i<Nums;i++)
{
int j=n[i];
System.out.println("第"+(i+1)+"辆车: "+"\t"+cars[j].getName()+"\t"+"\t"+1+"\t"+cars[j].getPassengerCapcity()
+"\t"+cars[j].getGoodsCapcity()+"\t"+cars[j].getPrice()+"/天"+"\t"+Days+"天");
}
System.out.println("合计"+"\t"+" "+"\t"+"总数量"+"\t"+"总载客量"+"\t"+"总载货量"+"\t"+"总金额");
int quantitySum=0;
int passengerSum=0;
int goodSum=0;
for(int i=0;i<Nums;i++)
{
if(Carquantity[i]>0)
{
int j=n[i];
quantitySum+=1;
passengerSum+=cars[j].getPassengerCapcity();
goodSum+=cars[j].getGoodsCapcity();
}
}
int p=0;
int pricesSum;
for(int i=0;i<Nums;i++)
{
if(Carquantity[i]>0)
{
int j=n[i];
p+=Carquantity[j]*cars[j].price;
}
}
pricesSum=p*Days;
System.out.println("\t"+"\t"+quantitySum+"\t"+passengerSum+"\t"+goodSum+"\t"+pricesSum);
System.out.println();
}catch(InputMismatchException e){
System.out .println("温馨提示:首先,请确定您输入的数是正整数;其次在选择车辆时请从可租车辆序号中进行选择!");
}
}
else if(confirm==0)
{
System.out.println("谢谢光临");
return;
}
else if(confirm!=1confirm!=0)
{
System.out.println("输入有误,请再次输入");
Initial sys1=new Initial();
sys1.chongfu();
}
}catch(InputMismatchException e){
System.out .println("温馨提示:请确定您输入的数是正整数1或者0!");
return;
}
}
private void carMessage(Car car) {
//System.out.println();
if(car instanceof passenger)
{
System.out.println(car.name+"\t"+car.price+"/天"+"\t"+"载客:"+car.getPassengerCapcity()+"人");
}
else if(car instanceof Goods)
{
System.out.println(car.name+"\t"+car.price+"/天"+"\t"+"载货:"+car.getGoodsCapcity()+"吨");
}
else if(car instanceof PiKa)
{
System.out.println(car.name+"\t"+car.price+"/天"+"\t"+"载客:"+car.getPassengerCapcity()+"人"+" "+"载货:"+car.getGoodsCapcity()+"吨");
}
}
}
点击查看更多内容
2人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦