多用户(最多4个)
文件crud
分为当前用户下文件、已打开文件
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static User[] userList = new User[4];
public static LinkedList<UserFile> openFileList = new LinkedList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean scanflag = true;
while (scanflag) {
System.out.println("请输入你要创建的用户数(最多4个):");
int usernum = scanner.nextInt();
if (usernum <= 4 && usernum > 0) {
adduser(usernum);
scanflag = false;
} else System.out.println("输入错误,请重新输入!");
}
//登录
boolean logflag = true;
while (logflag) {
System.out.println("请输入你想要登录的用户名:(exit退出,show展示全部用户信息)");
String logname = scanner.next();
if (userList[0]!=null && logname.equals(userList[0].getName())) operation(userList[0]);
else if (userList[1]!=null && logname.equals(userList[1].getName())) operation(userList[1]);
else if (userList[2]!=null && logname.equals(userList[2].getName())) operation(userList[2]);
else if (userList[3]!=null && logname.equals(userList[3].getName())) operation(userList[3]);
else if (logname.equals("exit")) logflag = false;
else if (logname.equals("show")) showAll();
else System.out.println("没这个用户!");
}
scanner.close();
}
//添加用户
public static void adduser(int usernum){
Scanner scanner = new Scanner(System.in);
for(int i=0;i<usernum;i++){
boolean flag = true;
l:while (flag) {
flag = false;
System.out.println("请输入第" + (i + 1) + "个用户的姓名:");
String name = scanner.next();
if (i != 0) {
for (int j = 0; j < i; j++) {
if (userList[j].getName().equals(name)) {
System.out.println("重名了,请重新输入!");
flag = true;
continue l;
}
}
}
userList[i] = new User(name);
}
}
//scanner.close();
}
//用户操作
public static void operation(User user){
System.out.println("当前在线:"+user.getName());
Scanner scanner = new Scanner(System.in);
boolean choose = true;
while(choose) {
System.out.println("请选择你要进行的操作:1.建立新文件 2.打开文件 3.删除文件 4.显示打开的文件列表 5.退出");
String chs = scanner.next();
int ch = new Integer(chs);
switch (ch) {
case 1:
create(user);
break;
case 2:
open(user);
break;
case 3:
delete(user);
break;
case 4:
showList();
break;
case 5:
choose = false;
break;
default:
System.out.println("你的选择有误,请重新选择!");
}
}
//scanner.close();
}
//建立新文件
public static void create(User user){
System.out.println("请输入想要创建的文件名:");
Scanner scanner = new Scanner(System.in);
String fileName = scanner.next();
UserFile userFile = new UserFile(fileName);
UserFile lastFile = null;
if(user.getUserFile()!=null){
lastFile = user.getUserFile();
while (lastFile.getNextFile()!=null){
lastFile = lastFile.getNextFile();
}
}
if (lastFile==null) user.setUserFile(userFile);
else lastFile.setNextFile(userFile);
//scanner.close();
}
//打开文件
public static void open(User user){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你要打开的文件名:");
String fileName = scanner.next();
UserFile file = null;
boolean flag = true;
if(user.getUserFile()!=null) {//用户是有文件的
file = user.getUserFile();
while(!file.getFilename().equals(fileName)){//文件名不对
if(file.getNextFile()==null){
System.out.println("找不到这个文件!");
flag = false;
break;
}
file = file.getNextFile();
}
while(flag) {
addOpen(file);//加入已打开文件链表
System.out.println("请输入你想要进行的操作:1.读文件 2.写文件 3.关闭文件");
String chs = scanner.next();
int ch = new Integer(chs);//转数字,避免类型有异常
if (ch == 1) {
flag = false;
read(file);
} else if (ch == 2){
flag = false;
write(file);
} else if (ch == 3){
flag = false;
openFileList.remove(file);
} else System.out.println("选择有误,请重新选择!");
}
}else if(isOpen(fileName)){//已经打开了
for(UserFile tempFile:openFileList){//找出来那个文件
if (tempFile.getFilename().equals(fileName)) file = tempFile;
}
while(flag) {
System.out.println("请输入你想要进行的操作:1.读文件 2.写文件 3.关闭文件");
String chs = scanner.next();
int ch = new Integer(chs);//转数字,避免类型有异常
if (ch == 1) {
flag = false;
read(file);
} else if (ch == 2){
flag = false;
write(file);
} else if (ch == 3){
flag = false;
openFileList.remove(file);
} else System.out.println("选择有误,请重新选择!");
}
}else {
System.out.println("找不到这个文件!");
}
//scanner.close();
}
//读文件
public static void read(UserFile file){
System.out.println("文件名:"+file.getFilename());
System.out.println("文件长度:"+file.getLen());
System.out.println("文件记录:");
Record record = file.getRecord();
if (record!=null) {
while (record.getNextRecord() != null) {
System.out.println(record.getName() + "\t" + record.getSex());
record = record.getNextRecord();
}
System.out.println(record.getName() + "\t" + record.getSex());
}else {
System.out.println("无记录!");
}
}
//写文件
public static void write(UserFile file){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入姓名:");
String name = scanner.next();
System.out.println("请输入性别:");
String sex = scanner.next();
Record record = new Record(name,sex);
//找到最后一条记录插入记录
Record lastRecord = null;
if(file.getRecord()==null){
file.setRecord(record);
}else{
lastRecord = file.getRecord();
while (lastRecord.getNextRecord()!=null){
lastRecord = lastRecord.getNextRecord();
}
lastRecord.setNextRecord(record);
}
//增加一条记录
file.setLen(file.getLen()+1);
}
//删除文件
public static void delete(User user){
Scanner scanner = new Scanner(System.in);
System.out.println("请输入想要删除的文件名:");
String fileName = scanner.next();
if (isOpen(fileName)){
UserFile thisFile = null;
for (UserFile tempFile:openFileList){
if (tempFile.getFilename().equals(fileName)) thisFile = tempFile;
}
openFileList.remove(thisFile);
for (int i=0;i<4;i++){
if (userList[i]!=null){
UserFile tempFile = userList[i].getUserFile();
if (tempFile==null) break;//用户一个文件都没有
else {
if (tempFile.getFilename().equals(fileName)) userList[i].setUserFile(tempFile.getNextFile());
else {
if(tempFile.getNextFile()!=null){
if (tempFile.getNextFile().getFilename().equals(fileName)){//临时文件头下一个要删
if (tempFile.getNextFile().getNextFile()==null) tempFile.setNextFile(null);//没有第三个,不用链过去
else tempFile.setNextFile(tempFile.getNextFile().getNextFile());
}else{//不删
if (tempFile.getNextFile().getNextFile()==null) break;//而且下下个没有,也就是到头了,就结束
else {//有下下个
while(tempFile.getNextFile().getNextFile()!=null && !tempFile.getNextFile().getNextFile().getFilename().equals(fileName)){//有下下个且不删
tempFile = tempFile.getNextFile();
}
if(tempFile.getNextFile().getNextFile()==null) System.out.println("找不到这个文件!");//终止是因为下下个没了
if(tempFile.getNextFile().getNextFile().getFilename().equals(fileName)){//终止是因为下下个要删了
tempFile = tempFile.getNextFile();//现在是因为下个要删了
if(tempFile.getNextFile().getNextFile()==null) tempFile.setNextFile(null);
else tempFile.setNextFile(tempFile.getNextFile().getNextFile());
}
}
}
}else {
continue;
}
}
}
}
}
}else {
if(user.getUserFile()==null) System.out.println("找不到这个文件!");
else if(user.getUserFile().getFilename().equals(fileName)) {
openFileList.remove(user.getUserFile());
user.setUserFile(user.getUserFile().getNextFile());
} else{
UserFile lastFile = user.getUserFile();
if(lastFile.getNextFile()!=null){
while (lastFile.getNextFile()!=null){//下个文件有
if(!lastFile.getNextFile().getFilename().equals(fileName)){//且不是要删的
lastFile = lastFile.getNextFile();
}else {//要删
if (lastFile.getNextFile().getNextFile()==null) {
openFileList.remove(lastFile.getNextFile());
lastFile.setNextFile(null);
break;
} else {
openFileList.remove(lastFile.getNextFile());
lastFile.setNextFile(lastFile.getNextFile().getNextFile());
break;
}
}
}
}
}
}
}
//展示已打开文件的列表
public static void showList(){
System.out.println("已打开的文件如下:");
if (openFileList.size()==0) System.out.println("无打开文件!");
for (UserFile tempFile:openFileList){
System.out.println("\t\t文件名:"+tempFile.getFilename()+"\t文件大小:"+tempFile.getLen());
}
}
//展示全部信息
public static void showAll(){
for(int i=0;i<4;i++){
if (userList[i]!=null){//有这个用户
System.out.println("用户名:"+userList[i].getName());
System.out.println("\t文件列表:");
UserFile tempFile = userList[i].getUserFile();
while (tempFile!=null){
System.out.println("\t\t文件名称:"+tempFile.getFilename()+"\t文件大小:"+tempFile.getLen());
System.out.println("\t\t记录列表:");
Record tempRecord = tempFile.getRecord();
while (tempRecord!=null){
System.out.println("\t\t\t姓名:"+tempRecord.getName()+"\t性别:"+tempRecord.getSex());
tempRecord = tempRecord.getNextRecord();
}
tempFile = tempFile.getNextFile();
}
}
}
}
//添加到打开文件列表(去重)
public static void addOpen(UserFile file){
boolean isHave = false;
for (UserFile tempFile:openFileList){
if(tempFile.equals(file)) isHave = true;
}
if (!isHave) openFileList.add(file);
}
//是否已打开
public static boolean isOpen(String fileName){
for (UserFile tempFile:openFileList){
if(tempFile.getFilename().equals(fileName)) return true;
}
return false;
}
}
class User{
private String name;
private UserFile userFile;
public User() {}
public User(String name) {this.name = name;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public UserFile getUserFile() {return userFile;}
public void setUserFile(UserFile userFile) {this.userFile = userFile;}
}
class UserFile{
private String filename;
private int len;
private Record record;
private UserFile nextFile;
public UserFile(String filename) {this.filename = filename;}
public String getFilename() {return filename;}
public void setFilename(String filename) {this.filename = filename;}
public int getLen() {return len;}
public void setLen(int len) {this.len = len;}
public Record getRecord() {return record;}
public void setRecord(Record record) {this.record = record;}
public UserFile getNextFile() {return nextFile;}
public void setNextFile(UserFile nextFile) {this.nextFile = nextFile;}
}
class Record{
private String name;
private String sex;
private Record nextRecord;
public Record() {}
public Record(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public String getSex() {return sex;}
public void setSex(String sex) {this.sex = sex;}
public Record getNextRecord() {return nextRecord;}
public void setNextRecord(Record nextRecord) {this.nextRecord = nextRecord;}
}