//PM6_3.java
/*
請建立一套商品登錄系統,功能有:顯示所有資料、增加商品資料、儲存資料;
*
資料儲存於storage.data檔案內
*/
/* Element.class
須於同目錄下
*/
import java.io.*;
import java.util.Scanner;
public class PM6_3 {
static Element[] art; //
商品資料的物件陣列
static int number; //
紀錄儲存筆數
static Scanner keyin; //
鍵盤輸入物件
static String file_R; //
庫存檔案
public static void main(String args[]) throws
IOException {
keyin = new Scanner(System.in);
art = new Element[50];
number=0; //
儲存資料的筆數
file_R = "storage.data";
read_data();
mainMenu();
int select = keyin.nextInt();
keyin.nextLine();
while (select !=5) {
switch (select) {
case 1: /*
顯示所有資料
*/
disp_data();
break;
case 2: /*
增加商品資料
*/
add_data();
break;
case 3: /*
修改商品資料
*/
modify_data();
break;
case 4: /*
儲存資料
*/
save_data();
break;
default:
System.out.printf("錯誤輸入,
請重新選擇
!!\n");
}
mainMenu();
select = keyin.nextInt();
keyin.nextLine();
}
}
/*
主工作項目選單
*/
public static void mainMenu() {
System.out.printf("\n==
春嬌超市
商品管理系統
==\n");
…..
….
}
/*
將
stroage.data
檔案輸入至系統
*/
public static void read_data() throws IOException {
File fileID = new File(file_R); //
產生輸入檔案物件
String inData;
if (fileID.exists()) {
BufferedReader data = new BufferedReader(new
FileReader(fileID));
while ((inData=data.readLine()) != null) {
Scanner s = new
Scanner(inData).useDelimiter("\t");
art[number] = new Element();
art[number].ID = s.next();
art[number].name = s.next();
art[number].price = s.nextInt();
art[number].unit = s.next();
art[number].stock = s.nextInt();
art[number].maker = s.next();
number = number +1;
}
data.close();
}
else {
System.out.printf("%s
檔案不存在,
請先建立它
\n", file_R);
System.out.printf("按
<Enter>
鍵離開
=>");
keyin.nextLine();
System.exit(1);
}
}
/*
顯示所有資料
*/
public static void disp_data() {
System.out.printf("==
列印所有商品資料
==\n");
System.out.printf("代號\t品名\t\t單價\t單位\t安全庫存量\t製造商\n");
for (int i=0; i<number; i++) {
System.out.printf("%s\t", art[i].ID);
System.out.printf("%4s\t", art[i].name);
System.out.printf("%d\t", art[i].price);
System.out.printf("%s\t", art[i].unit);
System.out.printf("%d\t\t", art[i].stock);
System.out.printf("%s\n", art[i].maker);
}
}
/*
增加商品資料
*/
public static void add_data(){
int k=number;
art[k] = new Element();
System.out.printf("請輸入商品編號
=>");
art[k].ID = keyin.nextLine();
System.out.printf("請輸入商品名稱
=>");
art[k].name = keyin.nextLine();
System.out.printf("請輸入單價
=>");
art[k].price = keyin.nextInt();
keyin.nextLine();
System.out.printf("請輸入單位
(個/箱/公斤)
=>");
art[k].unit = keyin.nextLine();
System.out.printf("請輸入庫存量
=>");
art[k].stock = keyin.nextInt();
keyin.nextLine();
System.out.printf("請輸入製造商
=>");
art[k].maker = keyin.nextLine();
number = number+1;
}
/*
修改商品資料
*/
public static void modify_data(){
System.out.printf("請輸入欲修改的商品編碼
=>");
String num = keyin.nextLine();
int flag = 0, test;
int i = 0;
while(i < number) {
test = art[i].ID.compareTo(num);
if (test == 0) {
flag = 1;
break;
}
i = i + 1;
}
if (flag == 0) {
System.out.printf("沒有
%s
資料,
拒絕處理
!!\n", num);
return;
}
System.out.printf("[%s]請輸入商品名稱
=>", art[i].name);
art[i].name = keyin.nextLine();
System.out.printf("[%d]請輸入單價
=>", art[i].price);
art[i].price = keyin.nextInt();
keyin.nextLine();
System.out.printf("[%s]請輸入單位
(個/箱/公斤)
=>", art[i].unit);
art[i].unit = keyin.nextLine();
System.out.printf("[%d]請輸入庫存量
=>", art[i].stock);
art[i].stock = keyin.nextInt();
keyin.nextLine();
System.out.printf("[%s]請輸入製造商
=>", art[i].maker);
art[i].maker = keyin.nextLine();
}
/*
儲存商品資料至
storage.data */
public static void save_data() throws IOException {
BufferedWriter outData = new BufferedWriter(new
FileWriter(file_R));
System.out.printf("**
將儲存檔案
(storage.data) **\n");
for (int i=0; i<number; i++) {
outData.write(art[i].ID + "\t");
outData.write(art[i].name + "\t");
outData.write(art[i].price + "\t");
outData.write(art[i].unit + "\t");
outData.write(art[i].stock + "\t");
outData.write(art[i].maker + "\n");
}
outData.close();
System.out.printf("*****
儲存完畢
****\n");
}
} |