小说阅读器`
刚学了java
第一次做的小系统 !
有瑕疵见谅
提前准备好一个文件夹Reabooooook
Readbooooook下Bookcase (书架) Bookmall(书城) register(注册) 文件
Bookcase下 各个分类文件 分类文件里menu.txt 以及两本书的内容
Bookmall同上
register下register.txt
package System;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Readbook {
/**
- 判断是否为vip 下载是否收费
*/
Boolean bool ;
/**
/**
/**
/**
- 注册的方法
*/
public void Regist(){
Scanner sc = new Scanner(System.in);
Register reg = new Register();
FileWriter fw = null;
/**
- 注册并存入register.txt文件里
*/
try {
File file = new File(“D:\Readbooooook\register\register.txt”);
// file.mkdirs();
fw = new FileWriter(file,true);
while(true){
System.out.println(“请输入用户名:”);
String name = sc.next();
reg.setName(name);
fw.write(name);fw.write(",");
System.out.println(“请输入密码:”);
String pwd1 = sc.next();
fw.write(pwd1);fw.write(";");
System.out.println(“请再次输入密码:”);
String pwd2 = sc.next();
if(pwd1.equals(pwd2)){
System.out.println(“注册成功!\n”);
reg.setPwd1(pwd1);
reg.setPwd2(pwd2);
break;
}else {System.out.println(“注册失败,请重新输入”);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 登录方法
*/
public void log(){
Scanner sc = new Scanner(System.in);
int i = 0;
while(i<5){
System.out.println(“请输入用户名”);
String name = sc.next();
System.out.println(“请输入密码”);
String password = sc.next();
boolean bo = login(name,password);
if(botrue){
System.out.println("登陆成功 ");break;
}else{
System.out.println(“用户名/密码错误,请重新输入,还有”+(4-i)+“次机会”);
i++;
}
}if(i5){
System.out.println(“很抱歉用户!您的错误次数已用完!”);
}
}
/**
- 判断是否登录成功的方法
- 与存在register里的数据进行比较
- @param name
- @param password
- @return
*/
public boolean login(String name,String password){
boolean isSuccess = false;
try {
fis = new FileReader(“D:\Readbooooook\register\register.txt”);
char[] c = new char[1024];
StringBuffer sbffer = new StringBuffer();
int length = 0;
while((length = fis.read©) != -1){
sbffer.append(c,0,length);
}
String str = sbffer.toString(); //账号,密码
String[] userArr = str.split(";");
for(int i = 0 ; i < userArr.length ; i++){
//在此处可以验证登录成功与否
String[] userMsg = userArr[i].split(",");
if(userMsg[0].equals(name)&&userMsg[1].equals(password)){
isSuccess = true;break;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return isSuccess;
}
/**
- 书城的方法
- 选择后直接调各自类型的方法
*/
public void bookmall(){
Scanner sc = new Scanner(System.in);
System.out.println(“请选择小说类型:\n1.古代\t2.竞技\t3.玄幻\t4.免费”);
int a = sc.nextInt();
switch(a){
case 1:gudai();break;
case 2:jingji();break;
case 3:xuanhuan();break;
case 4:free();break;
} }
/**
- 古代书类的方法
/
public void gudai(){
Scanner sc = new Scanner(System.in);
FileReader fr = null;
try{
/*
- 读取古代书类的menu
*/
fr = new FileReader(“D:\Readbooooook\Bookmall\古代\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read©)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
//选择书
System.out.println(“请选择感兴趣的小说:”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已选择【雪中悍刀行】”);
bookname = “雪中悍刀行”;
fi = new File(“D:\Readbooooook\Bookmall\古代\雪中悍刀行.txt”);
break;
case 2:System.out.println(“您已选择【长安十二时辰】”);
bookname = “长安十二时辰”;
fi = new File(“D:\Readbooooook\Bookmall\古代\长安十二时辰.txt”);
break;
default:System.out.println(“您选择的书籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 竞技书类的方法
*/
public void jingji(){
Scanner sc = new Scanner(System.in); FileReader fr = null;
try{
fr = new FileReader(“D:\Readbooooook\Bookmall\竞技\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read©)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
System.out.println(“请选择感兴趣的小说:”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已选择【全职高手】”);
bookname = “全职高手”;
fi = new File(“D:\Readbooooook\Bookmall\竞技\全职高手.txt”);
break;
case 2:System.out.println(“您已选择【英雄联盟之谁与争锋】”);
bookname = “英雄联盟之谁与争锋”;
fi = new File(“D:\Readbooooook\Bookmall\竞技\英雄联盟之谁与争锋.txt”);
break;
default:System.out.println(“您选择的书籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 玄幻书类的方法
*/
public void xuanhuan(){
Scanner sc = new Scanner(System.in); FileReader fr = null;
try{
fr = new FileReader(“D:\Readbooooook\Bookmall\玄幻\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read©)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
System.out.println(“请选择感兴趣的小说:”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已选择【吞噬星空】”);
bookname = “吞噬星空”;
fi = new File(“D:\Readbooooook\Bookmall\玄幻\吞噬星空.txt”);
break;
case 2:System.out.println(“您已选择【星辰变】”);
bookname = “星辰变”;
fi = new File(“D:\Readbooooook\Bookmall\玄幻\星辰变.txt”);
break;
default:System.out.println(“您选择的书籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 免费书类的方法
*/
public void free(){
Scanner sc = new Scanner(System.in);
FileReader fr = null;
bool = true;//无论是否vip都免费 try{
fr = new FileReader(“D:\Readbooooook\Bookmall\免费\menu.txt”);
char[] c = new char[1024];
int length = 0;
while((length = fr.read©)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}
System.out.println(“请选择感兴趣的小说:”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“您已选择【斗罗大陆】”);
bookname = “斗罗大陆”;
fi = new File(“D:\Readbooooook\Bookmall\免费\斗罗大陆.txt”);
break;
case 2:System.out.println(“您已选择【斗破苍穹】”);
bookname = “斗破苍穹”;
fi = new File(“D:\Readbooooook\Bookmall\免费\斗破苍穹.txt”);
break;
default:System.out.println(“您选择的书籍不存在!”);
}
}catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void print(){
/**
- 选择功能的方法
/
Scanner sc = new Scanner(System.in);
System.out.println(“1.添加至书架\n2.立即阅读\n3.立即下载”);
int a = sc.nextInt();
switch(a){
case 1:System.out.println(“添加成功!跳转至书架”);
add();bcase();
break;
case 2:read();
System.out.println("********************");
break;
case 3:load();break;
}
}
public void add( ){
/**
- 添加到书架的方法
*/
FileReader fr = null; //读小说
FileWriter fw = null;
FileWriter fww = null; //复制小说
File fff = null;
i++; //每添加一次书架 i+1
try {
fr = new FileReader(fi); //读到源文件里的小说 // 书名写进书架\目录.txt里
fff = new File(“D:\Readbooooook\Bookcase\menu.txt”);
fw = new FileWriter(fff,true);
fw.write(i+".");
fw.write(bookname);
//小说内容复制到书架\new.txt里
char[] c = new char[1024];
int len = 0;
while((len=fr.read©)!=-1){
String str = new String(c,0,len);
//根据书架的第几本书自动建新new i 文件
fww = new FileWriter(“D:\Readbooooook\Bookcase\new”+i+".txt");
fww.write(str);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fww.close();
fw.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
-
立即阅读的方法
*/
public void read(){
FileReader fr = null;
try {
fr = new FileReader(fi);
int length = 0;
char[] c = new char[1024];
while((length = fr.read©)!=-1){
String str = new String(c,0,length);
System.out.println(str);
}System.out.println("<<<小说阅读完成>>>"); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- 下载小说的方法
*/
public void load(){ Scanner sc = new Scanner(System.in);
if(boolfalse){
//false则需要花钱
System.out.println("<本次下载自动免密支付1书币!>");
System.out.println("<<自动添加至书架>>");
}else if(booltrue){
//true就免费
System.out.println("<免费下载并添加到书架成功!>");
}
//下载完成自动添加至书架
add();
System.out.println("[是否立即阅读?(y/n)否则跳转至书架]");
String a = sc.next();
if(a.equals(“y”)){
read();
}else{
bcase();
}
}
/**
- 书架的方法
*/
public void bcase(){ Scanner sc = new Scanner(System.in);
FileReader fr = null;
FileReader fr1 = null; char[] c = new char[1024];
int length = 0;
try {
//显示书架目录
fr = new FileReader(“D:\Readbooooook\Bookcase\menu.txt”);
while((length = fr.read©)!=-1){
String string = new String(c,0,length);
System.out.println(string);
}
System.out.println("[阅读1 or 退出2]");
int i = sc.nextInt();
if(i==1){
System.out.println(“请选择书籍[编号]”);
int a = sc.nextInt();
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>\n");
//书架里第一本书的读取
fr1 = new FileReader(“D:\Readbooooook\Bookcase\new”+a+".txt");
while((length = fr1.read©)!=-1){
String s = new String(c,0,length);
System.out.println(s);
}System.out.println("-----小说阅读完成-----");
}else if(i==2){
System.out.println("<退出书架!>");
}else {
System.out.println("<<输入错误!>>");
//错误则重新走书架方法
bcase();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fr.close();
if(fr1 != null){
fr1.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
- vip方法
*/
public void VIP(){
Scanner sc = new Scanner(System.in);
System.out.println(“亲是否充值vip(๑¯∀¯๑)?可享受全场小说下载免费!”);
String a = sc.next();
if(a.equals(“是”)){
//充值赋值bool为true
bool = true;
System.out.println(“是否立即支付y/n”);
String b = sc.next();
if(b.equals(“y”)){
System.out.println(“免密支付成功!”);
}else {
System.out.println(“支付失败”);
//支付不成功则再次赋值false
bool = false;
}
}else{
bool = false;
}
}
}
`
主方法
【不知道为什么一复制过来就乱了