手记

达达租车系统的代码编写(希望u幕友指正)

package firstSaample;
import java.util.Scanner;
public class demo {
public static void main (String[]args)
{
int[] sequence=new int[6];
int days;
String estimate;
Scanner scanner=new Scanner(System.in);
System.out.println("欢迎使用嗒嗒租车系统");
System.out.println("您想要租车吗?Y-是 N-不是");
estimate=scanner.next();
if(estimate.equals("y"))
{
System.out.println("序号 车型 租金 容量 ");
System.out.println("1. 奥迪4 500/天 载人4人 ");
System.out.println("2. 马自达6 400/天 载人4人 ");
System.out.println("3. 皮卡雪6 450/天 载人4 人载货2吨 ");
System.out.println("4. 金龙 800/天 载人20人 ");
System.out.println("5. 松花江 400/天 载货4吨");
System.out.println("6. 依维柯 1000/天 载货20吨 ");
System.out.println("请输入要租的数量 ");
int amount=scanner.nextInt();
for(int i=1;i<=amount;i++)
{
System.out.println("请输入第 "+i+"辆车序号");
sequence[i-1]=scanner.nextInt();
}
System.out.println("请输入租车天数 ");
days=scanner.nextInt();
Car car=new Car(sequence,days,amount);
car.print();
}
scanner.close();
}

}

Car类
package firstSaample;

public class Car {
int[] sequencePrice={500,400,450,800,400,1000};
int[] sequenceCapacityPeople={4,4,4,20,0,0};
int[] sequenceCapacityCargo={0,0,2,0,0,20};
int []sequence;
int days,amount;
public Car(int[] sequence,int days,int amount )
{
this.sequence=sequence;
this.days=days;
this.amount=amount;
}

    public int getReckoning()
    {
        int sum=0;
        for(int i=0;i<amount;i++)
        sum=sum+sequencePrice[sequence[i]-1];//减一是应为车的序号最小为1,而数组下标最小为0
        sum*=days;
        return sum;
    }

    public int getCapacityPeople()
    {
        int sum=0;
        for(int i=0;i<amount;i++)
        sum=sum+sequenceCapacityPeople[sequence[i]-1];//减一是应为车的序号最小为1,而数组下标最小为0
        return sum;
    }

    public int getCapacityCargo()
    {
        int sum=0;
        for(int i=0;i<amount;i++)
        sum=sum+sequenceCapacityCargo[sequence[i]-1];
        return sum;
    }

    public void print()
    {
        System.out.println("您的账单为"+this.getReckoning());
        System.out.println("您的载货量为"+this.getCapacityCargo());
        System.out.println("您的载客量为"+this.getCapacityPeople());
    }

}

1人推荐
随时随地看视频
慕课网APP