package com.hanqi;
public class Har {
// 品牌
private String pinP;
// 读
public String getPinP() {
return pinP;
}
// 写的属性
public void setPinP(String pinP) {
System.out.println("车的品牌设置为" + pinP);
this.pinP = pinP;
}
// 状态
private String zhuangtai = "静止";
public String getZhuangtai() {
return zhuangtai;
}
// 当前油量属性
private double youliang = 0;
public double getYouliang() {
return youliang;
}
private double youxiang;
public void setYouxiang(double youxiang) {
this.youxiang = youxiang;
}
public double getYouxiang() {
return youxiang;
}
// 一次加满 方法的重载
public void jiaYou() {
System.out.println("一次加满");
double yjyl = this.youxiang - this.youliang;
jiaYou(yjyl);
}
// 加油
public void jiaYou(double youliang) {
// 邮箱容量大于0
if (youxiang <= 0) {
System.out.println("忘装邮箱了");
return;
}
// 加油量大于0
else if (youliang < 0) {
System.out.println("不要偷我的油");
return;
} else if (!zhuangtai.equals("静止")) {
System.out.println("车停了再加油");
return;
}
// 只有静止状态下才能加
// 不能超过邮箱油量
else if (this.youliang + youliang > this.youxiang) {
System.out.println("邮箱已经加满了,不要再加了");
double syyl = this.youliang;
this.youliang = this.youxiang;
System.out.println("这次加了" + (youxiang - syyl) + "升油");
}
else {
this.youliang += youliang;
}
System.out.println("这次加了" + youliang + "升油");
}
public void fadong() {
if (this.youliang <= 0) {
System.out.println("煤油了,请先加油");
}
else if (!this.getZhuangtai().equals("静止")) {
System.out.println("车已经发动");
} else {
this.zhuangtai = "发动";
System.out.println("车发动了");
}
}
private double zongli;
// 行驶
public double getZongli() {
return zongli;
}
public void xingshi(double lic,double youhao)
{//车的状态是发动
if(!this.getZhuangtai().equals("发动")){
System.out.println("请先发动汽车 ");
}
else{
//double zyh = lic * youhao / 100;
double lc = this.youliang *100 / youhao;
if(lc < lic){
System.out.println("最多行驶 " + lc+ "公里");
this.zongli += lc;
this.youliang = 0;
this.zhuangtai = "静止";
}
else{
System.out.println("行驶了" + lic + "公里");
this.zongli += lic;
this.zhuangtai = "发动";
}
}
// 计算总油耗,判断是否没油了
}
/*
* private String Zhuangtai() { // TODO 自动生成的方法存根 return null; }
*/
public Har()// 方法名和类名一致;无返回值不用void;
{
System.out.println("调用了构造方法");
}
// 重载
public Har(String pinP) {
System.out.println("实例化车的品牌" + pinP);
this.pinP = pinP;
}
public static void main(String[] args) {
Har har = new Har("宝马");// 默认构造方法
har.fadong();
har.setYouxiang(40);
// har.setPinP("宝马");
har.jiaYou(20);
System.out.println("车的油量是" + har.getYouliang() + "升" + " " + "车的状态是" + har.getZhuangtai());
har.fadong();
har.jiaYou(10);
har.xingshi(100, 10);
System.out.println("车的油量是" + har.getYouliang() + "升" + " " + "车的状态是" + har.getZhuangtai());
har.jiaYou();
System.out.println("车的油量是" + har.getYouliang() + "升" + " " + "车的状态是" + har.getZhuangtai());
}
}
public class Har {
// 品牌
private String pinP;
// 读
public String getPinP() {
return pinP;
}
// 写的属性
public void setPinP(String pinP) {
System.out.println("车的品牌设置为" + pinP);
this.pinP = pinP;
}
// 状态
private String zhuangtai = "静止";
public String getZhuangtai() {
return zhuangtai;
}
// 当前油量属性
private double youliang = 0;
public double getYouliang() {
return youliang;
}
private double youxiang;
public void setYouxiang(double youxiang) {
this.youxiang = youxiang;
}
public double getYouxiang() {
return youxiang;
}
// 一次加满 方法的重载
public void jiaYou() {
System.out.println("一次加满");
double yjyl = this.youxiang - this.youliang;
jiaYou(yjyl);
}
// 加油
public void jiaYou(double youliang) {
// 邮箱容量大于0
if (youxiang <= 0) {
System.out.println("忘装邮箱了");
return;
}
// 加油量大于0
else if (youliang < 0) {
System.out.println("不要偷我的油");
return;
} else if (!zhuangtai.equals("静止")) {
System.out.println("车停了再加油");
return;
}
// 只有静止状态下才能加
// 不能超过邮箱油量
else if (this.youliang + youliang > this.youxiang) {
System.out.println("邮箱已经加满了,不要再加了");
double syyl = this.youliang;
this.youliang = this.youxiang;
System.out.println("这次加了" + (youxiang - syyl) + "升油");
}
else {
this.youliang += youliang;
}
System.out.println("这次加了" + youliang + "升油");
}
public void fadong() {
if (this.youliang <= 0) {
System.out.println("煤油了,请先加油");
}
else if (!this.getZhuangtai().equals("静止")) {
System.out.println("车已经发动");
} else {
this.zhuangtai = "发动";
System.out.println("车发动了");
}
}
private double zongli;
// 行驶
public double getZongli() {
return zongli;
}
public void xingshi(double lic,double youhao)
{//车的状态是发动
if(!this.getZhuangtai().equals("发动")){
System.out.println("请先发动汽车 ");
}
else{
//double zyh = lic * youhao / 100;
double lc = this.youliang *100 / youhao;
if(lc < lic){
System.out.println("最多行驶 " + lc+ "公里");
this.zongli += lc;
this.youliang = 0;
this.zhuangtai = "静止";
}
else{
System.out.println("行驶了" + lic + "公里");
this.zongli += lic;
this.zhuangtai = "发动";
}
}
// 计算总油耗,判断是否没油了
}
/*
* private String Zhuangtai() { // TODO 自动生成的方法存根 return null; }
*/
public Har()// 方法名和类名一致;无返回值不用void;
{
System.out.println("调用了构造方法");
}
// 重载
public Har(String pinP) {
System.out.println("实例化车的品牌" + pinP);
this.pinP = pinP;
}
public static void main(String[] args) {
Har har = new Har("宝马");// 默认构造方法
har.fadong();
har.setYouxiang(40);
// har.setPinP("宝马");
har.jiaYou(20);
System.out.println("车的油量是" + har.getYouliang() + "升" + " " + "车的状态是" + har.getZhuangtai());
har.fadong();
har.jiaYou(10);
har.xingshi(100, 10);
System.out.println("车的油量是" + har.getYouliang() + "升" + " " + "车的状态是" + har.getZhuangtai());
har.jiaYou();
System.out.println("车的油量是" + har.getYouliang() + "升" + " " + "车的状态是" + har.getZhuangtai());
}
}