[关闭]
@Senl 2017-04-05T17:08:06.000000Z 字数 6922 阅读 1045

4.1-4.4学习进度

技术进度


Java

TopView 项目试开发

用于登陆的类

创建人物的类

4.1下午回家,然后4.2上午去看病,所以耽搁了一点时间,所以两天的工作合在一起发


  1. // 面向用户的类,用于登陆用户
  2. import java.io.Serializable;
  3. public class Password implements Serializable {
  4. private String name="";
  5. private String password="";
  6. public void setName(String name) {
  7. this.name = name;
  8. }
  9. public String getName() {
  10. return name;
  11. }
  12. public String getPassword() {
  13. return password;
  14. }
  15. public void setPassword(String password) {
  16. this.password = password;
  17. }
  18. public void show(){
  19. System.out.println("name:"+name+"\n"+"password:"+password);
  20. }
  21. }

  1. // 登陆时switch确定操作类型
  2. import java.util.Scanner;
  3. public class Select {
  4. String fileName = "F:/Uesr_Name.txt";
  5. public void select() {
  6. Scanner int_sc = new Scanner(System.in);
  7. int select = int_sc.nextInt();
  8. Window wid = new Window();
  9. SignIn signIn = new SignIn();
  10. SignInCheck signIn_ck = new SignInCheck();
  11. while (select != 0) {
  12. switch( select ) {
  13. case 1 : if(signIn != null){
  14. signIn.signIn();
  15. }
  16. select = 2;
  17. /*case 2: Open op = new Open();
  18. Scanner str_sc1 = new Scanner(System.in);
  19. String name_check = str_sc1.nextLine();
  20. op.open(name_check);
  21. break;
  22. */
  23. case 2: if(signIn_ck != null){
  24. signIn_ck.signInCheck();
  25. }
  26. default: break;
  27. }
  28. }
  29. int_sc.close();
  30. }
  31. }

  1. //登陆时候,文字界面
  2. public class Window {
  3. public void print() {
  4. System.out.println("*********************************");
  5. System.out.println("*\t 输入一个整数:\t\t*");
  6. System.out.println("*\t 1. 创建新用户 "+"\t*");
  7. System.out.println("*\t 2. 登陆 \t\t*");
  8. System.out.println("*\t 0. 退出系统 \t\t*");
  9. System.out.println("*********************************");
  10. }
  11. }

  1. //创建用户
  2. import java.util.Scanner;
  3. public class SignIn {
  4. String fileName = "F:/Uesr_Name.txt";
  5. public void signIn() {
  6. System.out.println("请输入你的用户名:"+"\n");
  7. Scanner str_sc = new Scanner(System.in);
  8. Password pw = new Password();
  9. String name = str_sc.nextLine();
  10. while(name.equals("")){
  11. System.out.println("输入的用户名不能为空,请重新输入");
  12. name =str_sc.nextLine();
  13. }
  14. pw.setName(name);
  15. Drive.Write(fileName, name+"\r\n");
  16. System.out.println("请输入你的密码:"+"\n");
  17. String password =str_sc.nextLine();
  18. while(password.equals("")){
  19. System.out.println("输入的密码不能为空,请重新输入");
  20. password =str_sc.nextLine();
  21. }
  22. pw.setPassword(password);
  23. Drive.Write(fileName, password+"\r\n");
  24. Saved sav = new Saved();
  25. sav.name = pw.getName();
  26. sav.save(pw);
  27. System.out.println("保存成功,接下来请登陆");
  28. }
  29. }

  1. // 用序列化保存用户于本地
  2. import java.io.FileOutputStream;
  3. import java.io.ObjectOutputStream;
  4. public class Saved {
  5. String name="";
  6. public void save(Object obj) {
  7. try{ //序列化
  8. System.out.println("The uesr name is "+name);
  9. FileOutputStream fs = new FileOutputStream(name +".ser");
  10. ObjectOutputStream os = new ObjectOutputStream(fs);
  11. os.writeObject(obj);
  12. os.close();
  13. }catch(Exception ex){
  14. ex.printStackTrace();
  15. }
  16. }
  17. }

  1. // 用户登陆
  2. import java.util.Scanner;
  3. public class SignInCheck {
  4. public void signInCheck() {
  5. Confirm cf = new Confirm();
  6. Scanner ck_str = new Scanner(System.in);
  7. System.out.println("请输入您的用户名和密码");
  8. String name_ck = ck_str.nextLine();
  9. String pw_ck = ck_str.nextLine();
  10. cf.confirm(name_ck ,pw_ck);
  11. }
  12. }

  1. // 解序列化,验证用户
  2. import java.io.FileInputStream;
  3. import java.io.ObjectInputStream;
  4. import java.util.Scanner;
  5. public class Confirm {
  6. public void confirm(String name , String password) {
  7. try { //解序列化
  8. ObjectInputStream is = new
  9. ObjectInputStream(new FileInputStream(name+".ser"));
  10. //转换对象类型
  11. Password pw = (Password) is.readObject();
  12. is.close();
  13. String pw_ck = pw.getPassword();
  14. Boolean isTrue = true;
  15. Scanner sc = new Scanner(System.in);
  16. while(isTrue){
  17. if(pw_ck.equals(password)) {
  18. System.out.println("登陆成功");
  19. isTrue = false;
  20. }
  21. else{
  22. System.out.println("登陆失败,请重新输入");
  23. password = sc.nextLine();
  24. }
  25. }
  26. sc.close();
  27. } catch(Exception ex) {
  28. ex.printStackTrace();
  29. }
  30. }
  31. }

  1. // 启动器,有一个将用户以及密码写入本地.txt的方法
  2. import java.io.*;
  3. import java.util.*;
  4. public class Drive {
  5. public static void main(String[] args) {
  6. Window wid = new Window();
  7. Select slc = new Select();
  8. wid.print();
  9. slc.select();
  10. }
  11. public static void Write(String fileName, String context ){
  12. try {
  13. // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  14. FileWriter writer = new FileWriter(fileName, true); writer.write(context);
  15. writer.close();
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }

人物资料类


  1. import java.io.Serializable;
  2. // Person类,设定属性以及设定属性的方法
  3. //设置一个将对象实例所有的属性集合成一个字符串的方法
  4. public class Person implements Serializable {
  5. private String name;
  6. private int height;
  7. private int money;
  8. private int id=100;
  9. public String getName() {
  10. return name;
  11. }
  12. public void setName(String name) {
  13. this.name = name;
  14. }
  15. public int getHeight() {
  16. return height;
  17. }
  18. public void setHeight(int height) {
  19. this.height = height;
  20. }
  21. public int getMoney() {
  22. return money;
  23. }
  24. public void setMoney(int money) {
  25. this.money = money;
  26. }
  27. public String getId() {
  28. return id+"";
  29. }
  30. public void setId(int id) {
  31. this.id = id;
  32. }
  33. public String all(){
  34. String height0 = " 身高:"+height ;
  35. String money0 = "金钱:"+money ;
  36. //String id0 = id + "";
  37. String all = "姓名:"+name +" "+ height0 +" "+ money0;
  38. id++;
  39. return all;
  40. }
  41. }

  1. import java.util.Scanner;
  2. // Person集合操作类
  3. public class Unit {
  4. int select = 0;
  5. public void select() {
  6. String filename = "F:/person.txt";
  7. Scanner sc = new Scanner(System.in);
  8. Person ps = new Person();
  9. Window wid = new Window();
  10. wid.pPrint();
  11. String empty = sc.nextLine();
  12. select = sc.nextInt();
  13. while(select != 0){
  14. if(select == 1) {
  15. Create cr = new Create();
  16. cr.create();
  17. wid.pPrint();
  18. select = sc.nextInt();
  19. }
  20. if(select == 2) {
  21. System.out.println("以下是已有的人员资料");
  22. Read.readFileByLines(filename);
  23. wid.pPrint();
  24. select = sc.nextInt();
  25. }
  26. if(select == 3) {
  27. }
  28. if(select ==4 ) {
  29. }
  30. }
  31. }
  32. }

  1. \\ 输入人员资料类
  2. import java.io.*;
  3. import java.util.*;
  4. public class Create {
  5. public void create() {
  6. Person ps = new Person();
  7. Scanner sc = new Scanner(System.in);
  8. try { //解序列化
  9. ObjectInputStream is = new
  10. ObjectInputStream(new FileInputStream("Person.ser"));
  11. //转换对象类型
  12. ps = (Person) is.readObject();
  13. is.close();
  14. } catch(Exception ex) {
  15. ex.printStackTrace();
  16. }
  17. System.out.println("id:"+ps.getId());
  18. int id = Integer.parseInt(ps.getId());
  19. ps.setId(id++);
  20. //输入此人的名字
  21. System.out.println("请输入此人的名字");
  22. String name = sc.nextLine();
  23. while( name.equals("") ){
  24. System.out.println("输入的名字不能为空,请重新输入");
  25. name = sc.nextLine();
  26. }
  27. ps.setName(name);
  28. //输入此人的身高
  29. System.out.println("请输入此人的身高(cm)");
  30. int height = sc.nextInt();
  31. while( height < 0 ||height > 233) {
  32. System.out.println("输入的身高越界,请重新输入");
  33. height = sc.nextInt();
  34. }
  35. ps.setHeight(height);
  36. //输入此人的现金
  37. System.out.println("请输入此人的现金(¥)");
  38. int money = sc.nextInt();
  39. while( money <0 ){
  40. System.out.println("本人的现金不能为负数,请重新输入");
  41. money = sc.nextInt();
  42. }
  43. ps.setMoney(money);
  44. Save save = new Save();
  45. Save.write(ps.getId()+"\r\n");
  46. Save.write(ps.all()+"\r\n");
  47. ps.setId(id++);
  48. try{ //序列化
  49. FileOutputStream fs = new FileOutputStream("Person.ser");
  50. ObjectOutputStream os = new ObjectOutputStream(fs);
  51. os.writeObject(ps);
  52. os.close();
  53. }catch(Exception ex){
  54. ex.printStackTrace();
  55. }
  56. }
  57. }

  1. //将人物资料存储到文件
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. public class Save {
  5. //String filename = "F:/person.txt";
  6. public static void write(String context ){
  7. try {
  8. String filename = "F:/person.txt";
  9. // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
  10. FileWriter writer = new FileWriter(filename, true);
  11. writer.write(context);
  12. writer.close();
  13. } catch (IOException e) {
  14. e.printStackTrace();
  15. }
  16. }
  17. }

  1. //从文件中读取人员资料类
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. public class Read {
  7. public static void readFileByLines(String fileName) {
  8. File file = new File(fileName);
  9. BufferedReader reader = null;
  10. try {
  11. reader = new BufferedReader(new FileReader(file));
  12. String tempId = null;
  13. String tempString = null;
  14. while ((tempId = reader.readLine()) != null
  15. && (tempString = reader.readLine()) != null) {
  16. //读取文件并输出
  17. System.out.print( "id:"+tempId +" \n" );
  18. System.out.println( tempString + "\n" );
  19. }
  20. reader.close();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. } finally {
  24. if (reader != null) {
  25. try {
  26. reader.close();
  27. } catch (IOException e1) {
  28. }
  29. }
  30. }
  31. }
  32. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注