目录
前言
花了一周的时间写了一个管理项目-起点阅读系统,写这个项目并不难,涉及的知识点不多,很容易写出来,但又很不好写。
下面是管理项目的要求,有兴趣的小伙伴可以写写。
管理项目
1、自定义一个管理系统(图书管理系统和学生管理系统除外)。
2、要求:用户与管理员分离,实现增删改查,数据存储到文件里。
3、设计一个项目使用文档。
4、本次考核使用自己的电脑编写程序、统一使用idea进行编写。
5、将所有内容提交到Git中、考核时使用学长电脑从Git中拉取你们的项目,由自己讲述项目的实现及使用的技术点。
步骤
1,要先想一个思路和大致的步骤,确定大的方向正确,不然写的后面万一有更好的思路或这个代码有大的错误,都需要重新在写,很麻烦。
2,然后再搭建结构,把大致的框架写出来,分清楚要写几个类,每个类有什么功能等等
3,然后再填充细节,修改优化代码,这一步可能是最难的。
实现
我的想法是写一个起点阅读系统。
用户可以注册、登录和注销账号,登录后可以查看书籍信息、返回上一级页面、退出。
管理员可以可以注册、登录和注销账号,登录后可以查看书籍信息、增加数据信息···返回上一级页面、查看账号信息等等
书籍信息分为书名、作者、风格、月票数,同时选中书名就可以看书了(当然看书功能还没写,不能实现)
删除、修改、增加书籍信息都储存在集合中,退出系统时将信息在写入文件,并且一开始就从文件里读数据储存在集合中,避免数据丢失。
第一个类:测试类,调出开始页面
package system;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public class text {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
io1.read();//将储存账号的文件里的信息写到HashMap集合中,便于登录
ye.ye1();//调用页面的方法
}
}
运行:
-----欢迎使用起点阅读系统----
1 登录账号
2 注册账号
3 注销账号
4 退出系统
请输入你的选择: 4
进程已结束,退出代码为 1
这个功能是调用ye类的ye1方法实现的
第2个类:ye类实现账号的登录,注册,注销,和退出系统
package system;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Scanner;
//这个类用于登录,注册,注销和退出系统
public class ye {
static String account = "z";//定义管理员账号
static String password = "123";//定义密码
static HashMap<String, String> hm = new HashMap<>();
static Scanner scanner = new Scanner(System.in);
static String s2;//这个用于下一个页面
//此方法功能是显示页面,根据输入的数字,实现不同的功能
public static void ye1() throws InstantiationException, IllegalAccessException, IOException, InvocationTargetException, NoSuchMethodException {
while (true) {
System.out.println("-----欢迎使用起点阅读系统----");
System.out.println(" 1 登录账号");
System.out.println(" 2 注册账号");
System.out.println(" 3 注销账号");
System.out.println(" 4 退出系统 ");
System.out.print("请输入你的选择: ");
int a = scanner.nextInt();
switch (a) {
case 1:
login();
break;
case 2:
register();
break;
case 3:
logout();
break;
case 4:
io1.writer();
io.writer();
System.exit(1);
default:
System.out.println("你输入错误,请重新输入!");
}
}
}
//此方法实现登录功能
public static void login() throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
int z = 0, c = 0;
hm.put(account, password);
System.out.println("登录");
Scanner s1 = new Scanner(System.in);
System.out.print("请输入名字:");
s2 = s1.nextLine();
System.out.print("请输入密码:");
String s3 = s1.nextLine();
//判断账号是否正确
for (String s : hm.keySet()) {
if (s.equals(s2) && hm.get(s).equals(s3)) {
//判断账号是否为管理员的账号
if (s2.equals(account) && s3.equals(password)) z++;
c++;
}
}
//根据判断实现管理员和用户分离
if (z != 0) {
function.function2();//调用管理员的页面
} else if (c == 0) System.out.println("登录账号或密码错误!");
else {
function.function1();//调用用户的页面
}
}
//此方法实现注册
public static void register() {
System.out.println("注册");
System.out.print("请输入账号:");
String s4 = scanner.next();
if (hm.containsKey(s4)) {
System.out.println("该账号已注册,请重新注册!");
} else {
System.out.print("请输入密码:");
String s5 = scanner.next();
hm.put(s4, s5);
System.out.println("注册成功");
}
}
//此方法实现注销
public static void logout() {
System.out.println("请输入要注销的账号:");
String z1 = scanner.next();
if (hm.containsKey(z1)) {
System.out.println("请输入密码:");
String z2 = scanner.next();
if (hm.get(z1).equals(z2)) {
hm.remove(z1);
System.out.println("注销成功!");
} else System.out.println("密码错误,请重新注销");
} else System.out.println("无此账号,请重新注销!");
}
}
第3个类:书籍信息的类
package system;
public class shu {
private String name;
private String writer;
private String tyge;
private int yp;
public shu() {
}
public shu(String name, String writer, String tyge, int yp) {
this.name = name;
this.writer = writer;
this.tyge = tyge;
this.yp = yp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
public String getTyge() {
return tyge;
}
public void setTyge(String tyge) {
this.tyge = tyge;
}
public int getYp() {
return yp;
}
public void setYp(int yp) {
this.yp = yp;
}
}
第4个类:实现增删改查等等功能
package system;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class function {
public static int a = 0;
static HashMap<String, shu> hm = new HashMap<>();
public function() {
}
static Scanner scanner = new Scanner(System.in);
//管理员的页面
public static void function2() throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
io.read();
while (true) {
System.out.println("------欢迎管理员" + ye.s2 + "------");
System.out.println(" 1 查看书库信息");
System.out.println(" 2 添加书籍信息");
System.out.println(" 3 删除书籍信息");
System.out.println(" 4 修改书籍信息");
System.out.println(" 5 退出系统");
System.out.println(" 6 查看账号信息");
System.out.println(" 7 回退上一个页面");
System.out.print("请你选择功能: ");
int i = scanner.nextInt();
switch (i) {
case 1:
ck();
break;
case 2:
zj();
break;
case 3:
sc();
break;
case 4:
xg();
break;
case 5:
System.out.println("退出成功,欢迎下次使用");
io1.writer();
io.writer();
System.exit(1);
case 6:
io1.writer();
io1.read();
ck1();
break;
case 7:
ye.ye1();
System.out.println("回退成功");
break;
default:
System.out.println("你输入错误!");
}
System.out.println();
}
}
//用户的页面
public static void function1() throws IOException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
while (true) {
System.out.println("-------欢迎用户" + ye.s2 + "-------");
System.out.println(" 1 查看书库信息");
System.out.println(" 2 退出系统");
System.out.println(" 3 回退上一个页面");
System.out.print("请你选择功能: ");
int i = scanner.nextInt();
switch (i) {
case 1:
ck();
break;
case 2:
System.out.println("退出成功,欢迎下次使用");
io1.writer();
io.writer();
System.exit(1);
case 3:
ye.ye1();
System.out.println("回退成功");
break;
default:
System.out.println("你输入错误!");
}
System.out.println();
}
}
//实现查看书籍信息
public static void ck() {
System.out.println("-------查看书籍信息------");
if (!hm.isEmpty()) {
Set<String> z = hm.keySet();
for (String s : z) {
shu x = hm.get(s);
System.out.println("书名:" + x.getName() + " 作者:" + x.getWriter() + ", 风格:" + x.getTyge() + ", 月票:" + x.getYp());
}
} else {
System.out.println("现在还没有书籍,请先增加书籍");
a++;
}
}
//实现增加书籍信息
public static void zj() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
ck();
System.out.println("------添加书籍信息------");
System.out.print("请输入添加的书名:");
String z1 = scanner.next();
if (hm.containsKey(z1)) {
System.out.println("已有此书籍");
return;
}
System.out.print("请输入作者:");
String z2 = scanner.next();
System.out.print("请输入风格:");
String z3 = scanner.next();
System.out.print("请输入月票数:");
int z4 = scanner.nextInt();
Class<shu> shuClass = shu.class;
Constructor<shu> constructor = shuClass.getConstructor(String.class, String.class, String.class, int.class);
shu shu = constructor.newInstance(z1, z2, z3, z4);
hm.put(z1, shu);
System.out.println("添加成功");
}
//实现删除书籍信息
public static void sc() {
a = 0;
ck();
if (a == 0) {
System.out.println("------删除书籍信息------");
System.out.print("请输入删除的书名:");
String z1 = scanner.next();
if (hm.containsKey(z1)) {
hm.remove(z1);
System.out.println("删除成功");
} else System.out.println(" 删除失败,无此书籍!");
}
}
//实现修改书籍信息
public static void xg() {
a = 0;
ck();
if (a == 0) {
System.out.println("------修改书籍信息------");
System.out.print("请输入修改的书名:");
String z = scanner.next();
if (hm.containsKey(z)) {
shu shu = hm.get(z);
System.out.print("请输入修改后书名:");
shu.setName(scanner.next());
System.out.print("请输入修改后作者:");
shu.setWriter(scanner.next());
System.out.print("请输入修改后风格:");
shu.setTyge(scanner.next());
System.out.print("请输入修改后月票数:");
shu.setYp(scanner.nextInt());
System.out.println("修改成功");
} else System.out.println(" 修改失败,无此书籍!");
}
}
//实现查看账号信息
private static void ck1() {
System.out.println("------查看账号信息------");
Set<String> z = ye.hm.keySet();
for (String s : z) {
System.out.println("账号:" + s + ", 密码:" + ye.hm.get(s));
}
}
}
第5个类:io流类,储存和读数据数据和账号数据
package system;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@SuppressWarnings("all")//实现书籍信息的读和写
public class io {
public static void writer() throws IOException {
File file = new File("..//xm//src//system//shuck.txt");
file.delete();
BufferedWriter writer = new BufferedWriter(new FileWriter("..//xm//src//system//shuck.txt", true));
for (shu shu : function.hm.values()) {
writer.write(shu.getName() + " " + shu.getWriter() + " " + shu.getTyge() + " " + shu.getYp());
writer.newLine();
}
writer.close();
}
public static void read() throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
BufferedReader reader = new BufferedReader(new FileReader("..//xm//src//system//shuck.txt"));
String s, s1;
Class<shu> shuClass = shu.class;
Constructor<shu> constructor = shuClass.getConstructor(String.class, String.class, String.class, int.class);
while ((s = reader.readLine()) != null) {
if (!s.equals("")) {
String[] a = s.split(" ");
shu shu = constructor.newInstance(a[0], a[1], a[2], Integer.parseInt(a[3]));
function.hm.put(a[0], shu);
}
}
reader.close();
}
}
@SuppressWarnings("all")//实现账号信息的读和写
class io1 {
public static void writer() throws IOException {
File file = new File("..//xm//src//system//account.txt");
file.delete();
BufferedWriter writer = new BufferedWriter(new FileWriter("..//xm//src//system//account.txt", true));
for (String shu : ye.hm.keySet()) {
writer.write(shu + " " + ye.hm.get(shu));
writer.newLine();
}
writer.close();
}
public static void read() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("..//xm//src//system//account.txt"));
String s1;
while ((s1 = reader.readLine()) != null) {
if (!s1.equals("\r\n")) {
String[] b = s1.split(" ");
ye.hm.put(b[0], b[1]);
}
}
reader.close();
}
}
代码链接
码云:
https://gitee.com/zhaohaiyangzxc/git-xm.git
就这些了,下面的讲解和注意事项等等我有时间再写。
请多关注、点赞、收藏!!