package 答答租车系统; //父类 public abstract class Car{ String name="sdfs";//名字 int price;//租金 int capPerson;//载人量 int capGoods;//载货量 public abstract void show(); public abstract String getName(); public abstract void setName(String name); public abstract int getPrice(); public abstract void setPrice(int price); public abstract int getCapPerson(); public abstract void setCapPerson(int capPerson); public abstract int getCapGoods(); public abstract void setCapGoods(int capGoods); } package 答答租车系统; public class Auto extends Car { private String name; private int price; private int capPerson; private int capGoods=0; public Auto(String name,int price,int capPerson){ this.name=name; this.price=price; this.capPerson=capPerson; } 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) { if(capGoods!=0)System.out.println("该值默认为0,不允许修改"); else this.capGoods = capGoods; } } package 答答租车系统; public class Truck extends Car { private String name; private int price; private int capGoods; private int capPerson=0; public Truck(String name,int price,int capGoods){ this.name=name; this.price=price; this.capGoods=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 getCapGoods() { return capGoods; } public void setCapGoods(int capGoods) { this.capGoods = capGoods; } public int getCapPerson() { return capPerson; } public void setCapPerson(int capPerson) { if(capPerson!=0)System.out.println("该值默认为0,不允许修改"); else this.capPerson = capPerson; } } package 答答租车系统; public class Pickup extends Car { public String name; public int price; public int capPerson; public int capGoods; public Pickup(String name,int price,int capPerson,int capGoods){ this.name=name; this.price=price; this.capPerson=capPerson; this.capGoods=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 答答租车系统; 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("序号\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(); } } }
badbomb
一条小咸鱼
相关分类