扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
寝室类如下:
创新互联建站坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的鄢陵网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
import java.util.List;
import java.util.Objects;
public class Bedroom {
private String place;
private String manager;
private Integer bedCount;
private Integer tableCount;
private ListPerson persons;
private char sexFlag;//男:1 女:2
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getManager() {
return manager;
}
public void setManager(String manager) {
this.manager = manager;
}
public Integer getBedCount() {
return bedCount;
}
public void setBedCount(Integer bedCount) {
this.bedCount = bedCount;
}
public Integer getTableCount() {
return tableCount;
}
public void setTableCount(Integer tableCount) {
this.tableCount = tableCount;
}
public ListPerson getPersons() {
return persons;
}
public void setPersons(ListPerson persons) {
this.persons = persons;
}
public char getSexFlag() {
return sexFlag;
}
public void setSexFlag(char sexFlag) {
this.sexFlag = sexFlag;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bedroom bedroom = (Bedroom) o;
return sexFlag == bedroom.sexFlag
Objects.equals(place, bedroom.place)
Objects.equals(manager, bedroom.manager)
Objects.equals(bedCount, bedroom.bedCount)
Objects.equals(tableCount, bedroom.tableCount)
Objects.equals(persons, bedroom.persons);
}
@Override
public int hashCode() {
return Objects.hash(place, manager, bedCount, tableCount, persons, sexFlag);
}
@Override
public String toString() {
return "Bedroom{" +
"place='" + place + '\'' +
", manager='" + manager + '\'' +
", bedCount=" + bedCount +
", tableCount=" + tableCount +
", persons=" + persons +
", sexFlag=" + sexFlag +
'}';
}
}
寝室中住宿的人员类如下:
import java.util.Objects;
public class Person {
private String sno;
private String name;
private String subject;
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return Objects.equals(sno, person.sno)
Objects.equals(name, person.name)
Objects.equals(subject, person.subject);
}
@Override
public int hashCode() {
return Objects.hash(sno, name, subject);
}
@Override
public String toString() {
return "Person{" +
"sno='" + sno + '\'' +
", name='" + name + '\'' +
", subject='" + subject + '\'' +
'}';
}
}
锁加单例:
public class Regist{ private static Regist rgst=new Regist(); private Regist(){} private int cnt=0; private int max=100; public static getRegist(){return rgst;} ///设置最大上线人数 public void setMax(int max){this.max=max;} ///判断新用户能否登入 public synchronized boolean login(){ if(cnt=max)return false; ++cnt; return true; } ///用户退出时调用 public synchronized void logout(){ --cnt; }};
有新用户要登入时,先Regist::getRegist().login()判断能否登入,返回true则表示没到用户上限,可以登入,否则表示到上限了,不能登入,用户退出时需要调用Regist::getRegist().logout()
synchronized不清楚可以百度,是种类似锁的东西。作用于方法上,相当于调用方法前先lock,调用之后unlock,其锁的对象是该方法的类对象。
线程不是太多可以用这个方法,线程太多用这个可能慢了些,好像加锁比较耗时间,如果同一个时间太多人进行登入操作,都得在那等着,一个一个进行,反应慢
全部代码已发送你的邮箱。
以下是部分代码。
package zhidao;
import java.util.Scanner;
public class Main {
//从控制台读入信息
private Scanner scanner = new Scanner(System.in);
private StudentService ss = new StudentService();
public static void main(String[] args) {
Main main = new Main();
main.startService();
}
public void startService(){
System.out.println("欢迎使用宿舍管理系统");
//加载之前的舍友信息
ss.getFromFile();
while(true){
System.out.println("1,添加室友信息\t2,显示所有室友信息\t3,按姓名查找室友\t4,保存室友信息到文件\t5,调入舍友\t6,调离舍友\t7,退出");
System.out.print("请选择:");
int choose = Integer.parseInt(scanner.nextLine());
switch (choose) {
case 1:
this.addRoomMates();
break;
case 2:
ss.printRoomMates();
break;
case 3:
this.findRoomMatesByName();
break;
case 4:
ss.saveToFile();
break;
case 5:
this.addRoomMates();
break;
case 6:
this.removeRoomMatesBySno();
break;
case 7:
System.out.println("info -- 感谢使用,再见!");
System.exit(0);
break;
default:
System.out.println("info -- 无此项,请重新选择!");
break;
}
}
}
/**
* 调离舍友
*/
private void removeRoomMatesBySno() {
while(true){
System.out.println("1,调离舍友\t2,退到上一层");
System.out.print("请选择:");
int choose = Integer.parseInt(scanner.nextLine());
switch (choose) {
case 1:
remove();
break;
case 2:
return;
default:
System.out.println("info -- 无此项,请重新选择!");
}
}
}
/**
* 调离
*/
private void remove() {
System.out.print("请输入学号:");
String sno = scanner.nextLine();
ss.removeRoomMatesBySno(sno);
}
/**
* 按姓名查找
*/
private void findRoomMatesByName() {
while(true){
System.out.println("1,查找\t2,退到上一层");
System.out.print("请选择:");
int choose = Integer.parseInt(scanner.nextLine());
switch (choose) {
case 1:
check();
break;
case 2:
return;
default:
System.out.println("info -- 无此项,请重新选择!");
}
}
}
/**
* 查找
*/
private void check() {
System.out.print("请输入姓名:");
String name = scanner.nextLine();
ss.findRoomMateByName(name);
}
/**
* 添加室友
*/
private void addRoomMates() {
while(true){
System.out.println("1,添加\t2,退到上一层");
System.out.print("请选择:");
int choose = Integer.parseInt(scanner.nextLine());
switch (choose) {
case 1:
add();
break;
case 2:
return;
default:
System.out.println("info -- 无此项,请重新选择!");
}
}
}
/**
* 添加
*/
private void add() {
System.out.print("请输入学号:");
String sno = scanner.nextLine();
System.out.print("请输入姓名:");
String name = scanner.nextLine();
System.out.print("请输入性别:");
String sex = scanner.nextLine();
System.out.print("请输入年龄:");
int age = Integer.parseInt(scanner.nextLine());
ss.addRoomMates(new Student(sno,name,sex,age));
}
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流