import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
public class Demo {
static Scanner sc = new Scanner(System.in);
static Map<String, String> map = new HashMap<String, String>();
static{
Properties p = new Properties();
try {
p.load(new BufferedReader(new FileReader("user.properties")));
String userName = p.getProperty("userName");
String password = p.getProperty("password");
String realName = p.getProperty("realName");
map.put("userName", userName);
map.put("password", password);
map.put("realName", realName);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void register() throws IOException{
System.out.println("请输入用户名");
String userName = sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
System.out.println("请输入真实姓名");
String realName = sc.nextLine();
BufferedWriter bw = new BufferedWriter(new FileWriter("user.txt"));
bw.write("userName="+userName);
bw.newLine();
bw.write("password="+password);
bw.newLine();
bw.write("realName="+realName);
bw.newLine();
bw.close();
System.out.println("注册成功");
}
public static void login() throws IOException{
System.out.println("请输入用户名");
String userName = sc.nextLine();
System.out.println("请输入密码");
String password = sc.nextLine();
if(userName.equals(map.get("userName"))&&password.equals(map.get("password"))){
System.out.println("欢迎"+map.get("realName")+"登陆");
}else{
System.out.println("用户名或密码有误");
}
}
public static void main(String[] args) {
try {
login();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class User {
private String userName;
private String password;
private String realName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public User(String userName, String password, String realName) {
super();
this.userName = userName;
this.password = password;
this.realName = realName;
}
public User() {
super();
}
}
homeWork
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.TreeSet;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TreeSet<Student> ts = new TreeSet<>();
System.out.println("请输入学生信息按照如下格式:1001#张三#20");
while(true){
String info = sc.nextLine();
if("exit".equals(info)){
break;
}
String[] str = info.split("#");
Student student = new Student(str[0], str[1], Integer.parseInt(str[2]));
ts.add(student);
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("Student.docx"));
bw.write("学号\t姓名\t年龄");
bw.newLine();
for(Student stu:ts){
bw.write(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge());
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("学生信息录入完毕");
}
}
public class Student implements Comparable<Student> {
private String id;
private String name;
private Integer age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Student(String id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student() {
super();
}
@Override
public int compareTo(Student s) {
int num = this.getAge()-s.getAge();
num = num==0?this.getId().compareTo(s.getId()):num;
num = num==0?this.getName().compareTo(s.getName()):num;
return num;
}
}