我們今天用流寫一個小系統(tǒng),我們將某個人的信息(姓名,年齡,成績)通過前臺獲取數(shù)據(jù),然后再中間層做處理(封裝到這個人的對象里),最后再寫入流中;然后我們要求可以對這個人的信息可以增刪改查。
首先分層,分成前臺顯示層,后臺業(yè)務(wù)處理層,存儲層。
前臺顯示層主要的功能就是顯示功能模塊,但它并沒有真正的功能處理能力,它只是一個顯示給用戶看的界面,可以獲取用戶提交過來的數(shù)據(jù)并將數(shù)據(jù)轉(zhuǎn)交后臺。
而真正具有處理數(shù)據(jù)能力的是后臺業(yè)務(wù)處理層,這是程序的核心,他可以把用戶提交過來的數(shù)據(jù)加以處理,按照我們需要的功能去處理數(shù)據(jù),最終得到我們想要的結(jié)果。
最后我們要把處理好的數(shù)據(jù)存儲到存儲層,可以把它存儲到流、文件或者網(wǎng)絡(luò)中。當(dāng)我們想要保存數(shù)據(jù)的時候,他就可以存儲數(shù)據(jù);當(dāng)我們想要處理數(shù)據(jù)的時候,他就要把數(shù)據(jù)提取到后臺由我們來處理。
這就是業(yè)務(wù)分層,也就是我們以后寫程序的核心思想,理解這個思想可以幫助我們更快更好的寫程序,十分重要。
我們做的這個系統(tǒng)是增刪改查一個學(xué)生的信息,我們要在前臺附加上一個功能界面。
下面附代碼:
封裝好的學(xué)生類:
public class Person implements Serializable{

private String name;
private int age;
private float score;
public Person(String name,int age,float score){
this.name=name;
this.age=age;
this.score=score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
public String toString(){
return "姓名:"+this.name+",年齡:"+this.age+",成績:"+this.score;
}
}
前臺:
public class Menu {

private InputData input = null;
public Menu(){
this.input = new InputData();
while(true){
this.show();
}
}
public void show(){
System.out.println("\t\t\t1、增加人員信息:");
System.out.println("\t\t\t2、刪除人員信息:");
System.out.println("\t\t\t3、修改人員信息:");
System.out.println("\t\t\t4、瀏覽人員信息:");
System.out.println("\t\t\t5、退出系統(tǒng):");
System.out.print("請輸入選擇:");
int select = input.getInt();
switch(select){
case 1:{
new FilePerson().add();
break;
}
case 2:{
new FilePerson().remove();
break;
}
case 3:{
new FilePerson().modify();
break;
}
case 4:{
new FilePerson().search();
break;
}
case 5:{
System.out.println("退出系統(tǒng)");
System.exit(1);
}
default:{
System.out.println("請輸入數(shù)字(1-5):");
break;
}
}
}
}
看一下運行效果:

然后我們看看業(yè)務(wù)處理層:
輸入數(shù)據(jù)處理層:
他是將前臺提交過來的數(shù)據(jù)進行處理,將輸入的數(shù)據(jù)轉(zhuǎn)化成想要的整型,浮點型或者字符型;若類型不匹配則要求用戶繼續(xù)輸入。
public class InputData {

private BufferedReader input = null;
public InputData(){
this.input = new BufferedReader(new InputStreamReader(System.in));
}
public String getString(){
String str = null;
try {
str = input.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int getInt(){
int i = 0;
String str =null;
boolean b = true;
while(b){
str = this.getString();
if(!(str.matches("\\d+"))){
System.out.print("請輸入整形數(shù)據(jù):");
}
else{
i = Integer.valueOf(str);
b = false;
}
}
return i;
}
public float getFloat(){
float f = 0.0f;
String str = null;
boolean b = true;
while(b){
str = this.getString();
if(!(str.matches("\\d+?.\\d{1}"))){
System.out.print("請輸入浮點型數(shù)據(jù):(小數(shù)點后保留一位)");
}
else{
f = Float.valueOf(str);
b =false;
}
}
return f;
}
}
封裝學(xué)生層:
他是將輸入正確的前臺信息封裝到學(xué)生對象里,然后可以對該對象進行操作(增刪改查),也就是先從數(shù)據(jù)源提取數(shù)據(jù)或者存儲數(shù)據(jù),再進行處理的過程。
public class FilePerson {

private InputData input = null;
public FilePerson(){
this.input = new InputData();
}
public void add(){
String name = null;
int age =0;
float score = 0.0f;
System.out.print("請輸入姓名:");
name = input.getString();
System.out.print("請輸入年齡:");
age = input.getInt();
System.out.print("請輸入成績:");
score = input.getFloat();
Person p = new Person(name,age,score);
try {
new FileObject().save(p);
System.out.println("數(shù)據(jù)添加成功!");
} catch (Exception e) {
System.out.println("數(shù)據(jù)添加失敗!");
}
}
public void remove(){
new FileObject().clear();
}
public void modify(){
Person p = null;
try {
p = (Person)new FileObject().show();
} catch (Exception e) {
System.out.println("內(nèi)容顯示失敗,請確定數(shù)據(jù)是否存在!");
}
if(p != null){
String name = null;
int age =0;
float score = 0.0f;
System.out.print("請輸入姓名:(原姓名:"+p.getName()+")");
name = input.getString();
System.out.print("請輸入年齡:(原年齡:"+p.getAge()+")");
age = input.getInt();
System.out.print("請輸入成績:(原成績:"+p.getScore()+")");
score = input.getFloat();
p = new Person(name,age,score);
try {
new FileObject().save(p);
System.out.println("數(shù)據(jù)更新成功");
} catch (Exception e) {
System.out.println("數(shù)據(jù)更新失敗");
}
}
}
public void search(){
Person p = null;
try {
p = (Person)new FileObject().show();
} catch (Exception e) {
System.out.println("內(nèi)容顯示失敗,請確定數(shù)據(jù)是否存在!");
}
if(p != null){
System.out.println(p);
}
}
}
最后我們再看看存儲層:
他可以將封裝好的數(shù)據(jù)進行存儲,或者進行提取。
附代碼:
public class FileObject {

private static final String FILENAME = "E:\\person.txt";
public void save(Object obj) throws IOException{
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(new File(FILENAME)));
} catch (Exception e) {
e.printStackTrace();
}
Person p = (Person)obj;
out.writeObject(p);
out.flush();
out.close();
}
public Object show() throws FileNotFoundException, IOException, ClassNotFoundException{
Object obj = null;
ObjectInputStream read = null;
read = new ObjectInputStream(new FileInputStream(new File(FILENAME)));
obj = read.readObject();
read.close();
return obj;
}
public void clear(){
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(new File(FILENAME)));
} catch (Exception e) {
e.printStackTrace();
}
try {
out.reset();
out.flush();
out.close();
System.out.println("數(shù)據(jù)刪除成功");
} catch (IOException e) {
System.out.println("數(shù)據(jù)刪除失敗");
}
}
}
我們看看運行結(jié)果:




首先分層,分成前臺顯示層,后臺業(yè)務(wù)處理層,存儲層。
前臺顯示層主要的功能就是顯示功能模塊,但它并沒有真正的功能處理能力,它只是一個顯示給用戶看的界面,可以獲取用戶提交過來的數(shù)據(jù)并將數(shù)據(jù)轉(zhuǎn)交后臺。
而真正具有處理數(shù)據(jù)能力的是后臺業(yè)務(wù)處理層,這是程序的核心,他可以把用戶提交過來的數(shù)據(jù)加以處理,按照我們需要的功能去處理數(shù)據(jù),最終得到我們想要的結(jié)果。
最后我們要把處理好的數(shù)據(jù)存儲到存儲層,可以把它存儲到流、文件或者網(wǎng)絡(luò)中。當(dāng)我們想要保存數(shù)據(jù)的時候,他就可以存儲數(shù)據(jù);當(dāng)我們想要處理數(shù)據(jù)的時候,他就要把數(shù)據(jù)提取到后臺由我們來處理。
這就是業(yè)務(wù)分層,也就是我們以后寫程序的核心思想,理解這個思想可以幫助我們更快更好的寫程序,十分重要。
我們做的這個系統(tǒng)是增刪改查一個學(xué)生的信息,我們要在前臺附加上一個功能界面。
下面附代碼:
封裝好的學(xué)生類:


































前臺:














































看一下運行效果:

然后我們看看業(yè)務(wù)處理層:
輸入數(shù)據(jù)處理層:
他是將前臺提交過來的數(shù)據(jù)進行處理,將輸入的數(shù)據(jù)轉(zhuǎn)化成想要的整型,浮點型或者字符型;若類型不匹配則要求用戶繼續(xù)輸入。






















































封裝學(xué)生層:
他是將輸入正確的前臺信息封裝到學(xué)生對象里,然后可以對該對象進行操作(增刪改查),也就是先從數(shù)據(jù)源提取數(shù)據(jù)或者存儲數(shù)據(jù),再進行處理的過程。













































































最后我們再看看存儲層:
他可以將封裝好的數(shù)據(jù)進行存儲,或者進行提取。
附代碼:












































我們看看運行結(jié)果:




