java大作業總結——圖書借閱管理系統

孤木不成林發表於2018-11-26

這幾天,一直在忙著寫java大作業,想總結一下這次作業學到的東西。

定義member類

//定義member類
package com.homework;

public class Member {
	private String num;
	private String name;
	private String phone;
	public Member(String num,String name,String phone) {
		this.num=num;
		this.name=name;
		this.phone=phone;
	}
	public String getNum() {
		return this.num;
	}
	public String getName() {
		return this.name;
	}
	public String getPhone() {
		return this.phone;
	}
	public void setNum(String num) {
		this.num=num;
	}
	public void setName(String name) {
		this.name=name;
	}
	public void setPhone(String phone) {
		this.phone=phone;
	}
	public String toString() {
		return "編號: "+this.num+" 姓名: "+this.name+" 電話號: "+this.phone;
	}
}

memberSystem類中有關定義

Member arr[]=new Member[1000];
	private int x=0;

 

1.時間延遲函式

Thread.currentThread();
try {
		Thread.sleep(1000);
} catch (InterruptedException e) {
		e.printStackTrace();
}

2.檔案讀寫

將member陣列寫入TXT檔案中

//將member陣列寫入TXT檔案中
public class MemberSystem {
	Member arr[]=new Member[1000];
	private int x=0;
	private int s=0;
	private int n=0;
	private int a=0;
	Scanner in=new Scanner(System.in);
	public void writeTxt() {
		boolean b=false;
		File fMember=new File("member.txt");
		if(!fMember.exists()) {
			b=false;
			try {
				b=fMember.createNewFile();
			}catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
				if(b) {
					System.out.println("資訊已儲存");
				}
			}
		}
		try {
			Writer w=new FileWriter(fMember);
			w.write(toStringMember());
			w.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("儲存完成");
	}

toStringMember函式

public String toStringMember() {
		String str="";
		for(int i=0;i<x;i++) {
			str=str+arr[i].toString()+"\r\n";
		}
		return str;
	}

 

addMember函式

public void addMember(String num,String name,String phone) {
		arr[x]=new Member(num, name, phone);
		x++;

	}

從TXT檔案中讀取陣列元素

public void readMember() {
		String pathname = "member.txt"; 
        try (FileReader reader = new FileReader(pathname);
             BufferedReader br = new BufferedReader(reader) 
        ) {
            String line;
           
            while ((line = br.readLine()) != null) {
            	String[] st=line.split(" ");
            	arr[x]=new Member("","","");
            	arr[x].setName(st[3]);
            	arr[x].setNum(st[1]);
            	arr[x].setPhone(st[5]);
            	x++;
            	//this.addMember(st[1], st[3], st[5]);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

	}

 

相關文章