问答详情
源自:12-2 项目问题解析 1

第二季项目代码,请大家们指教,蟹蟹泥萌。

package dadaCarRental;

import java.util.Scanner;

public class DadaTest {
        
    public static void main(String[] args) {
        //Vehicle → Object(assignment) → Arrays
        Vehicle [] vehicles = new Vehicle[6];
        vehicles [0] = new Vehicle("奥迪A4",4,0,500);
        vehicles [1] = new Vehicle("马自达6",4,0,400);
        vehicles [2] = new Vehicle("皮卡雪6",4,2,450);
        vehicles [3] = new Vehicle("金龙",20,0,800);
        vehicles [4] = new Vehicle("松花江",0,4,400);
        vehicles [5] = new Vehicle("依维柯",0,20,1000);
        //test:System.out.println(vehicles[3].price);
        
        //welcome.
        System.out.println("*****欢迎使用达达租车系统*****");
        System.out.println("您是否要租车:1是 0 否");
        
        //Input user's choise.
        Scanner input = new Scanner(System.in);
        int inputNum1 = input.nextInt();
        
        //Judge and feedback sth.
        if(inputNum1 == 1){
            System.out.println("*****您可租车的类型及其价目表*****");
            System.out.println("序号" + "    " + "汽车名称 " + "        " + "租金" + "    " + "容量");
            for(int i=0; i<6; i++){
                System.out.print(i+1+"、       "+vehicles[i].name + "        " +vehicles[i].price + "    ");
                vehicles[i].action(vehicles[i].passangersNum, vehicles[i].cargoNum);
                System.out.print("\n");
            }
            
            //Start to rent.
            System.out.println("请输入您要租车的数量:");
            int inputNum2 = input.nextInt();
            int totalPrice = 0;
            int totalP = 0;
            int totalC = 0;
            int pIndex = 0;
            int cIndex = 0;
            
            //set string arrays to store names. 
            String [] pVehicles = new String[inputNum2];
            String [] cVehicles = new String[inputNum2];
            
                //Input and record the detils.
                for(int i=1; i<= inputNum2; i++){
                    System.out.println("请输入第" + i + "辆车的序号");
                    int carIndex = input.nextInt();
                    if(carIndex<=0 || carIndex>=7){
                        System.out.println("你输错序号辣,重头再来吧!╮(╯_╰)╭");
                        break;
                    }
                    
                    totalPrice = totalPrice + vehicles[carIndex-1].price;
                    
                    if(vehicles[carIndex-1].passangersNum !=0){
                        totalP = totalP + vehicles[carIndex-1].passangersNum;
                        pVehicles[pIndex] = vehicles[carIndex-1].name;
                        pIndex = pIndex + 1 ;
                    }
                    if(vehicles[carIndex-1].cargoNum !=0){
                        totalC = totalC + vehicles[carIndex-1].cargoNum;
                        cVehicles[cIndex] = vehicles[carIndex-1].name;
                        cIndex = cIndex + 1 ;
                    }
                }
            
            //Print the bill and info.
            if(totalPrice !=0 ){
                System.out.println("*****您的账单*****");
                System.out.println("总价:"+totalPrice+"元"+"\n");
            
                System.out.print("可载人的车:");
                if(totalP!=0){
                    System.out.print("(可载"+totalP+"人)"+"\n");
                    for(int i = 0; i<pIndex; i++ ){
                        System.out.print(pVehicles[i]+"    ");
                    }
                }else{
                    System.out.println("根本没有租辣!"+"\n");
                }
            
                System.out.print("\n"+"可载货的车:");
                if(totalC!=0){
                    System.out.print("(可载"+totalC+"吨)"+"\n");
                        for(int i = 0; i<cIndex; i++){
                            System.out.print(cVehicles[i]+"    ");
                        }
                }else{
                    System.out.print("根本没有租辣!");
                    }
                
            }else{
                System.out.println("而且你要支付我咨询费100块!");
                }
            
        }else if(inputNum1 == 0){
            System.out.println("好吧。。。白白!");
        }else{
            System.out.println("按错辣!╮( ̄⊿ ̄)╭!粗去重新来!");
        }
        
        }    
    }

class Vehicle {
    String name;
    int passangersNum;
    int cargoNum;
    int price;
    
    //set the initial paramater.
    Vehicle(String vName,int pNum,int cNum,int vPrice){
        name = vName;
        passangersNum = pNum;
        cargoNum = cNum;
        price = vPrice;
    };
    
    //judge what it carrys.
    void action(int pnum,int cnum){
        if(pnum != 0){
            System.out.print("载客:" + pnum + "人");
        }
        if(cnum != 0){
            System.out.print("载货"+ cnum + "吨");
        }
    }
}


提问者:ilikeWaltz 2016-06-22 18:07

个回答

  • ilikeWaltz
    2016-06-22 18:13:59

    第一次写项目,自己分析自己的一些问题:

    1、没有再去划分子类,直接用父类。

    2、如果选车环节,随便无序输入序号的话,最后的车型输出会很难看。打算后面用数组来解决。

    3、没有解决如果输错序号或量后怎么重新输入的问题。

    4、封装、继承、多态和接口都没有使用,对此重要概念运用不熟练。写的不像是面向对象而是面向过程的语言。


    以上,还请大腿们多多赐教,蟹蟹。