package 答答租车系统;
//父类
public class Car{
String name="sdfs";//名字
int price;//租金
int capPerson;//载人量
int capGoods;//载货量
public void show(){
System.out.println(this.name+"\t"+this.price+"元/天\t\t"+this.capGoods+"吨\t\t"+this.capPerson+"人");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getCapPerson() {
return capPerson;
}
public void setCapPerson(int capPerson) {
this.capPerson = capPerson;
}
public int getCapGoods() {
return capGoods;
}
public void setCapGoods(int capGoods) {
this.capGoods = capGoods;
}
}
package 答答租车系统;
public class Auto extends Car {//只能载人的车
public Auto(String name,int price,int capPerson){
this.setName(name);
this.setPrice(price);
this.setCapPerson(capPerson);
this.setCapGoods(0);
}
public void setCapGoods(int capGoods) {
if(capGoods!=0)System.out.println("该值默认为0,不允许修改");
else this.capGoods = capGoods;
}
}
package 答答租车系统;
public class Truck extends Car {//只能载货的车
public Truck(String name,int price,int capGoods){
this.setName(name);
this.setPrice(price);
this.setCapGoods(capGoods);
this.setCapPerson(0);
}
public void setCapPerson(int capPerson) {
if(capPerson!=0)System.out.println("该值默认为0,不允许修改");
else this.capPerson = capPerson;
}
}
package 答答租车系统;
public class Pickup extends Car {//既能载人又能载货
public Pickup(String name,int price,int capPerson,int capGoods){
this.setName(name);
this.setPrice(price);
this.setCapPerson(capPerson);
this.setCapGoods(capGoods);
}
}
package 答答租车系统;
import java.util.Scanner;
//测试类
public class Test {
public static void main(String args[]){
int IF=2;//控制用户是否租车的变量
//初始化车辆信息
Car[] allRent = {new Auto("奥迪A4",500,4),new Auto("马自达6",400,4),new Pickup("皮卡雪6",450,4,2),
new Auto("金龙 ",800,20),new Truck("松花江",400,4),new Truck("依维河",1000,20)};
System.out.println("欢迎来到嗒嗒租车系统!");
System.out.println("输入1进入租车系统,输入0退出系统!");
while(IF==2){
System.out.println("请输入:");
Scanner input=new Scanner(System.in);
IF=input.nextInt();
if(IF==1){
int i,id,sum;
int priceSum=0,personSum=0,goodsSum=0;//租车总价格,载人/货量
//输出可供租借的车辆信息
System.out.println("\n"+"您可以租用的车辆信息如下:");
System.out.println("序号\t"+"款式\t\t"+"价格 \t\t\t"+"载货量\t"+"载人量\t ");
for( i=0;i<allRent.length;i++){
System.out.print((i+1)+".\t");
allRent[i].show();
}
//获得租车的数量,并放入chooseCar[]数字
System.out.println("请输入您要租车的数量:");
sum=input.nextInt();
Car[] chooseCar=new Car[sum];
int[] day=new int[sum];
//租车序号及各自天数
for(i=0;i<sum;i++){
System.out.print("请输入您要租的第"+(i+1)+"辆车的序号:");
id=input.nextInt()-1;
chooseCar[i]=allRent[id];
System.out.print("请输入您要租用该车的天数:");
day[i]=input.nextInt();
}
//最终租车信息列表
System.out.println("您共租用了以下车辆");
for(i=0;i<sum;i++){
priceSum+=chooseCar[i].getPrice()*day[i];
personSum+=chooseCar[i].getCapPerson();
goodsSum+=chooseCar[i].getCapGoods();
System.out.print((i+1)+".\t"+day[i]+"天"+"\t");
chooseCar[i].show();
}
System.out.println("您所租的车辆租金共计:"+priceSum);
System.out.println("您所租的车辆载人量共计:"+personSum);
System.out.println("您所租的车辆载货量共计:"+goodsSum);
}
else if(IF==0) System.out.println("您已经退出租车系统,欢迎再次使用!");
else {
System.out.println("您输入的为无效信息,请重新输入!");
IF=2;
}
input.close();
}
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章