问答详情
源自:12-1 综合练习

课程作业嗒嗒租车系统 源码如下 不同看法可以交流

Car.java

package com.dada.test;

public class Car {

private int id;

private String name;

private int price;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

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 Car(int id, String name, int price) {

super();

this.id = id;

this.name = name;

this.price = price;

}

}

GoodCar.java

package com.dada.test;

public class GoodCar extends Car {

private int goodcapacity;

public GoodCar(int id, String name, int price, int goodcapacity) {

super(id, name, price);

this.goodcapacity = goodcapacity;

}

public int getGoodcapacity() {

return goodcapacity;

}

public void setGoodcapacity(int goodcapacity) {

this.goodcapacity = goodcapacity;

}

}

GPCar.java

package com.dada.test;

public class GPCar extends Car {

private int goodcapacity;

private int peoplecapacity;

public GPCar(int id, String name, int price, int goodcapacity, int peoplecapacity) {

super(id, name, price);

this.goodcapacity = goodcapacity;

this.peoplecapacity = peoplecapacity;

}

public int getGoodcapacity() {

return goodcapacity;

}

public void setGoodcapacity(int goodcapacity) {

this.goodcapacity = goodcapacity;

}

public int getPeoplecapacity() {

return peoplecapacity;

}

public void setPeoplecapacity(int peoplecapacity) {

this.peoplecapacity = peoplecapacity;

}

}

PeopleCar.java

package com.dada.test;

public class PeopleCar extends Car {

private int peoplecapacity;

public PeopleCar(int id, String name, int price, int peoplecapacity) {

super(id, name, price);

this.peoplecapacity = peoplecapacity;

}

public int getPeoplecapacity() {

return peoplecapacity;

}

public void setPeoplecapacity(int peoplecapacity) {

this.peoplecapacity = peoplecapacity;

}

}

Test.java

package com.dada.test;

import java.util.Scanner;

import com.dada.test.*;

public class Test {

public static void main(String[] args) {

PeopleCar pc1 = new PeopleCar(1,"奥迪A4",500,4);

PeopleCar pc2 = new PeopleCar(2,"马自达6",400,4);

GPCar gpc = new GPCar(3,"皮卡雪6",450,2,4);

PeopleCar pc3 = new PeopleCar(4,"金龙",800,20);

GoodCar gc1 = new GoodCar(5,"松花江",400,4);

GoodCar gc2 = new GoodCar(6,"依维柯",1000,20);

Scanner sc = new Scanner(System.in);

boolean keep = true;

System.out.println("*********嗒嗒租车系统*********");

while(keep){

System.out.print("是否租用汽车1/0:");

if("0".equals(sc.next())){

System.out.println("*********系统已退出*********");

break;

}

 

System.out.println("您可租车的类型及价目表:");

System.out.println("1. 奥迪A4 500元/天 载人:4人\n" +"2. 马自达6 400元/天 载人:4人\n" +"3. 皮卡雪6 450元/天 载人:4人载货:2吨\n"+"4. 金龙 800元/天 载人:20人\n"+"5. 松花江 400元/天 载货:4吨\n"+"6. 依维柯 1000元/天 载货:20吨\n");

int peoplesum=0;

int goodsum=0;

int pricesum=0;

String carname="";

System.out.print("输入你租用车的数量:");

int carnum = sc.nextInt();

System.out.print("请输入你租用车的天数:");

int day =sc.nextInt();

for(int i=0;i<carnum;i++){

System.out.print("输入你租用第" + (i+1) + "俩车的id:");

int id = sc.nextInt();

if(id==1){

carname +=pc1.getName()+"  ";

peoplesum += pc1.getPeoplecapacity();

pricesum += pc1.getPrice()*day;

}else if(id==2){

carname +=pc2.getName()+"  ";

peoplesum += pc2.getPeoplecapacity();

pricesum += pc2.getPrice()*day;

}else if(id==3){

carname +=gpc.getName()+"  ";

peoplesum += gpc.getPeoplecapacity();

goodsum += gpc.getGoodcapacity();

pricesum += gpc.getPrice()*day;

}else if(id==4){

carname +=pc3.getName()+"  ";

peoplesum += pc3.getPeoplecapacity();

pricesum += pc3.getPrice()*day;

}else if(id==5){

carname +=gc1.getName()+"  ";

goodsum += gc1.getGoodcapacity();

pricesum += gc1.getPrice()*day;

}else if(id==6){

carname +=gc2.getName()+"  ";

goodsum += gc2.getGoodcapacity();

pricesum += gc2.getPrice()*day;

}else{

System.out.println("您输入的id有误!");

}

}

System.out.println("选择的车有:"+carname );

System.out.println("租金:"+pricesum);

System.out.println("载人量:"+peoplesum+"人");

System.out.println("载货量:"+goodsum+"吨");

}

}

}


提问者:毒打妻儿田教授 2019-01-16 13:48

个回答

  • 慕函数1299684
    2019-02-04 13:16:55
    已采纳

    你这个代码有一些问题  

    1. 当你在选择是否租车的时候 没有考虑0/1之外的其他情况 比如输入3的话也会进入选车界面https://img2.mukewang.com/5c57ca750001185403550255.jpg

    2. 当你输入要租的车辆序号时,如果输入一个错误的车辆序号,比如11,这时会提示输入错入,重新输入

      https://img1.mukewang.com/5c57caa80001ba5b02470077.jpg

      就像这样,用户会少选择一辆车

  • qq_童化金_dwxHIS
    2019-03-08 16:15:30

    请问一下,如果需要租用两种汽车,该肿么办?

  • qq_慕莱坞602165
    2019-01-20 16:31:40

    //Car.java

    public abstract class Car {
        public abstract String name();
    
        public abstract int price();
    
        public abstract int space();
    }

    //MannedCar.java
        

    public class MannedCar extends Car {
        private String name;
        private int price;
        private int space;
    
        public MannedCar() {
        }
    
        MannedCar(String name, int price, int space) {
            this.name = name;
            this.price = price;
            this.space = space;
        }
        
        @Override
        public String name() {
            return name;
        }
    
        @Override
        public int price() {
            return price;
        }
    
        @Override
        public int space() {
            return space;
        }
    }

    //PikaCar.java

    public class PikaCar extends Car {
    
        private String name;
        private int price;
        private int space;
      
    
    
        public PikaCar(String name, double price, int space) {
            this.name = name;
            this.price = (int) price;
            this.space = space;
        }
    
      
    
        @Override
        public String name() {
            return name;
        }
    
        @Override
        public int price() {
            return price;
        }
    
        @Override
        public int space() {
            return space;
        }
    
    }

    //CarryCargoCar.java

    public class CarryCargoCar extends Car{
    
        private String name;
        private int price;
        private int space;
        
     
    
        public CarryCargoCar(String name, double price, int space) {
            this.name = name;
            this.price = (int) price;
            this.space = space;
        }
    
    
    
        @Override
        public String name() {
            return name;
        }
    
        @Override
        public int price() {
            return price;
        }
    
        @Override
        public int space() {
            return space;
        }
    }

    //Text.java

    import java.util.Scanner;
    public class Test {
        private static Scanner scannerInt = new Scanner(System.in);
    
        public static void main(String[] args) {
            System.out.println("欢迎使用答答租车系统:");
            System.out.println("您是否要租车:1是 0否");
            int choose = scannerInt.nextInt();
            Car[] car = {new MannedCar("奥迪A4", 500, 4), new MannedCar("马自达6", 400, 4), new PikaCar("皮卡雪6", 450, 4), new MannedCar("金龙", 800, 20), new CarryCargoCar("松花江", 400, 4), new CarryCargoCar("依维柯", 1000, 20)};
    
            switch (choose) {
                case 1:
                    System.out.println("您可租车的类型及其价格目录表:");
                    System.out.println("序号\t汽车名称\t租金\t        容量");
                    for (int i = 1; i < 7; i++) {
                        System.out.print(i + ".\t    " + car[i - 1].name() + "\t    " + car[i - 1].price() + "元/天" + "\t    ");
                        if (car[i - 1].name().equals("皮卡雪6")) {
                            System.out.println("载人:" + 4 + " 载货:" + 2 + "吨");
                        } else if ((i - 1) == 0 || (i - 1) == 1 || (i - 1) == 3) {
                            System.out.println("载人:" + car[i - 1].space() + "人");
                        } else if ((i - 1) == 4 || (i - 1) == 5) {
                            System.out.println("载货:" + car[i - 1].space() + "吨");
                        }
                    }
    
                    System.out.println("请输入您要租汽车的数量:");
                    int number = scannerInt.nextInt();
                    Car[] num = new Car[number];
                    if (number != 0) {
                        for (int i = 1; i <= number; i++) {
                            System.out.println("请输入第" + i + "辆车的序号:");
                            int choice = scannerInt.nextInt();
                            if (choice < 1 || i > 6) {
                                System.out.println("您输入的序号不存在,请重新输入!");
                                i--;
                            } else {
                                num[i - 1] = car[choice - 1];
                            }
                        }
                        double money = 0;
                        System.out.println("请输入租车天数:");
                        int day = scannerInt.nextInt();
                        for (int i = 0; i < num.length; i++) {
                            money = money + num[i].price();
                        }
                        System.out.println("您的账单:");
                        System.out.println("***可载人的车有:");
                        int sum = 0;
                        for (int i = 0; i < num.length; i++) {
                            if (num[i].name().equals("奥迪A4") || num[i].name().equals("马自达6") || num[i].name().equals("金龙") || num[i].name().equals("皮卡雪6")) {
                                System.out.print(num[i].name() + "   ");
                                if (num[i].name().equals("皮卡雪6")) {
                                    sum += 4;
                                } else {
                                    sum = sum + num[i].space();
                                }
                            }
                        }
    
                        for (int i = 0; i < num.length; i++ ) {
                            if (num[i].name().equals("奥迪A4") || num[i].name().equals("马自达6") || num[i].name().equals("金龙") || num[i].name().equals("皮卡雪6")) {
                                System.out.println("共载人:" + sum + "人");
                                break;
                            }
                        }
    
                        System.out.println("***可载货的车有:");
                        for (int i = 0; i < num.length; i++) {
                            if (num[i].name().equals("皮卡雪6") || num[i].name().equals("松花江") || num[i].name().equals("依维柯")) {
                                System.out.print(num[i].name() + "   ");
                                if (num[i].name().equals("皮卡雪6")) {
                                    sum += 2;
                                } else {
                                    sum = sum + num[i].space();
                                }
                            }
                        }
                        for (int i = 0; i < num.length; i++) {
                            if (num[i].name().equals("皮卡雪6") || num[i].name().equals("松花江") || num[i].name().equals("依维柯")) {
                                System.out.println("共载货:" + sum + "吨");
                                break;
                            }
                        }
                        double amount = money * day;
                        System.out.println("***租车总价格:" + amount + "元");
                    } else {
                        System.out.println("您没有选择任何车辆!");
                    }
                    break;
                case 0:
                    System.exit(0);
                    break;
                default:
                    System.out.println("您输入的选择不存在");
            }
        }
    }


  • qq_慕莱坞5143194
    2019-01-17 18:50:46

    就只把输出语句里的代码,输出