要求:
1、完成一個對水果倉庫管理的簡單系.。
2、完成,增加、查詢、修改簡單的操作。
分析:
1、首先我們要有進(jìn)行分成分析 表層----操作層----底層;
2、表層有什么 :可供操作的選項即(增、查、改);
3、操作層有什么:完成不同的操作(讀文件和寫文件);
4、底層有什么:完成數(shù)據(jù)的寫出和讀入;
步驟:
先完成如下javaproject的創(chuàng)建:

//下面為每個類中的具體內(nèi)容(按我做改程序的順須寫的)
//Fruit
package VO;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Fruit implements Serializable {
//屬性
private String name;
private String area;
private float kg;
//構(gòu)造方法
public Fruit(){}
public Fruit(String name,String area,float kg){
this.name =name;
this.area =area;
this.kg =kg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
public float getKg() {
return kg;
}
public void setKg(float kg) {
this.kg = kg;
}
public String toString (){
return "水果名:"+this.name +" 源產(chǎn)地:"+this.area+" 現(xiàn)重量:"+this.kg ;
}
}
//主方法
package Main;
import Menu.Menu;
public class Main {
//主方法
public static void main(String[] args){
System.out.println("簡單的水果倉庫管理系統(tǒng)");
//調(diào)用Menu方法
new Menu();
}
}
//Menu
package Menu;
import OP.FruitOperate;
import Util.InputDate;
public class Menu {
InputDate input= null;
public Menu(){
this.input=new InputDate();
//循環(huán)出現(xiàn)菜單
while (true){
this.show();
}
}
//需要定義的菜單內(nèi)容
private 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、注銷管理系統(tǒng)");
System.out.print("\n\n請選擇您要使用的操作:");
int temp = input.getInt();
switch(temp){
case 1:{ // 增加水果庫存
new FruitOperate().add(); //業(yè)務(wù)處理層
break;
}
case 2:{ // 瀏覽庫存信息
new FruitOperate().show();
break;
}
case 3:{ // 修改庫存信息
new FruitOperate().update();
break;
}
case 4:{ //注銷管理系統(tǒng)
System.out.println("注銷中******************");
System.out.println("注銷成功!");
System.exit(1);
}
default:{ //錯誤信息提示
System.out.println("請選擇正確的操作范圍如下:");
break;
}
}
}
}
//對度入數(shù)據(jù)的相關(guān)操作
package Util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputDate {
private BufferedReader buf =null;
public InputDate(){
buf = new BufferedReader(new InputStreamReader(System.in));
};
public String getString(){
String str = null;
try {
str = buf.readLine();
} catch (IOException e) {}
return str;
}
public int getInt(){
int temp = 0;
//如果輸入的不是數(shù)字,告訴用戶輸入錯了~
//可以使用正則驗證
String str = null;
boolean flag = true;
while(flag){
//輸入數(shù)據(jù)
str = this.getString();
if (!(str.matches("\\d+"))){
//如果輸入的不是一個數(shù)字,則必須重新輸入
System.out.print("輸入的內(nèi)容必須是上面操作代號的范圍,請重新輸入:");
}else{
//輸入的是一個正確的數(shù)字,則可以進(jìn)行轉(zhuǎn)換
temp = Integer.parseInt(str);
//表示退出循環(huán)
flag = false;
}
}
return temp;
}
public float getFloat() {
float f = 0.0f;
//如果輸入的不是數(shù)字,提示告訴用戶輸入錯了~
//可以使用正則驗證
String str = null;
boolean flag = true;
while(flag){
//輸入數(shù)據(jù)
str = this.getString();
if (!(str.matches("\\d+?.\\d{1,2}"))){
//如果輸入的不是一個數(shù)字,則必須重新輸入
System.out.print("輸入的內(nèi)容必須是小數(shù)(小數(shù)點后兩位),請重新輸入:");
}else{
//輸入的是一個正確的數(shù)字,則可以進(jìn)行轉(zhuǎn)換
f = Float.parseFloat(str);
//表示退出循環(huán)
flag = false;
}
}
return f;
}
}
///實現(xiàn)管理的方法類
package OP;
import Util.FileOperate;
import Util.InputDate;
import VO.Fruit;
public class FruitOperate {
private InputDate input = null;
public FruitOperate(){
this.input = new InputDate();
}
//完成具體的Fruit對象操作
public void add(){
//要使用輸入數(shù)據(jù)的類
String name = null;
String area = null;
float kg = 0.0f;
System.out.print("輸入水果名:");
name = this.input.getString();
System.out.print("輸入源產(chǎn)地:");
area = this.input.getString();
System.out.print("輸入入庫量:");
kg = this.input.getFloat();
//生成Fruit對象,把對象保存在文件中
Fruit f = new Fruit(name,area,kg);
try{
new FileOperate().save(f); //io操作層
System.out.println("數(shù)據(jù)保存成功!");
}catch(Exception e){
System.out.println("數(shù)據(jù)保存失敗!");
}
}
public void show(){
//從文件中把內(nèi)容讀進(jìn)來
Fruit f = null;
try{
f = (Fruit) new FileOperate().read();
}catch(Exception e){
System.out.println("內(nèi)容顯示失敗,請確定數(shù)據(jù)是否存在!");
}
if(f != null){
System.out.println(f);
}
}
public void update(){
//先將之前的信息查出來
Fruit f = null;
try{
f = (Fruit) new FileOperate().read();
}catch(Exception e){
System.out.println("內(nèi)容顯示失敗,請確定數(shù)據(jù)是否存在!");
}
if(f != null){
String name = null;
String area = null;
float kg =0.0f;
System.out.print("請輸入新的水果名(原水果名:"+f.getName()+")");
name = this.input.getString();
System.out.print("請輸入新的源產(chǎn)地(原源產(chǎn)地:"+f.getArea()+")");
area = this.input.getString();
System.out.print("請輸入新的庫總量(原庫總量:"+f.getKg()+")");
kg = this.input.getFloat();
//信息重新設(shè)置
f.setName(name);
f.setArea(area);
f.setKg(kg);
try{
new FileOperate().save(f);
System.out.println("數(shù)據(jù)更新成功!");
}catch(Exception e){
System.out.println("數(shù)據(jù)更新失敗!");
}
}
}
}
//底層操作 文件的讀入和讀出
package Util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class FileOperate {
public static final String FILENAME = "E:\\fruit.txt";
//把對象保存在文件之中
public void save(Object obj){
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(new File(FILENAME)));
//寫入對象
out.writeObject(obj);
}catch(Exception e){
try {
throw e;
} catch (Exception e1) {}
}finally {
try {
out.close();
}catch(Exception e){}
}
}
//把對象從文件之中讀出來
public Object read() throws Exception{
Object obj = null;
ObjectInputStream input =null;
try {
input = new ObjectInputStream(new FileInputStream(new File(FILENAME)));
obj = input.readObject();
} catch (Exception e) {
throw e;
}finally{
try{
input.close();
}catch(Exception e){}
}
return obj;
}
}
運行結(jié)果如下:


這篇文章只是一個原理 ,感謝閱讀 希望能夠提供寶貴意見!QQ:237333696