补充之前的java课程设计,不仅仅只有方法,更加人性化的在控制台给予提示输入,加入了键盘输入的代码
下面是源码,很长的
前两个类仅仅是判断是多了equals()方法,不知道为什么,加入了键盘输入方法就必须这么判断,
可能以前不规范吧
package qfy;
class myclass implements Comparable<myclass>{
String classnum;
String classname;
String classyuan;
String classtest;
String classtype;
String type;
public myclass(String num,String name,String yuan,String test){
classnum=num;
classname=name;
classtest=test;
classyuan=yuan;
}
//获得到所有的属性
public String getMyclass(){
return classnum+" "+classname+" "+classyuan+" "+classtest;
}
//通过判断传来的字符串,来选择性修改某个属性,老方法
public String changeMyclass(String classtype,String now){
if(classtype.equals("成绩")){
this.classtest=now;
return this.classtype;
}
if(classtype.equals("学号")){
return this.classnum=now;
}
if(classtype.equals("姓名")){
return this.classname=now;
}
if(classtype.equals("学院")){
return this.classyuan=now;
}
return "ok";
}
public String getClassname(){
return classname;
}
public int compareTo(myclass o) {
// TODO Auto-generated method stub
return this.classtest.compareTo(o.classtest);
}
}
package qfy;
class teacher implements Comparable<teacher> {
String teachernum;
String teachername;
String teachertel;
public teacher(String num,String name,String tel){
teachertel=tel;
teachername=name;
teachernum=num;
}
//获得到所有的属性
public String getTeacher(){
return teachernum+" "+teachername+" "+teachertel;
}
//通过判断传来的字符串,来选择性修改某个属性
public String changeTeacher(String classtype,String now){
if(classtype=="teachernum"){
return this.teachernum=now;
}
if(classtype=="teachername"){
return this.teachername=now;
}
if(classtype=="teachertel"){
return this.teachertel=now;
}
return "ok";
}
public String getTeachername(){
return teachername;
}
public int compareTo(teacher o) {
// TODO Auto-generated method stub
return this.teachernum.compareTo(o.teachernum);
}
}
下面的两个改变的比较大,主要是加入了键盘输入
package qfy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class test {
public static void main(String[] args) throws IOException {
System.out.println("欢迎进入学生系统");
List<myclass> list = new ArrayList<myclass>();
// 添加OK
// 查找OK
//
// 改变OK
//
// 删除调用OK
//
// System.out.println(list.size());
// 排序调用
//
while (true) {
System.out.println("请输入想要进行的操作");
Scanner in = new Scanner(System.in);
System.out.println("添加"+" "+"查找"+" "+"修改"+" "+"删除"+" "+"排序");
String string = in.next();
if(string.equals("添加")){
addclass(list);
}
if(string.equals("查找")){
getMyClass(list);
}
if(string.equals("修改")){
changetype(list);
}
if(string.equals("删除")){
remove(list);
}
if(string.equals("排序")){
sort(list);
}
File file = new File("myclass.db");
if (!file.exists()) {
file.createNewFile();
}
// 写
FileWriter fw = new FileWriter(file);
for (int i = 0; i < list.size(); i++) {
String result = myclassToString(list.get(i));
fw.write(result + "\n");
}
fw.close();
System.out.println("========================================");
// 读
BufferedReader br = new BufferedReader(new FileReader(file));
String temp;
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
br.close();
}
}
// 删除的方法
public static void remove(List<myclass> list) {
int i = 0;
Scanner in = new Scanner(System.in);
System.out.println("请输入删除对象的名字");
String name = in.next();
int j=0;
for (i = 0; i < list.size(); i++) {
if (list.get(i).getClassname().equals(name)) {
list.remove(i);
System.out.println("删除成功");
j=1;
break;
}
}
if (j==0) {
System.out.println("查无此人,请输入正确人名");
}
}
// 排序方法
public static void sort(List<myclass> list) {
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getMyclass());
}
}
// 变成字符串
public static String myclassToString(myclass my) {
String result = null;
StringBuffer mystr = new StringBuffer();
mystr.append(my.classnum + " ");
mystr.append(my.classname + " ");
mystr.append(my.classyuan + " ");
mystr.append(my.classtest + " ");
result = mystr.toString();
return result;
}
// 查找
public static void getMyClass(List<myclass> list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入所知任意信息");
String string = in.next();
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getMyclass()).indexOf(string) != -1) {
System.out.println("资料如下");
System.out.println(list.get(i).getMyclass());
break;
} else {
System.out.println("查无此人");
break;
}
}
}
// 添加方法
public static void addclass(List<myclass> list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入学号");
String num = in.next();
System.out.println("请输入姓名");
String name = in.next();
System.out.println("请输入学院");
String yuan = in.next();
System.out.println("请输入成绩");
String test = in.next();
list.add(new myclass(num, name, yuan, test));
}
// 改变方法 changetype(list,"classname","ly", "qqq");
public static void changetype(List<myclass> list) {
// char saveoldtype=oldtype;
Scanner in = new Scanner(System.in);
System.out.println("请输入该人任意信息");
String classtype = in.nextLine();
System.out.println("请输入原来信息");
String oldtype = in.nextLine();
System.out.println("请输入修改信息");
String now = in.nextLine();
for (int i = 0; i < list.size(); i++) {
if ((list.get(i).getMyclass()).indexOf(oldtype) != -1) {
System.out.println("修改完成,结果如下");
list.get(i).changeMyclass(classtype, now);
System.out.println(list.get(0).getMyclass());
break;
} else {
System.out.println("没有资料,无法修改");
break;
}
}
}
}
package qfy;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class test_teacher {
public static void main(String[] args) throws IOException {
List<teacher> list = new ArrayList<teacher>();
System.out.println("欢迎进入老师系统");
while (true) {
System.out.println("请输入想要进行的操作");
Scanner in = new Scanner(System.in);
System.out.println("添加"+" "+"查找"+" "+"修改"+" "+"删除"+" "+"排序");
String string = in.next();
if(string.equals("添加")){
addclass(list);
}
if(string.equals("查找")){
getTeaCher(list);
}
if(string.equals("修改")){
changetype(list);
}
if(string.equals("删除")){
remove(list);
}
if(string.equals("排序")){
sort(list);
}
File file = new File("myclass.db");
if (!file.exists()) {
file.createNewFile();
}
// 写
FileWriter fw = new FileWriter(file);
for (int i = 0; i < list.size(); i++) {
String result = teacherToString(list.get(i));
fw.write(result + "\n");
}
fw.close();
System.out.println("========================================");
// 读
BufferedReader br = new BufferedReader(new FileReader(file));
String temp;
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
br.close();
}
}
// 删除的方法
public static void remove(List<teacher> list) {
int i = 0;
Scanner in = new Scanner(System.in);
System.out.println("请输入删除对象的名字");
String name = in.next();
int j=0;
for (i = 0; i < list.size(); i++) {
if (list.get(i).getTeachername().equals(name)) {
list.remove(i);
System.out.println("删除成功");
j=1;
break;
}
}
if (j==0) {
System.out.println("查无此人,请输入正确人名");
}
}
// 排序方法
public static void sort(List<teacher> list) {
Collections.sort(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getTeacher());
}
}
// 变成字符串
public static String teacherToString( teacher teacher) {
String result = null;
StringBuffer mystr = new StringBuffer();
mystr.append(teacher.teachernum + " ");
mystr.append(teacher.teachername + " ");
mystr.append(teacher.teachertel+" ");
result = mystr.toString();
return result;
}
// 查找
public static void getTeaCher(List<teacher> list) {
Scanner in = new Scanner(System.in);
System.out.println("请输入所知任意信息");
String string = in.next();
for (int i = 0; i < list.size(); i++) {
if((list.get(i).getTeacher()).indexOf(string)!=-1){
System.out.println("资料如下");
System.out.println(list.get(i).getTeacher());
break;
}else{
System.out.println("查无此人");
break;
}
}
}
//添加
public static void addclass(List<teacher> list){
Scanner in = new Scanner(System.in);
System.out.println("请输入学号");
String num = in.next();
System.out.println("请输入姓名");
String name = in.next();
System.out.println("请输入电话");
String tel = in.next();
list.add(new teacher(num, name, tel));
}
//改变方法 changetype(list,"teachername","qfy", "qqq");
public static void changetype(List<teacher> list){
//char saveoldtype=oldtype;
Scanner in = new Scanner(System.in);
System.out.println("请输入该人任意信息");
String classtype = in.nextLine();
System.out.println("请输入原来信息");
String oldtype = in.nextLine();
System.out.println("请输入修改信息");
String now = in.nextLine();
for (int i = 0; i < list.size(); i++) {
if((list.get(i).getTeacher()).indexOf(oldtype)!=-1){
System.out.println("修改完成,结果如下");
list.get(i).changeTeacher(classtype,now);
System.out.println(list.get(0).getTeacher());
break;
}else{
System.out.println("没有资料,无法修改");
break;
}
}
}
}