[Java] 纯文本查看 复制代码 package cn.itcast.KuiHuaBaoDian4;
/**
* 一、需求说明:创建三个图书类对象,找出价格最高的图书并打印该图书的所有信息。
* 二、设计“图书类”Book,要求有以下属性:
* 图书编号:
* 书名:
* 价格:
**/
public class MainApp {
public static void main(String[] args) {
//创建三个图书对象
Book b1 = new Book("1001","金瓶梅",68.8);
Book b2 = new Book("1002","后宫秘史",99.8);
Book b3 = new Book("1003","寡妇门前是非多",79.8);
//调用最值方法求最贵的书并调用打印方法打印
printBook(mostExpensive(b1, b2, b3));
}
//求最值方法(简单的三个数)
public static Book mostExpensive(Book b1,Book b2,Book b3) {
if(b1.getPrice() > b2.getPrice()) {
if(b1.getPrice() > b3.getPrice()) {
return b1;
} else {
return b3;
}
} else if (b2.getPrice() > b3.getPrice()) {
return b2;
} else {
return b3;
}
}
//打印方法
public static void printBook(Book b) {
System.out.println("编号: " + b.getCode() + " 书名: '" +
b.getName() + "' 价格: " + b.getPrice());
}
}
package cn.itcast.KuiHuaBaoDian4;
/**
* 二、设计“图书类”Book,要求有以下属性:
* 图书编号:
* 书名:
* 价格:
**/
public class Book {
/**
* 成员变量
* */
//编号
private String code;
//书名
private String name;
//价格
private double price;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
/**
* 构造方法
* */
//无参
public Book() {
}
//有参
/*
public Book(String code,String name,double price) {
// this.code = code;
// this.name = name;
// this.price = price;
this.setCode(code);
this.setName(name);
this.setPrice(price);
}
*/
public Book(String code, String name, double price) {
super();
this.code = code;
this.name = name;
this.price = price;
}
}
|