在线小说阅读

package com.test_1129.com.bean;


import java.io.Serializable;

/*小说类*/
public class Fiction implements Serializable {

    private String id;
    private String name;
    private String title;
    private String content;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Fiction(){

    }

    public Fiction(String id, String name, String title, String content) {
        this.id = id;
        this.name = name;
        this.title = title;
        this.content = content;
    }

    @Override
    public String toString() {
        return "Fiction{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}




package com.test_1129.com.bean;


import java.io.Serializable;

/*用户类*/
public class User implements Serializable {

    private String id;
    private String username;
    private String password;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(){}

    public User(String id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}




package com.test_1129.com.dao.impl;

import com.test_1129.com.bean.Fiction;
import com.test_1129.com.dao.FictionDao;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FictionDaoImpl implements FictionDao {
    static List<Fiction> fictionList = new ArrayList<>();

    static {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("D:\\fs\\data\\fiction.txt");
            ois= new ObjectInputStream(fis);
            fictionList = (List<Fiction>) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if(ois != null){
                    ois.close();
                }
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void reload() throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\fs\\data\\fiction.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(fictionList);
    }

    @Override
    public void checkNovelsList() {
        int i=1;
        for (Fiction fic:fictionList){
            System.out.println(i+"."+"名称:"+fic.getTitle()+" 作者:"+fic.getName());
            i++;
    }
}

    @Override
    public void addFiction(Fiction fiction) {
        fictionList.add(fiction);
        //重新加载
        try {
            reload();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}





package com.test_1129.com.dao.impl;

import com.test_1129.com.bean.User;
import com.test_1129.com.dao.UserDao;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    static List<User> userList = new ArrayList<>();

    static {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("D:\\fs\\data\\user.txt");
            ois= new ObjectInputStream(fis);
            userList = (List<User>) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if(ois != null){
                    ois.close();
                }
                if(fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /*重新加载*/
    public void reload() throws IOException {
        FileOutputStream fos = new FileOutputStream("D:\\fs\\data\\user.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(userList);
    }



    @Override
    public User selectUserByUsername(String username) {
        for(User user : userList){
            if(user.getUsername().equals(username)){
                return user;
            }
        }
        return  null;
    }

    @Override
    public void addUser(User user) {
        userList.add(user);
        //重新加载
        try {
            reload();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



package com.test_1129.com.dao;

import com.test_1129.com.bean.Fiction;
import com.test_1129.com.bean.User;

public interface FictionDao {
    public void checkNovelsList();
    public void addFiction(Fiction fiction);
}



package com.test_1129.com.dao;

import com.test_1129.com.bean.User;

public interface UserDao {

    public User selectUserByUsername(String username);

    public void addUser(User user);
}




package com.test_1129.com.init;

import com.test_1129.com.bean.Fiction;
import com.test_1129.com.bean.User;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
//private int id;
//private String name;
//private String title;
//private String content;
public class FictionInit {
    public static void main(String[] args) throws IOException {
        List<Fiction> fictionList = new ArrayList<>();
        fictionList.add(new Fiction("1","张三","阿斯顿","你好,哈哈哈"));
        fictionList.add(new Fiction("2","李四","请问饿","谢谢谢谢"));
        fictionList.add(new Fiction("3","admin","ASD","SDDF"));
        fictionList.add(new Fiction("4","lucy","lucy","sfsad"));

        FileOutputStream fos = new FileOutputStream("D:\\fs\\data\\fiction.txt");

        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(fictionList);
    }
}




package com.test_1129.com.init;

import com.test_1129.com.bean.User;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

/*用户数据的初始化*/
public class UserInit {

    public static void main(String[] args) throws IOException {
        List<User> userList = new ArrayList<>();
        userList.add(new User("1","zhangsan","123456"));
        userList.add(new User("2","zhanghang","111111"));
        userList.add(new User("3","admin","admin"));
        userList.add(new User("4","lucy","lucy"));

        FileOutputStream fos = new FileOutputStream("D:\\fs\\data\\user.txt");

        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(userList);



    }
}




package com.test_1129.com.service.impl;

import com.test_1129.com.bean.Fiction;
import com.test_1129.com.dao.FictionDao;
import com.test_1129.com.dao.FictionDao;
import com.test_1129.com.dao.impl.FictionDaoImpl;
import com.test_1129.com.dao.impl.UserDaoImpl;
import com.test_1129.com.service.FictionService;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class FictionServiceImpl implements FictionService {
    private FictionDao fictionDao =  new FictionDaoImpl();


    @Override
    public void  readNovel(String novelname,String name) {
        File file=new File("D:\\fs\\data\\"+novelname+"_"+name+".txt");
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file));
            String str;
            while ((str=reader.readLine())!=null){

                System.out.println(str);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void upload(String novelname, String name,String path) {
        File file=new File("D:\\fs\\data\\"+novelname+"_"+name+".txt");
        File file1=new File(path);
        BufferedWriter writer = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file1));
            writer = new BufferedWriter(new FileWriter(file));
            String str;
            String str1="";
            while ((str=reader.readLine())!=null){
                str1=str1+str;
            }
            writer.write(str1);
            writer.flush();
            Fiction fiction=new Fiction();
            fiction.setContent(str1);
            fiction.setName(name);
            fiction.setTitle(novelname);
            fiction.setId(UUID.randomUUID().toString().replace("-",""));
            fictionDao.addFiction(fiction);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void Download(String novelname, String name,String Downloadpath) {
        File file=new File("D:\\fs\\data\\"+novelname+"_"+name+".txt");
        File file1=new File(Downloadpath);
        BufferedWriter writer = null;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(file1));
            writer = new BufferedWriter(new FileWriter(file));
            String str;
            String str1="";
            while ((str=reader.readLine())!=null){
                str1=str1+str;
            }
            writer.write(str1);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//    private String id;
//    private String name;
//    private String title;
//    private String content;



package com.test_1129.com.service.impl;

import com.test_1129.com.bean.User;
import com.test_1129.com.dao.UserDao;
import com.test_1129.com.dao.impl.UserDaoImpl;
import com.test_1129.com.service.UserService;

import java.util.UUID;

/*业务依赖于持久层*/
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    @Override
    public String login(String username, String password) {
        User user = userDao.selectUserByUsername(username);
        if(user == null){
            return "账号未注册";
        }
        if(!user.getPassword().equals(password)){
            return "密码错误";
        }
        return null;
    }

    @Override
    public String register(String username, String password) {
        User user = userDao.selectUserByUsername(username);
        if(user != null){
            return  "账号已注册!";
        }

        user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setId(UUID.randomUUID().toString().replace("-",""));
        userDao.addUser(user);
        return "注册成功!";
    }
}




package com.test_1129.com.service;

import com.test_1129.com.bean.Fiction;
import com.test_1129.com.bean.User;

public interface FictionService {
    public void readNovel(String novelname,String name);
    public void upload(String novelname,String name,String path);
    public void Download(String novelname,String name,String Downloadpath);

}




package com.test_1129.com.service;

public interface UserService {

    public String login(String username,String password);

    public String register(String username,String password);
}




package com.test_1129.com.view;


import com.test_1129.com.bean.User;
import com.test_1129.com.dao.FictionDao;
import com.test_1129.com.dao.impl.FictionDaoImpl;
import com.test_1129.com.service.FictionService;
import com.test_1129.com.service.UserService;
import com.test_1129.com.service.impl.FictionServiceImpl;
import com.test_1129.com.service.impl.UserServiceImpl;

import java.util.Scanner;

public class SystemView {

    private static UserService userService = new UserServiceImpl();
    private static FictionService fictionService = new FictionServiceImpl();
    private static FictionDao fictionDao =  new FictionDaoImpl();
    public static void main(String[] args) {
        index();
        //Downloadnovel();
    }//C:\Users\86195\Desktop\lht.txt

    public static void  index(){
        while(true){
            System.out.println("----------欢迎使用飞思小说阅读系统----------");
            System.out.println("         1.登录         2.注册      3.查看小说列表      4.读小说      5.上传     6.下载");
            Scanner input = new Scanner(System.in);
            String select = input.next();
            switch (select){
                case "1":
                    login();
                    break;
                case "2":
                    register();
                    break;
                case "3":
                    checkNovelsListall();
                    break;
                case "4":
                    read();
                    break;
                case "5":
                    uploadnovel();
                    break;
                case "6":
                    Downloadnovel();
                    break;
                default:
                    System.out.println("输入错误!");
                    break;
            }
        }

    }

    public static void login(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入账号:");
        String username = input.next();
        System.out.println("请输入密码:");
        String password = input.next();
        String msg = userService.login(username,password);
        System.out.println(msg);
    }

    public static void register(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入账号:");
        String username = input.next();
        System.out.println("请输入密码:");
        String password = input.next();
        String msg = userService.register(username,password);
        System.out.println(msg);
    }
    public static void checkNovelsListall(){

        fictionDao.checkNovelsList();
    }
    public static void read(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入名称:");
        String novelname = input.next();
        System.out.println("请输入作者:");
        String name = input.next();
        fictionService.readNovel(novelname,name);
    }
    public static void uploadnovel(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入名称:");
        String novelname = input.next();
        System.out.println("请输入作者:");
        String name = input.next();
        System.out.println("请输入路径:");
        String path = input.next();
        fictionService.upload(novelname,name,path);
    }
    public static void Downloadnovel(){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入名称:");
        String novelname = input.next();
        System.out.println("请输入作者:");
        String name = input.next();
        System.out.println("请输入路径:");
        String path = input.next();
        fictionService.upload(novelname,name,path);
    }
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值