为了账号安全,请及时绑定邮箱和手机立即绑定

来交作业了

package com.imooc;
import java.util.Arrays;
import java.util.Scanner;

public class RentSystem {		
  //创建可租车辆清单	
  static final Car[] cars= {		
     new Sedan("奥迪A4",500,4),		
     new Sedan("马自达6",400,4),		
     new PickUp("皮卡雪6",450,4,2),		
     new Coach("金龙",800,20),		
     new Truck("松花江",400,4),		
     new Truck("依维柯",1000,20)		
     };		
 
  static Scanner input=new Scanner(System.in);
 	
  public static void main(String[] args) {		
  // TODO Auto-generated method stub				
    System.out.println("欢迎使用答答租车系统");				
    isNeeded(); //判断是否需要租车		
    displayList(); //显示可租车清单		
    int num=rentCarNum(); //获得租车数量		
    int[] nums=getCarList(num); //获得租车型号		
    System.out.println(Arrays.toString(nums));  //验证租车型号		
    int days=getRentDays(); //获得租车天数		
    payCheck(nums,days);			
    }		
  
  private static void isNeeded() {		
    System.out.println("您是否要租车: 1是 0否");		
    int rentOrNot=2;		
    while ((rentOrNot!=0)&&(rentOrNot!=1)) {			
      rentOrNot=input.nextInt();			
      if (rentOrNot==0) {				
        System.out.println("感谢使用,再见");			
        }else if (rentOrNot!=1) {				
          System.out.println("输入有误,请重新输入");			
          }else if (rentOrNot==1) {				
            break;			
            }		
         }	
      }		
    
  private static void displayList() {		
    System.out.println("您可租车的型号和价目表:");		
    for (int i=0;i<cars.length;i++) {			
      if (cars[i] instanceof Sedan) {				
        Sedan car=(Sedan) cars[i];				
        System.out.println((i+1) +"  " + car.name + "  " + car.rent +"元/天   " +"载"+car.seat+"人");			
        }else if (cars[i] instanceof Truck) {				
          Truck car=(Truck)cars[i];				
          System.out.println((i+1) +"  " + car.name + "  " + car.rent +"元/天   " +"载"+car.cargo+"吨货");			
          }else if (cars[i] instanceof PickUp) {				
            PickUp car=(PickUp)cars[i];				
            System.out.println((i+1) +"  " + car.name + "  " + car.rent +"元/天   " +"载"+car.cargo+"吨货"+car.seat+"人");			
            }else if (cars[i] instanceof Coach) {				Coach car=(Coach) cars[i];				
              System.out.println((i+1) +"  " + car.name + "  " + car.rent +"元/天   " +"载"+car.seat+"人");			
              }					
          }	
       }		
     
  private static int rentCarNum() {		
    System.out.println("请输入您要租车的数量");		
    int rentCarNum=input.nextInt();		
    return rentCarNum;		
    }			
  
  private static int getRentDays() {		
    System.out.println("请输入您要租车的天数");		
    int rentDays=input.nextInt();		
    return rentDays;	
    }		
  
  private static int[] getCarList(int num) {		
    int[] carList=new int[num];		
    int i=0;		
    while (i<num) {			
      System.out.println("请输入第"+(i+1)+"辆车的型号");			
      int model=input.nextInt();			
      if((model>cars.length)||(model<=0)) {				
        System.out.println("输入错误,请重新输入");			
        }else {				
          carList[i]=model;				
          i++;			
        }				
     }		
      return carList;	
   }		
   
  private static void payCheck(int[] nums,int days) {		
    int sumRent=0;		
    int sumSeat=0;		
    int sumCargo=0;		
    System.out.println("您选中了以下车辆:");		
    for (int i=0;i<nums.length;i++) {			
      int a=nums[i]-1;			
      if (cars[a] instanceof Sedan) {				
        Sedan rentedCar=(Sedan) cars[a];				
        sumRent=sumRent+rentedCar.rent;				
        sumSeat=sumSeat+rentedCar.seat;				
        System.out.print(rentedCar.name+"\t");			
        }else if (cars[a] instanceof Truck) {				
          Truck rentedCar=(Truck)cars[a];				
          sumRent=sumRent+rentedCar.rent;				
          sumCargo=sumCargo+rentedCar.cargo;				
          System.out.print(rentedCar.name+"\t");			
          }else if (cars[a] instanceof PickUp) {				
            PickUp rentedCar=(PickUp)cars[a];				
            sumRent=sumRent+rentedCar.rent;				
            sumCargo=sumCargo+rentedCar.cargo;				
            System.out.print(rentedCar.name+"\t");				
            sumSeat=sumSeat+rentedCar.seat;			
            }else if (cars[a] instanceof Coach) {				
              Coach rentedCar=(Coach)cars[a];				
              sumRent=sumRent+rentedCar.rent;				
              sumSeat=sumSeat+rentedCar.seat;				
              System.out.print(rentedCar.name+"\t");			
              }		
            }				
            System.out.println("\n总金额:"+sumRent*days+"元");		
            System.out.println("总载人数:"+sumSeat+"人");		
            System.out.println("总载货量:"+sumCargo+"吨");			
          }
        }


正在回答

1 回答

可以哦

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Java入门第二季 升级版
  • 参与学习       530553    人
  • 解答问题       6091    个

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

进入课程

来交作业了

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信