馬上要進行java基礎考試了,閑着寫一次博客,把這10周學的東西過一遍,可能沒過全,.....但是我覺得增刪改查是必須的,以前一直不會用ArrayList 自定義對象....... 案例如下:自己根據以前寫的作業想的哈!!!
1 學生信息管理系統 有姓名 性別 年齡 電話 其中 性別必須定義為布爾型 但是你輸入值的時候 要輸入男女 ,這就用到了 轉化
2 實現對學生管理的添加 修改 刪除(這個我沒寫,沒時間了,要打一把游戲)修改 寫到文件等操作;
3 自定義一異常類(算是畫蛇添足人家本來就有,自己非要定義一下, 回顧一下)
//主函數Tel.java
package 周四考試練習一波; import java.io.IOException; import java.util.Scanner; public class Tel { public static void main(String[] args) throws ExecptionDemo, IOException { // TODO 自動生成的方法存根 menu(); } public static void menu() throws ExecptionDemo, IOException//這是菜單函數遞歸調用就很舒服 {Scanner scanner =new Scanner(System.in); Method mm=new Method(); System.out.println("--------學生信息管理系統-------"); System.out.println("1 添加"); System.out.println("2 顯示"); System.out.println("3 查詢"); System.out.println("4 修改"); System.out.println("5 寫到文件"); System.out.println("6 讀入文件"); System.out.println("--------學生信息管理系統-------"); System.out.println("請輸入要選定的序號!"); int choose=scanner.nextInt(); switch (choose) { case 1: //添加 {mm.add(); menu();break;} case 2: //顯示 {mm.show(); menu();break;} case 3: //刪除 {mm.delete(); menu();break;} case 4: //修改 {mm.update(); menu();break;} case 5: //寫到文件 {mm.writeit(); menu();break;} case 6: //讀入文件 {mm.readit(); menu();break;} default: System.out.println("輸入無效即將推出");System.exit(0);break; } } }
user.java 函數
package 周四考試練習一波; public class User { public User(String name, int age, boolean sex, int phone) { super(); this.name = name; this.age = age; this.sex = sex; this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean getsex() { return sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isSex() { return sex; } public void setSex(boolean sex) { this.sex = sex; } public int getPhone() { return phone; } public void setPhone(int phone) { this.phone = phone; } private String name; private int age; private boolean sex; private int phone; }
method.java函數
package 周四考試練習一波; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Method { Scanner scanner=new Scanner(System.in); static ArrayList<User>arrayList=new ArrayList<User>();//這個必須定義為靜態的要不不能全局使用,剛開始寫錯了 public boolean converseto(String a)//這是由男轉化為boolean型的函數------我特么蛋疼寫完之后發現還不如直接重載 {if(a=="男") {return true; }else return false; } public String converseto(boolean a)//原諒我新手沒重載 {if(a==true) {return "男"; }else return "女"; } public void add()//添加函數一次添加一個 { System.out.println("請輸入 姓名"); String name=scanner.next(); System.out.println("請輸入 年齡"); int age=scanner.nextInt(); System.out.println("請輸入 性別"); String sex=scanner.next(); System.out.println("請輸入 手機號"); int phone=scanner.nextInt(); arrayList.add(new User(name, age, converseto(sex), phone)); } public void show() { for(User temp:arrayList) { System.out.println("姓名 "+temp.getName()+" 年齡 "+temp.getAge()+" 性別 "+converseto(temp.getsex())+" 手機號 "+temp.getPhone()); } } public void delete() throws ExecptionDemo//這是查詢函數,沒寫功能我也不該名字了怪麻煩的 {System.out.println("請輸入要查找的姓名或者手機號"); String find=scanner.next(); int flag=0; for(User temp:arrayList) { if(temp.getName().equals(find)||(temp.getPhone()+"").equals(find)) { System.out.println("姓名 "+temp.getName()+" 年齡 "+temp.getAge()+" 性別 "+converseto(temp.getsex())+" 手機號 "+temp.getPhone()); flag=1; break; } } if(flag==0) {System.out.println("文件未找到"); throw new ExecptionDemo("文件未找到"); } } public void update() throws ExecptionDemo//修改函數 { System.out.println("請輸入要查找的姓名或者手機號"); String find=scanner.next(); int flag=0; for(User temp:arrayList) { if(temp.getName().equals(find)||(temp.getPhone()+"").equals(find)) {System.out.println("請輸入 姓名"); temp.setName(scanner.next()); System.out.println("請輸入 年齡"); temp.setAge(scanner.nextInt()); System.out.println("請輸入 性別"); temp.setSex(converseto(scanner.next())); System.out.println("請輸入 手機號"); temp.setAge(scanner.nextInt()); flag=1; System.out.println("修改成功"); break; } } if(flag==0) {System.out.println("文件未找到"); throw new ExecptionDemo("文件未找到"); } } public void writeit() throws IOException//寫到文件函數 {BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(new File("1.txt"),true)); for(User temp:arrayList) {bufferedWriter.write("姓名 "+temp.getName()+" 年齡 "+temp.getAge()+" 性別 "+converseto(temp.getsex())+" 手機號 "+temp.getPhone()+"\r\n"); } bufferedWriter.flush(); bufferedWriter.close(); } public void readit() throws IOException//將文件讀入內存函數,讀進去之后可以進行查詢修改的,,因為你讀入了內存 { BufferedReader bufferedReader=new BufferedReader(new FileReader("1.txt")); String temp=bufferedReader.readLine(); while(temp!=null) {String b[]= temp.split(" "); temp=bufferedReader.readLine(); arrayList.add(new User(b[1],Integer.parseInt(b[3]),converseto(b[5]),Integer.parseInt(b[7]))); System.out.println(Arrays.toString(b)); }; System.out.println("導入文件成功"); bufferedReader.close(); } }
//自定義異常類函數ExecptionDemo.java
package 周四考試練習一波; public class ExecptionDemo extends Exception{ public ExecptionDemo(String e) throws ExecptionDemo {super(e); } }
截圖如下:............................