1 package org.hanqi.pn0120;
2
3 //汽车
4 public class Car {
5
6 //颜色
7 private String yanse;
8
9 //品牌
10 private String pinpai;
11
12 //油箱容量
13 private double youxiangrl = 40;
14
15 //当前油量
16 private double dangqianyl = 0;
17
18 public double getDanqianyl()
19 {
20 return this.dangqianyl;
21 }
22
23 //加油
24 public void jiaYou(double youliang)
25 {
26 if (youliang < 0)
27 {
28 System.out.println("不要偷油");
29 }
30 else if (this.dangqianyl + youliang > this.youxiangrl)
31 {
32 //本次加油量
33 double bcyl = this.youxiangrl - this.dangqianyl;
34
35 this.dangqianyl = this.youxiangrl;
36
37 System.out.println("油太多了,已经加满了,这次加了 " + bcyl + "升油");
38 }
39 else
40 {
41 this.dangqianyl += youliang;
42
43 System.out.println("这次加了 " + youliang + " 升油");
44 }
45 }
46
47 //private double youhao =
48
49 private double xingshizlc = 0;
50
51 public double getXingshizlc()
52 {
53 return this.xingshizlc;
54 }
55
56 //行驶
57 public void xingshi(double xingshilc, double youhao)
58 {
59 //
60 if (this.dangqianyl <= 0)
61 {
62 System.out.println("车没有油,怎么开车");
63
64 return;
65 }
66
67 if (xingshilc < 0 || youhao < 0)
68 {
69 System.out.println("不可能这么开车");
70 }
71 else
72 {
73 //油耗
74 double dangqyh = xingshilc * youhao / 100;
75
76 this.dangqianyl -= dangqyh;
77
78 //验证当前油量
79 //this.dangqianyl = this.dangqianyl > 0 ? this.dangqianyl : 0;
80
81 if(this.dangqianyl < 0)
82 {
83 this.dangqianyl = 0;
84
85 System.out.println("你是推着车吗?");
86 }
87
88 this.xingshizlc += xingshilc;
89
90 System.out.println("本次行驶了 " + xingshilc + " 公里");
91 }
92
93 }
94
95 public String getYanse() {
96 return yanse;
97 }
98
99 public void setYanse(String yanse) {
100 this.yanse = yanse;
101 }
102
103 public String getPinpai() {
104 return pinpai;
105 }
106
107 public void setPinpai(String pinpai) {
108 this.pinpai = pinpai;
109 }
110
111 public double getYouxiangrl() {
112 return youxiangrl;
113 }
114
115 public void setYouxiangrl(double youxiangrl) {
116 this.youxiangrl = youxiangrl;
117 }
118
119 public Car(String pinpai, double youxiangrl) {
120 super();
121 this.pinpai = pinpai;
122 this.youxiangrl = youxiangrl;
123 }
124
125 //设计模式 - 单例模式(单态模式)
126
127 //静态 static
128
129
130 //main方法
131 public static void main(String[] args)
132 {
133
134 Car myBenchi = new Car("奔驰", 50);
135
136 System.out.println("品牌 = " + myBenchi.getPinpai());
137 System.out.println("油箱容量 = " + myBenchi.getYouxiangrl());
138
139 myBenchi.xingshi(200, 13);
140
141 myBenchi.jiaYou(30);
142
143 myBenchi.jiaYou(30);
144
145
146 System.out.println("当前油量 = " + myBenchi.getDanqianyl());
147
148 myBenchi.xingshi(300, 15);
149
150 System.out.println("当前油量 = " + myBenchi.getDanqianyl());
151
152 myBenchi.xingshi(200, 13);
153
154 System.out.println("当前总里程 = " + myBenchi.getXingshizlc());
155 System.out.println("当前油量 = " + myBenchi.getDanqianyl());
156
157 }
158
159
160 }