老师帮忙看下是否满足题目要求?

老师帮忙看下是否满足题目要求?

package test;


public class Book {

// 属性:书名、作者、出版社、价格

private String bookNm;

private String author;

private String press;

private double price;


/**

要求:

1、 设计构造函数实现对属性赋值

2、 设置私有属性,get/set方法实现对属性的访问

3、 限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10

4、 限定作者、书名均为只读属性

5、信息介绍方法描述图书所有信息

 * */


//构造方法:

public Book(String bookNm,String author,String press,double price ) {

this.bookNm=bookNm;

this.author=author;

this.setPress(press);

this.setPrice(price);

}

public Book( ) {

}

//书名方法,只读

public String getBookNm() {

return bookNm;

}

//作者方法,只读

public String getAuthor() {

return author;

}



//出版社方法

public String getPress() {

return press;

}


public void setPress(String press) {

this.press = press;

}

//价格方法

public double getPrice() {

return price;

}


public void setPrice(double price) {

this.price = price;

}

//下半段信息介绍方法,描述图书所有信息

public  void BookInt(String bookNm,String author,String press,double price ) {

System.out.println("书名:"+bookNm);

System.out.println("作者:"+author);

System.out.println("出版社:"+press);

System.out.println("价格:"+price+"元");

}


}





package test;


public class BookTest {

public static void main(String[]args) {

//实例化对象

Book one=new Book("红楼梦","曹雪芹","人民文学出版社",20.0);

System.out.println("图书价格最低10元");

System.out.println("书名:"+one.getBookNm());

System.out.println("作者:"+one.getAuthor());

System.out.println("出版社:"+one.getPress());

//限定图书价格必须大于10,如果无效需进行提示,并强制赋值为10

if(one.getPrice()<10.0)

System.out.println("图书价格无效,价格应为10元");//如果无效需进行提示

else 

    System.out.println("价格:"+one.getPrice()+"元");

System.out.println("========================");

//图书2介绍,实例化对象

Book two=new Book();

two.BookInt("小李飞刀","古龙","中国长安出版社",55.5);



}


}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕酷酷 2019-07-30 13:32:25

同学你好,代码完成的不错,但是还有一个小问题:

  • 在限定图书价格时,可以将判断语句写入到Book类的setPrice()方法中,建议将if···else···语句中的语句块添加{},否则语句块中含有多条语句时,将只执行第一句。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


  • 提问者 weixin_慕先生706308 #1
    public void setPrice(double price) { if (price < 10) { //限定图书价格必须大于10,如果无效需进行提示, System.out.println("###价格无效###"); this.price = 10;// 并强制赋值为10 } else { this.price = price; } }
    2019-07-30 14:38:16
  • 提问者 weixin_慕先生706308 #2
    已改,谢谢老师
    2019-07-30 14:39:15
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师