书城代码
本文最后更新于 159 天前,其中的信息可能已经有所发展或是发生改变。

书城代码

version1

1. 思路

一共要创建两个类:

  • 一个是主类
  • 一个是创建书对象的类。

书城的大致思路如下:

  1. 展示书架

    • 通过2添加书籍后,把书籍加入合集并转换为book格式
    • 使用for循环遍历list,通过sout输出各个值
  2. 上架书籍

    • 创建新类:把书籍视为对象,创建书的编号,名字,作者这三个属性并使用构造器
    • 通过screen函数输入来自键盘的数据
    • 保存在合集之中
  3. 下架书籍

    • 输入一个想要删除的书目编号

    • 以书的编号为索引 ,开始在书中for循环遍历,如果找到了对应的数目,删除并退出循环

  4. 退出

    • 使用break函数退出循环

2. 代码

1. main函数(我自己是命名为meul)

package com.book;

import java.util.ArrayList;
import java.util.Scanner;

public class meul {
    public static void main(String[] args) {
        // 基础菜单--展示有什么功能,并与用户进行交互
        ArrayList list = new ArrayList();
        // 把书放在这个集合里暂时存储
       while (true){
           System.out.println("--欢迎来到书城");
           System.out.println("目录:");
           System.out.println("1. 展示书籍");
           System.out.println("2. 上架书籍");
           System.out.println("3. 下架书籍");
           System.out.println("4. 退出应用");
           Scanner sc = new  Scanner(System.in);
           // 初始化,并使用键盘输入的方式。之后也可以使用
           System.out.println("--请输入你想要实现功能所对应的序号:");
           int choice = sc.nextInt();
           // 赋值

           if (choice == 1){
               System.out.println("【书城】》》》1. 展示书籍");
               // 遍历
               for (int i=0;i <= list.size()-1;i++){
                   book b = (book)(list.get(i));
                   /*转换书籍的格式。有点看不明白。
                   list.get(i) 就是在没有泛式的情况下获取object格式的数据。
                   结合整句就是把集合的数据转换成book
                    */
                   System.out.println(b.getBnum()+"---"+b.getBname()+"---"+b.getBauthor());
               }
           }
           else if (choice ==2){
               //先把功能写出来
                System.out.println("【书城】》》》2. 上架书籍");
                   System.out.println("请输入书籍编号");
                   int num =  sc.nextInt();
                   System.out.println("请输入书籍名称");
                   String name =sc.next();
                   System.out.println("请输入书籍作者");
                   String author = sc.next();
                   //初始化类
                   book b =new book();
                   //把值塞进去
                   b.setBauthor(author);
                   b.setBname(name);
                   b.setBnum(num);
//集合。把塞进去的值存在列表里

               list.add(b);

           }
           else if (choice ==3){
               System.out.println("【书城】》》》3. 下架书籍");
               System.out.println("输入你要下架的书籍编号:");
               // 遍历,for循环,数值相同就删除
               int delnum = sc.nextInt();
               for (int i=0;i <= list.size()-1;i++) {
                   book b = (book) (list.get(i));
                   if (delnum == b.getBnum()){
                       list.remove(b);
                       System.out.println("书籍下架成功");
                       break;
                   }
               }
           }
           else if (choice == 4){
               System.out.println("【书城】》》》4. 退出应用");
               break;
               // 停止整个循环
           }
           else {
               break;
           }

       }
    }

}

2. book.class

package com.book;

public class book {
  //属性
    private int Bnum;
    private String Bname;
    private String Bauthor;
// 动作
//元素+get/set(因为使用private)+创造构造器+添加空类
    public String getBname() {
        return Bname;
    }

    public void setBname(String bname) {
        Bname = bname;
    }

    public int getBnum() {
        return Bnum;
    }

    public void setBnum(int bnum) {
        Bnum = bnum;
    }

    public String getBauthor() {
        return Bauthor;
    }

    public void setBauthor(String bauthor) {
        Bauthor = bauthor;
    }

    public book(int bnum, String bauthor, String bname) {
        this.Bnum = bnum;
        this.Bauthor = bauthor;
        this.Bname = bname;
    }

    public book() {
    }
}

version2(使用管道流)

1. 思路

2. 代码

package com.second;

import java.io.*;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

public class meul {

        public static void main(String[] args) throws IOException ,ClassNotFoundException    {
            // 基础菜单--展示有什么功能,并与用户进行交互
            ArrayList
<book> list = new ArrayList<>();
            while (true){
                System.out.println("--欢迎来到书城");
                System.out.println("目录:");
                System.out.println("1. 展示书籍");
                System.out.println("2. 上架书籍");
                System.out.println("3. 下架书籍");
                System.out.println("4. 退出应用");
                Scanner sc = new  Scanner(System.in);
                // 初始化,并使用键盘输入的方式。之后也可以使用
                System.out.println("--请输入你想要实现功能所对应的序号:");
                int choice = sc.nextInt();
                // 赋值

                if (choice == 1){
                    System.out.println("【书城】》》》1. 展示书籍");
                    File f = new File("e://book.txt");
                    if (f.exists() == true) { //假设文件存在,我们才能执行展示工作;不存在就直接进行友好提示,所以使用if-else判断

                        FileInputStream fis = new FileInputStream(f);
                        ObjectInputStream ois = new ObjectInputStream(fis);
                        list = (ArrayList
<book>) ois.readObject();
                        ois.close();

                        // 遍历
                        for (int i = 0; i <= list.size() - 1; i++) {
                            book b = (book) (list.get(i));
                   /*转换书籍的格式。有点看不明白。
                   list.get(i) 就是在没有泛式的情况下获取object格式的数据。
                   结合整句就是把集合的数据转换成book
                    */
                            System.out.println(b.getBnum() + "---" + b.getBname() + "---" + b.getBauthor());
                        }
                    }else {
                        System.out.println("当前没有书籍,请先进行上新");
                    }
                }
                else if (choice ==2){
                    //先把功能写出来
                    System.out.println("【书城】》》》2. 上架书籍");
                    System.out.println("请输入书籍编号");
                    int num =  sc.nextInt();
                    System.out.println("请输入书籍名称");
                    String name =sc.next();
                    System.out.println("请输入书籍作者");
                    String author = sc.next();
                    //初始化类
                    book b =new book();
                    //把值塞进去
                    b.setBauthor(author);
                    b.setBname(name);
                    b.setBnum(num);
//集合。把塞进去的值存在列表里
                    // 把书放在这个集合里暂时存储

                    list.add(b);
                    // 使用流
                    File f = new File("e://book.txt");
                    FileOutputStream fr = new FileOutputStream(f);
                    ObjectOutputStream oos = new ObjectOutputStream(fr);

                    oos.writeObject(list);
                    oos.close();

                }
                else if (choice ==3){
                    System.out.println("【书城】》》》3. 下架书籍");
                    System.out.println("输入你要下架的书籍编号:");

                    //将集合从文件取出
                    File f = new File("e://book.txt");
                    FileInputStream fis = new FileInputStream(f);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    list = (ArrayList
<book>) ois.readObject();
                    ois.close();
                    // 取出后遍历,for循环,数值相同就删除。删除对应编号书籍
                    int delnum = sc.nextInt();
                    for (int i=0;i <= list.size()-1;i++) {
                        book b = (book) (list.get(i));
                        if (delnum == b.getBnum()){
                            list.remove(b);
                            System.out.println("书籍下架成功");
                            break;
                        }
                    }
                    // 再把集合放回去
                    FileOutputStream fr = new FileOutputStream(f);
                    ObjectOutputStream oos = new ObjectOutputStream(fr);
                    oos.writeObject(list);
                    oos.close();

                }
                else if (choice == 4){
                    System.out.println("【书城】》》》4. 退出应用");
                    break;
                    // 停止整个循环
                }
                else {
                    break;
                }

            }
        }

    }
package com.second;

import java.io.Serializable;

public class book implements Serializable {
    //属性
    private int Bnum;
    private String Bname;
    private String Bauthor;
    // 动作
//元素+get/set(因为使用private)+创造构造器+添加空类
    public String getBname() {
        return Bname;
    }

    public void setBname(String bname) {
        Bname = bname;
    }

    public int getBnum() {
        return Bnum;
    }

    public void setBnum(int bnum) {
        Bnum = bnum;
    }

    public String getBauthor() {
        return Bauthor;
    }

    public void setBauthor(String bauthor) {
        Bauthor = bauthor;
    }

    public book(int bnum, String bauthor, String bname) {
        this.Bnum = bnum;
        this.Bauthor = bauthor;
        this.Bname = bname;
    }

    public book() {
    }
}

version3 使用mysql

1. 思路

2. 代码

感谢大家参观我的毛坯房。
暂无评论

发送评�? 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇