日本java图书馆_菜鸡的Java笔记 图书馆

图书大厦

开发要求

现在要求模拟一个图书大厦图书管理的程序结构,可以在图书大厦实现某一类图书的上架操作,下架操作,以及关键字模糊查询的操作

注:只考虑类结构,图书属性只关注名字与价格

具体内容

分析:.........

范例:需要定义的是图书标准

interface Book{ // 准备出图书信息

public String getTitle(); // 得到书的名字

public double getPrice();//得到书的价钱

}

范例:定义图书大厦,一个图书大厦要保存有多本书的信息,所以图书大厦应该使用链表

classBookShop{private Link books = new LinkImpl();//表示的是所有的书

public void add(Book book){//上架图书

this.books.add(book);//向链表中保存数据

}public void delete(Book book){//下架图书

this.books.remove(book);

}publicLink search(String keyWord){

Link result= newLinkImpl();

Object[] obj= this.books.toArray();//将所有的数据转变为 Object 数组

for(int x = 0;x < obj.length; x ++){

Book book=(Book)obj[x];if(book.getTitle().contains(keyWord)){ //有此关键字

result.add(book);

}

}returnresult;

}

}

现在有了接口了,下面定义子类的时候只需要实现接口覆写方法即可。程序里面包含有 Link 接口的 remove() 方法

这个方法如果要想正常执行,则需要覆写 equals() 方法

范例:定义计算机类图书

class ComputerBook implementsBook{privateString title;private doubleprice;public ComputerBook(String title,doubleprice){this.title =title;this.price =price;

}public booleanequals(Object obj){if(this ==obj){return true;

}if(obj == null){return false;

}if(!(obj instanceofComputerBook)){return false;

}

ComputerBook b=(ComputerBook)obj;if(this.title.equals(b.title) && this.price ==b.price){return true;

}return false;

}publicString getTitle(){return this.title;

}public doublegetPrice(){return this.price;

}publicString toString(){return "【计算机类图书】名称 = "+this.title+",价格 = "+this.price;

}

}

范例:定义数字类图书

class MathBook implementsBook{privateString title;private doubleprice;public MathBook(String title,doubleprice){this.title =title;this.price =price;

}public booleanequals(Object obj){if(this ==obj){return true;

}if(obj == null){return false;

}if(!(obj instanceofMathBook)){return false;

}

MathBook b=(MathBook)obj;if(this.title.equals(b.title) && this.price ==b.price){return true;

}return false;

}publicString getTitle(){return this.title;

}public doublegetPrice(){return this.price;

}publicString toString(){return "【数学类图书】名称 = "+this.title+",价格 = "+this.price;

}

}

范例:进行代码的测试

interfaceLink{

}class LinkImpl implements Link{ //外部的程序只关心此类

}interface Book{//准备出图书信息

public String getTitle(); //得到书的名字

public double getPrice();//得到书的价钱

}classBookShop{private Link books = new LinkImpl();//表示的是所有的书

public void add(Book book){//上架图书

this.books.add(book);//向链表中保存数据

}public void delete(Book book){//下架图书

this.books.remove(book);

}publicLink search(String keyWord){

Link result= newLinkImpl();

Object[] obj= this.books.toArray();//将所有的数据转变为 Object 数组

for(int x = 0;x < obj.length; x ++){

Book book=(Book)obj[x];if(book.getTitle().contains(keyWord)){ //有此关键字

result.add(book);

}

}returnresult;

}

}class ComputerBook implementsBook{privateString title;private doubleprice;public ComputerBook(String title,doubleprice){this.title =title;this.price =price;

}public booleanequals(Object obj){if(this ==obj){return true;

}if(obj == null){return false;

}if(!(obj instanceofComputerBook)){return false;

}

ComputerBook b=(ComputerBook)obj;if(this.title.equals(b.title) && this.price ==b.price){return true;

}return false;

}publicString getTitle(){return this.title;

}public doublegetPrice(){return this.price;

}publicString toString(){return "【计算机类图书】名称 = "+this.title+",价格 = "+this.price;

}

}class MathBook implementsBook{privateString title;private doubleprice;public MathBook(String title,doubleprice){this.title =title;this.price =price;

}public booleanequals(Object obj){if(this ==obj){return true;

}if(obj == null){return false;

}if(!(obj instanceofMathBook)){return false;

}

MathBook b=(MathBook)obj;if(this.title.equals(b.title) && this.price ==b.price){return true;

}return false;

}publicString getTitle(){return this.title;

}public doublegetPrice(){return this.price;

}publicString toString(){return "【数学类图书】名称 = "+this.title+",价格 = "+this.price;

}

}public classactualCombat{public static voidmain(String args[]){

BookShop shop= newBookShop();

shop.add(new ComputerBook("java开发",79.0));

shop.add(new ComputerBook("java数据库编程",69.0));

shop.add(new ComputerBook("java网络编程",76.0));

shop.add(new ComputerBook("数学与java",59.0));

shop.add(new ComputerBook("java与线性代数",49.0));

shop.add(new ComputerBook("网络数学",29.0));

shop.delete(new ComputerBook("java数据库编程",69.0)); //下架操作

Link tepm = shop.search("java"); //模糊查询

Object obj[] = tepm.toArray(); //变为对象数组

for(int x = 0;x < obj.length;x ++){

System.out.println(obj[x]);

}

}

}

这样的程序模型可以在生活中不断演变,例如:一个公园可以有很多的树,种树和砍

一个停车场里可以停放轿车,卡车,电动车

总结

这样的操作模型之中,对于链表只是使用

本程序是以接口为主的编程操作,这种形式在开发中随处可见

下面是一个简单的Java实现: ``` import java.util.*; public class WolfSheepCabbage { public static void main(String[] args) { // 初始状态:false表示在左岸,true表示在右岸 boolean wolf = false; // 狼 boolean sheep = false; // 羊 boolean cabbage = false; // 菜 boolean boat = false; // 船 Scanner scanner = new Scanner(System.in); while (true) { // 输出当前状态 System.out.println("左岸:狼 " + (wolf ? "✅" : "❎") + " 羊 " + (sheep ? "✅" : "❎") + " 菜 " + (cabbage ? "✅" : "❎")); System.out.println("右岸:狼 " + (!wolf ? "✅" : "❎") + " 羊 " + (!sheep ? "✅" : "❎") + " 菜 " + (!cabbage ? "✅" : "❎")); // 判断是否完成任务 if (!wolf && !sheep && !cabbage) { System.out.println("恭喜你完成任务!"); break; } // 输出提示信息 System.out.println("请选择要运输的物品(1:狼,2:羊,3:菜),或者选择运输船(4:船):"); // 获取用户输入 int choice = scanner.nextInt(); // 判断用户选择 switch (choice) { case 1: // 运输狼 if (sheep == cabbage && sheep != wolf) { System.out.println("狼会吃掉羊或菜,操作无效!"); } else { wolf = !wolf; boat = !boat; } break; case 2: // 运输羊 if (cabbage == wolf && cabbage != sheep) { System.out.println("羊会吃掉菜,操作无效!"); } else { sheep = !sheep; boat = !boat; } break; case 3: // 运输菜 if (sheep == wolf && sheep != cabbage) { System.out.println("狼会吃掉羊或菜,操作无效!"); } else { cabbage = !cabbage; boat = !boat; } break; case 4: // 运输船 if (wolf == sheep && wolf != cabbage) { System.out.println("狼会吃掉羊或菜,操作无效!"); } else if (boat && (!wolf || !sheep || !cabbage)) { System.out.println("船上必须有人!"); } else { boat = !boat; } break; default: System.out.println("输入无效!"); break; } } } } ``` 在程序运行过程中,会输出当前状态,然后提示用户选择要运输的物品或船。根据用户的选择,更新当前状态,直到完成任务。需要注意的是,在运输船的时候,必须有人在船上,否则操作无效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值