交作业,类分的比较细,用了接口,参考了大神的代码用了List集合
各种名字起的不是很好..
车类Auto.java
package com.imooc;
public class Auto {
private int autoNum;
private String autoName;
private int autoRentprice;
public int getAutoNum() {
return autoNum;
}
public void setAutoNum(int autoNum) {
this.autoNum = autoNum;
}
public String getAutoName() {
return autoName;
}
public void setAutoName(String autoName) {
this.autoName = autoName;
}
public int getAutoRentprice() {
return autoRentprice;
}
public void setAutoRentprice(int autoRentprice) {
this.autoRentprice = autoRentprice;
}
}
小汽车类car.java
package com.imooc;
public class car extends Auto implements ImannedAuto {
private int mannedNum;
public car(int autoNum,String autoName,int autoRentprice,int mannedNum){
this.setAutoName(autoName);
this.setAutoNum(autoNum);
this.setAutoRentprice(autoRentprice);
this.mannedNum=mannedNum;
}
@Override
public void setmannedNum(int mannedNum) {
// TODO Auto-generated method stub
this.mannedNum=mannedNum;
}
public int getmannedNum(){
return mannedNum;
}
}
大客车类Motorcoach.java:
package com.imooc;
public class Motorcoach extends Auto implements ImannedAuto {
private int mannedNum;
public Motorcoach(int autoNum,String autoName,int autoRentprice,int mannedNum){
this.setAutoName(autoName);
this.setAutoNum(autoNum);
this.setAutoRentprice(autoRentprice);
this.mannedNum=mannedNum;
}
@Override
public void setmannedNum(int mannedNum) {
// TODO Auto-generated method stub
if(mannedNum<10)
{
System.out.println("大客车载人数应为10人以上!");
}
else this.mannedNum=mannedNum;
}
public int getmannedNum(){
return mannedNum;
}
}
小货车类Smalltruck.java:
package com.imooc;
public class Smalltruck extends Auto implements IlabenAuto {
private int labenNum;
public Smalltruck(int autoNum,String autoName,int autoRentprice,int labenNum){
this.setAutoName(autoName);
this.setAutoNum(autoNum);
this.setAutoRentprice(autoRentprice);
this.labenNum=labenNum;
}
@Override
public void setlabenNum(int labenNum) {
// TODO Auto-generated method stub
if(labenNum>5)
{
System.out.println("小货车的载货量应为5吨以下!");
}
else this.labenNum=labenNum;
}
public int getlabenNum(){
return labenNum;
}
}
大货车类Bigtruck.java:
package com.imooc;
public class Bigtruck extends Auto implements IlabenAuto {
private int labenNum;
public Bigtruck(int autoNum,String autoName,int autoRentprice,int labenNum){
this.setAutoName(autoName);
this.setAutoNum(autoNum);
this.setAutoRentprice(autoRentprice);
this.labenNum=labenNum;
}
@Override
public void setlabenNum(int labenNum) {
// TODO Auto-generated method stub
if(labenNum<10)
{
System.out.println("大货车的载货量应为10吨以上!");
}
else this.labenNum=labenNum;
}
public int getlabenNum(){
return labenNum;
}
}
皮卡类Pickup.java:
package com.imooc;
public class Pickup extends Auto implements IlabenAuto, ImannedAuto {
private int labenNum;
private int mannedNum;
public Pickup(int autoNum,String autoName,int autoRentprice,int labenNum,int mannedNum){
this.setAutoName(autoName);
this.setAutoNum(autoNum);
this.setAutoRentprice(autoRentprice);
this.labenNum=labenNum;
this.mannedNum=mannedNum;
}
@Override
public void setmannedNum(int mannedNum) {
// TODO Auto-generated method stub
this.mannedNum=mannedNum;
}
@Override
public void setlabenNum(int labenNum) {
// TODO Auto-generated method stub
this.labenNum=labenNum;
}
public int getmannedNum(){
return mannedNum;
}
public int getlabenNum(){
return labenNum;
}
}
载人接口ImannedAuto.java:
package com.imooc;
public abstract interface ImannedAuto {
abstract void setmannedNum(int mannedNum);
}
载货接口IlabenAuto.java;
package com.imooc;
public abstract interface IlabenAuto {
abstract void setlabenNum(int labenNum);
}
主函数类initialTest.java:
package com.imooc;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class initialTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("欢迎使用嗒嗒租车系统!");
System.out.println("您是否要租车? :1 是 0 否");
Scanner c=new Scanner(System.in);
int k=c.nextInt();
if(k == 1){
System.out.println("您可租车的类型及其价目表:");
Auto [] carForRent={new car(1, "奥迪A4", 500, 4),
new car(2, "马自达6", 400, 4),
new Pickup(3, "皮卡雪6", 450, 4, 2),
new Smalltruck(4, "金龙", 800, 2),
new Smalltruck(5, "松花江", 400, 4),
new Bigtruck(6, "依维柯", 1000, 20)};
System.out.println("序号"+"\t"+"汽车名称"+"\t"+"租金"+"\t"+"\t"+"容量");
//打印输出可供租的汽车清单
for(Auto Auto: carForRent){
if(Auto instanceof car){
System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
Auto.getAutoRentprice()+"/天"+"\t"+"载人:"+((car) Auto).getmannedNum());
}
if(Auto instanceof Motorcoach){
System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
Auto.getAutoRentprice()+"/天"+"\t"+"载人:"+((Motorcoach) Auto).getmannedNum());
}
if(Auto instanceof Smalltruck){
System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
Auto.getAutoRentprice()+"/天"+"\t"+"载货:"+((Smalltruck) Auto).getlabenNum());
}
if(Auto instanceof Bigtruck){
System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
Auto.getAutoRentprice()+"/天"+"\t"+"载货:"+((Bigtruck) Auto).getlabenNum());
}
if(Auto instanceof Pickup){
System.out.println(Auto.getAutoNum()+"\t"+Auto.getAutoName()+"\t"+
Auto.getAutoRentprice()+"/天"+"\t"+"载人:"+((Pickup) Auto).getmannedNum()+
"载货:"+((Pickup) Auto).getlabenNum());
}
}
System.out.println("请输入您要租的汽车的数量");
int totalNum=c.nextInt(); //用于接收汽车的总数量
//调用集合框架List用于存储客户租车的清单
List<Auto> autoList=new ArrayList<Auto>();
int p=0; //p变量用来拓展carList的容量
List<Auto> autoListPassenger=new ArrayList<Auto>(); //用来储存可载人的车辆
List<Auto> autoListTrunk=new ArrayList<Auto>(); //用来储存可载货的车辆
int totalPassenger=0;; //用来存储总的载人数
int totalCarrayCargo=0; //用来存储总的载货量
int totalMoney=0; //用来计算租车的总资金
/*
* 主要思想:用循环遍历在添加客户租车的序号,用集合框架List来存储客户租的车辆。
* 同时在客户输入序号的时候与三种车辆进行比对,计算相应车辆的载人数,载货量以及租车的总资金
* 同时添加到载人车辆或者载货车辆当中
*/
for(int i=0;i<totalNum;i++){
System.out.println("请输入第"+(i+1)+"辆车的序号:");
int num=c.nextInt(); //用于接收输入的汽车的序号
autoList.add((Auto) carForRent[num-1]);
System.out.println("成功添加:"+autoList.get(p).getAutoName());
if(carForRent[num-1] instanceof car){
totalPassenger+=((car)autoList.get(p)).getmannedNum();
totalMoney+=((car)autoList.get(p)).getAutoRentprice();
autoListPassenger.add(autoList.get(p));
}
if(carForRent[num-1] instanceof Motorcoach){
totalPassenger+=((Motorcoach)autoList.get(p)).getmannedNum();
totalMoney+=((Motorcoach)autoList.get(p)).getAutoRentprice();
autoListPassenger.add(autoList.get(p));
}
if(carForRent[num-1] instanceof Pickup){
totalPassenger+=((Pickup)autoList.get(p)).getmannedNum();
totalCarrayCargo+=((Pickup)autoList.get(p)).getlabenNum();
totalMoney+=((Pickup)autoList.get(p)).getAutoRentprice();
autoListPassenger.add(autoList.get(p));
autoListTrunk.add(autoList.get(p));
}
if(carForRent[num-1] instanceof Smalltruck){
totalCarrayCargo+=((Smalltruck)autoList.get(p)).getlabenNum();
totalMoney+=((Smalltruck)autoList.get(p)).getAutoRentprice();
autoListTrunk.add(autoList.get(p));
}
if(carForRent[num-1] instanceof Bigtruck){
totalCarrayCargo+=((Bigtruck)autoList.get(p)).getlabenNum();
totalMoney+=((Bigtruck)autoList.get(p)).getAutoRentprice();
autoListTrunk.add(autoList.get(p));
}
p++;
}
System.out.println("请输入租车的天数:");
int carRenDay=c.nextInt();
totalMoney=totalMoney*carRenDay;
System.out.println("********************可载人的车有********************");
for (Auto auto : autoListPassenger) {
System.out.println(auto.getAutoName());
}
System.out.println("********************可载货的车有********************");
for (Auto auto : autoListTrunk) {
System.out.println(auto.getAutoName());
}
System.out.println("********************租车的总价格********************");
System.out.println(totalMoney*carRenDay);
}
else if(k==0){
System.out.println("感谢您的再次使用!再见!");
}else{
System.out.println("您输入的信息有错!");
}
}
}