当前位置:网站首页>Write a Book class that has at least two attributes: name and price.
Write a Book class that has at least two attributes: name and price.
2022-07-21 04:51:00 【Get a cat first】
【 Problem description 】 Write a Book class , This class has at least name and price Two attributes . This class is to implement Comarable Interface , On the interface compareTo() The method specifies two Book The size relationship of class instances is the price Property size relationship . In the main function , Select the appropriate collection type to store Book Class , Then create a new Book Class object , And check which objects in the collection the object is equal to .
【 Input form 】 Enter one for each line Book Class object , Names and prices are separated by commas , The input end symbol of the collection class is #, Then enter a new Book Class object .
【 Output form 】
Show search results , If you find it , It displays the information of the specific object , Did not find , Do not show .
【 The sample input 1】
input several Book,in the end #
yuwen,10
shuxue,12
yingyu,11
input a new Book:
kexue,12
【 Sample output 1】
new book:kexue as same books
shuxue,12.0
【 The sample input 2】
input several Book,in the end #
yuwen,10
shuxue,12
waiyu,11
input a new Book:
kexue,13
【 Sample output 2】
new book:kexue as same books
We can learn from :
package Test;
import java.util.ArrayList;
import java.util.Scanner;
public class Mybook {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Define a Book Class , Used to store books put in
ArrayList<Book> book = new ArrayList<>();
System.out.println("input several Book,in the end #");
while(true){
// Circular judgement , And read in the stored data
String input =in.nextLine();
if (input.equals("#")){
break;
}
String[] a = input.split(",");// The title requires this as the dividing symbol , By "," The boundary information will be stored in a Array ,a[0],a[1]....
String a1 = a[0];
int a2 = Integer.parseInt(a[1]);// Convert to number
Book b = new Book();
b.setName(a1);//set Method
b.setPrice(a2);
book.add(b);// Add to dynamic array
}
System.out.println("input a new Book:");
String input1 = in.nextLine();
String[] c = input1.split(",");
String c1 = c[0];
int c2 = Integer.parseInt(c[1]);
Book b1 = new Book();
b1.setName(c1);
b1.setPrice(c2);
b1.compareTo(b1,book);// Calling method
in.close();
}
}
class Book implements Comarable{
String name;
double price;
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
}
// Defining interfaces , Define methods in interfaces
interface Comarable{
default void compareTo(Book b1,ArrayList<Book> books){
System.out.println("new book:<"+b1.name+">as same books");
// Loop through the dynamic array , And compare
for (Book book : books) {
if (b1.price == book.price) {
System.out.println(book.name + "," + book.price);
}
}
}
}
边栏推荐
猜你喜欢
与传统IT开发相比,低代码平台有何优势?
超全干货:数据可视化的设计总结,工具,技术要点统统都有
Keras calls plot_ Model error reporting solution
RNA 20. Single sample immune infiltration analysis (ssgsea) in SCI articles
JS基础--Math
DNA 7. 基因组拷贝数变异分析及可视化 (GISTIC2.0)
Jupyternotebook plug-in management and installation
Dynamic password lock based on stm32
Super dry goods: design summary, tools and technical points of data visualization are all available
动态调试JS代码
随机推荐
DNA 8. Mutation heterogeneity of cancer and search for new cancer driver genes (mutsigcv)
DNA 6. 基因组变异之绘制精美瀑布图(ComplexHeatmap)
[FPGA tutorial case 32] communication case 2 - FSK modulation signal generation based on FPGA
【FPGA教程案例31】通信案例1——基于FPGA的ASK调制信号产生
Study on immune related factors between TMB and ICI response in if:12+ different cancers
English introductory notes
NAT network address translation
JS基础--Math
À propos des outils d'édition XML
关于在服务器上部署Drupal网站的相关操作总结
Access数据库对象包括哪六个?Access与 Excel 最重要的区别是什么?
「Object.defineProperty」【面试业务必备】
With great power there must come great responsibility.
FigDraw 13. Mulberry graph of SCI article drawing and article reproduction (Sankey)
Principle and protection of DOM XSS
【云原生之kubernetes】kubernetes集群下初始化容器的使用方法
Install Apache manually
整数及浮点数在内存中的存储
SCS【1】今天开启单细胞之旅,述说单细胞测序的前世今生
wangEditor Uncaught (in promise) Error: 初始化节点已存在编辑器实例,无法重复创建编辑