@Senl
2017-04-05T17:08:06.000000Z
字数 6922
阅读 1045
技术进度
4.1下午回家,然后4.2上午去看病,所以耽搁了一点时间,所以两天的工作合在一起发
// 面向用户的类,用于登陆用户import java.io.Serializable;public class Password implements Serializable {private String name="";private String password="";public void setName(String name) {this.name = name;}public String getName() {return name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public void show(){System.out.println("name:"+name+"\n"+"password:"+password);}}
// 登陆时switch确定操作类型import java.util.Scanner;public class Select {String fileName = "F:/Uesr_Name.txt";public void select() {Scanner int_sc = new Scanner(System.in);int select = int_sc.nextInt();Window wid = new Window();SignIn signIn = new SignIn();SignInCheck signIn_ck = new SignInCheck();while (select != 0) {switch( select ) {case 1 : if(signIn != null){signIn.signIn();}select = 2;/*case 2: Open op = new Open();Scanner str_sc1 = new Scanner(System.in);String name_check = str_sc1.nextLine();op.open(name_check);break;*/case 2: if(signIn_ck != null){signIn_ck.signInCheck();}default: break;}}int_sc.close();}}
//登陆时候,文字界面public class Window {public void print() {System.out.println("*********************************");System.out.println("*\t 输入一个整数:\t\t*");System.out.println("*\t 1. 创建新用户 "+"\t*");System.out.println("*\t 2. 登陆 \t\t*");System.out.println("*\t 0. 退出系统 \t\t*");System.out.println("*********************************");}}
//创建用户import java.util.Scanner;public class SignIn {String fileName = "F:/Uesr_Name.txt";public void signIn() {System.out.println("请输入你的用户名:"+"\n");Scanner str_sc = new Scanner(System.in);Password pw = new Password();String name = str_sc.nextLine();while(name.equals("")){System.out.println("输入的用户名不能为空,请重新输入");name =str_sc.nextLine();}pw.setName(name);Drive.Write(fileName, name+"\r\n");System.out.println("请输入你的密码:"+"\n");String password =str_sc.nextLine();while(password.equals("")){System.out.println("输入的密码不能为空,请重新输入");password =str_sc.nextLine();}pw.setPassword(password);Drive.Write(fileName, password+"\r\n");Saved sav = new Saved();sav.name = pw.getName();sav.save(pw);System.out.println("保存成功,接下来请登陆");}}
// 用序列化保存用户于本地import java.io.FileOutputStream;import java.io.ObjectOutputStream;public class Saved {String name="";public void save(Object obj) {try{ //序列化System.out.println("The uesr name is "+name);FileOutputStream fs = new FileOutputStream(name +".ser");ObjectOutputStream os = new ObjectOutputStream(fs);os.writeObject(obj);os.close();}catch(Exception ex){ex.printStackTrace();}}}
// 用户登陆import java.util.Scanner;public class SignInCheck {public void signInCheck() {Confirm cf = new Confirm();Scanner ck_str = new Scanner(System.in);System.out.println("请输入您的用户名和密码");String name_ck = ck_str.nextLine();String pw_ck = ck_str.nextLine();cf.confirm(name_ck ,pw_ck);}}
// 解序列化,验证用户import java.io.FileInputStream;import java.io.ObjectInputStream;import java.util.Scanner;public class Confirm {public void confirm(String name , String password) {try { //解序列化ObjectInputStream is = newObjectInputStream(new FileInputStream(name+".ser"));//转换对象类型Password pw = (Password) is.readObject();is.close();String pw_ck = pw.getPassword();Boolean isTrue = true;Scanner sc = new Scanner(System.in);while(isTrue){if(pw_ck.equals(password)) {System.out.println("登陆成功");isTrue = false;}else{System.out.println("登陆失败,请重新输入");password = sc.nextLine();}}sc.close();} catch(Exception ex) {ex.printStackTrace();}}}
// 启动器,有一个将用户以及密码写入本地.txt的方法import java.io.*;import java.util.*;public class Drive {public static void main(String[] args) {Window wid = new Window();Select slc = new Select();wid.print();slc.select();}public static void Write(String fileName, String context ){try {// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件FileWriter writer = new FileWriter(fileName, true); writer.write(context);writer.close();} catch (IOException e) {e.printStackTrace();}}}
import java.io.Serializable;// Person类,设定属性以及设定属性的方法//设置一个将对象实例所有的属性集合成一个字符串的方法public class Person implements Serializable {private String name;private int height;private int money;private int id=100;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getMoney() {return money;}public void setMoney(int money) {this.money = money;}public String getId() {return id+"";}public void setId(int id) {this.id = id;}public String all(){String height0 = " 身高:"+height ;String money0 = "金钱:"+money ;//String id0 = id + "";String all = "姓名:"+name +" "+ height0 +" "+ money0;id++;return all;}}
import java.util.Scanner;// Person集合操作类public class Unit {int select = 0;public void select() {String filename = "F:/person.txt";Scanner sc = new Scanner(System.in);Person ps = new Person();Window wid = new Window();wid.pPrint();String empty = sc.nextLine();select = sc.nextInt();while(select != 0){if(select == 1) {Create cr = new Create();cr.create();wid.pPrint();select = sc.nextInt();}if(select == 2) {System.out.println("以下是已有的人员资料");Read.readFileByLines(filename);wid.pPrint();select = sc.nextInt();}if(select == 3) {}if(select ==4 ) {}}}}
\\ 输入人员资料类import java.io.*;import java.util.*;public class Create {public void create() {Person ps = new Person();Scanner sc = new Scanner(System.in);try { //解序列化ObjectInputStream is = newObjectInputStream(new FileInputStream("Person.ser"));//转换对象类型ps = (Person) is.readObject();is.close();} catch(Exception ex) {ex.printStackTrace();}System.out.println("id:"+ps.getId());int id = Integer.parseInt(ps.getId());ps.setId(id++);//输入此人的名字System.out.println("请输入此人的名字");String name = sc.nextLine();while( name.equals("") ){System.out.println("输入的名字不能为空,请重新输入");name = sc.nextLine();}ps.setName(name);//输入此人的身高System.out.println("请输入此人的身高(cm)");int height = sc.nextInt();while( height < 0 ||height > 233) {System.out.println("输入的身高越界,请重新输入");height = sc.nextInt();}ps.setHeight(height);//输入此人的现金System.out.println("请输入此人的现金(¥)");int money = sc.nextInt();while( money <0 ){System.out.println("本人的现金不能为负数,请重新输入");money = sc.nextInt();}ps.setMoney(money);Save save = new Save();Save.write(ps.getId()+"\r\n");Save.write(ps.all()+"\r\n");ps.setId(id++);try{ //序列化FileOutputStream fs = new FileOutputStream("Person.ser");ObjectOutputStream os = new ObjectOutputStream(fs);os.writeObject(ps);os.close();}catch(Exception ex){ex.printStackTrace();}}}
//将人物资料存储到文件import java.io.FileWriter;import java.io.IOException;public class Save {//String filename = "F:/person.txt";public static void write(String context ){try {String filename = "F:/person.txt";// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件FileWriter writer = new FileWriter(filename, true);writer.write(context);writer.close();} catch (IOException e) {e.printStackTrace();}}}
//从文件中读取人员资料类import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;public class Read {public static void readFileByLines(String fileName) {File file = new File(fileName);BufferedReader reader = null;try {reader = new BufferedReader(new FileReader(file));String tempId = null;String tempString = null;while ((tempId = reader.readLine()) != null&& (tempString = reader.readLine()) != null) {//读取文件并输出System.out.print( "id:"+tempId +" \n" );System.out.println( tempString + "\n" );}reader.close();} catch (IOException e) {e.printStackTrace();} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}}}}