Java实现答答租车系统

时间:2022-09-04 22:12:35

本文实例为大家分享了java实现答答租车系统的具体代码,供大家参考,具体内容如下

项目需求:

Java实现答答租车系统

基本界面需求:

Java实现答答租车系统

and:

Java实现答答租车系统

最后是把账单打印出来:

Java实现答答租车系统

个人代码实现

基本思路:考虑到车辆之间的共性,设置一个父类car, 子类mannedcar(载人), truck(载货),bothcary(既载人又载货),三者继承父类car的price, name属性, getname()方法, 同时重写getpersonnum, getgoodsnum方法。

car.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package car;
 
public abstract class car {
  protected int price;
  protected string name;
  protected int getprice() {
    return price;
  }
  protected string getname() {
    return name;
  }
  public int getpersonnum() {
    // todo auto-generated method stub
    return 0;
  }
  public int getgoodsnum() {
    // todo auto-generated method stub
    return 0;
  }
 
}

mannedcar.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package car;
 
public class mannedcar extends car {
  private int personnum;
 
  public mannedcar() {
    this.personnum = 0;
    this.price = 0;
    this.name = "";
  }
 
  public mannedcar(int personnum, int price, string name) {
    this.personnum = personnum;
    this.price = price;
    this.name = name;
  }
 
  @override
  public int getpersonnum() {
    return personnum;
  }
}

truck.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package car;
 
public class truck extends car{
  private int goodsnum;
 
  public truck() {
    this.price = 0;
    this.goodsnum = 0;
    this.name = "";
  }
 
  public truck(int price, int goodsnum, string name) {
    this.price = price;
    this.goodsnum = goodsnum;
    this.name = name;
  }
 
  @override
  public int getgoodsnum() {
    return goodsnum;
  }
}

bothcarry.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package car;
 
public class bothcarry extends car {
  private int personnum;
  private int goodsnum;
 
  public bothcarry() {
    this.personnum = 0;
    this.goodsnum = 0;
    this.name = "";
    this.price = 0;
  }
 
  public bothcarry(int price, int personnum,
      int goodsnum, string name) {
    this.personnum = personnum;
    this.goodsnum = goodsnum;
    this.price = price;
    this.name = name;
  }
 
  public int getpersonnum() {
    return personnum;
  }
 
  public int getgoodsnum() {
    return goodsnum;
  }
}

系统:

carsystem.java:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package car;
 
import java.util.scanner;
import java.util.arraylist;
 
public class carsystem {
 
  private static string goodbyeinfo = "欢迎再次使用本系统,再见!";
  private static int dayborrow;
 
  public static void beginsystem() {
    carsystem.systemwelcome();
    scanner scanner = new scanner(system.in);
    string usercommand = scanner.next();
 
 
    switch(usercommand){
    case "1":
      carsystem.getuserinput();
      break;
    case "0":
      system.out.println(goodbyeinfo);
      break;
    default:
      system.out.println("输入错误..end running..");
      system.exit(0);
      break;
    }
  }
 
  public static void systemwelcome() {
    system.out.println("欢迎使用答答租车系统:");
    system.out.println("您是否要租车: 1-是 0-否");
  }
 
  public static void getuserinput() {
    int numcarborrow = 0;
    arraylist<car> carlist = new arraylist<car>(6);
    carlist.add(new mannedcar(4,500,"奥迪a4"));
    carlist.add(new mannedcar(4,400,"马自达6"));
    carlist.add(new bothcarry(450,4,2,"皮卡雪6"));
    carlist.add(new mannedcar(20,800,"金龙"));
    carlist.add(new truck(400,4,"松花江"));
    carlist.add(new truck(1000,20,"依维河"));
 
    system.out.println("请输入您要租汽车的数量:");
    scanner sr = new scanner(system.in);
 
    numcarborrow = sr.nextint();
 
    int[] carnumlist = new int[numcarborrow];
    for(int i=0;i<numcarborrow;i++) {
      system.out.println("请输入第" + (i+1) + "辆车的序号:");
      if (sr.hasnext()) {
        carnumlist[i] = sr.nextint();
      }
    }
    system.out.println("请输入租车天数:");
    if (sr.hasnext()) {
      dayborrow = sr.nextint();
    }
    sr.close();
 
    stringbuilder manned = new stringbuilder();
    int numofmanned = 0;
    stringbuilder goods = new stringbuilder();
    int numofgoods = 0;
    int totalcost = 0;
 
    for(int i = 0;i < carnumlist.length;i++) {
      if(carnumlist[i]>0 && carnumlist[i] < 3 || carnumlist[i]==4) {
        manned.append("  ");
        manned.append(carlist.get(carnumlist[i]-1).getname());
        numofmanned += carlist.get(carnumlist[i]-1).getpersonnum();
      }
      else if(carnumlist[i]==3) {
        manned.append("  ");
        manned.append(carlist.get(carnumlist[i]-1).getname());
        goods.append("  ");
        goods.append(carlist.get(carnumlist[i]-1).getname());
        numofmanned += carlist.get(carnumlist[i]-1).getpersonnum();
        numofgoods += carlist.get(carnumlist[i]-1).getgoodsnum();
      }
      else {
        goods.append("  ");
        goods.append(carlist.get(carnumlist[i]-1).getname());
        numofgoods += carlist.get(carnumlist[i]-1).getgoodsnum();
      }
      totalcost += carlist.get(carnumlist[i]-1).getprice();
    }
    //output
    system.out.println("您的账单:\n***可载人的车有:");
    system.out.println(manned + "  共载人: " + numofmanned);
    system.out.println("***载货的车有:\n" + goods + "  共载货 : " + numofgoods + "吨");
    system.out.println("***租车总价格: " + totalcost * dayborrow + "元");
 
  }
 
}

主程序:

?
1
2
3
4
5
6
7
8
9
10
package car;
 
public class carsystemtest {
 
  public static void main(string[] args) {
    // todo auto-generated method stub
    carsystem.beginsystem();
  }
 
}

运行结果:

Java实现答答租车系统

Java实现答答租车系统

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/Joseph_Cherry/article/details/60530978