今天使用數組和循環結構實現了一個簡單的商品管理系統
主要功能如下:
1.查詢所有商品、2.添加、3.修改、4.刪除、5.退出
- 查詢:
編號 | 名稱 | 價格 |
1 | 蘋果 | 3.1 |
2 | 橘子 | 3.5 |
-
添加:
輸入編號名稱價格添加到數組中
-
修改:
根據編號修改名稱、價格
-
刪除:
根據編號刪除商品
-
退出:
結束程序, 謝謝使用
import java.util.Scanner;
public class ShoppingManageSystem {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] ids = {1,2,3};
String[] names = {"香蕉","蘋果","橘子"};
double[] prices = {1.2,3.2,1.5};
//歡迎界面
System.out.println("*********************************");
System.out.println("*****歡迎使用JJ商品管理系統*******");
System.out.println("*********************************");
System.out.println("=================================");
//功能選擇
while (true) {
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("**************************");
System.out.println("請選擇功能:");
String choose = input.next();
if (choose.equals("1")) { //查詢商品功能
System.out.println("編號\t名稱\t價格");
System.out.println("-------------------------");
for (int i = 0; i < prices.length; i++) {
if (ids[i]>=0) {
System.out.print(ids[i] + "\t");
System.out.print(names[i] + "\t");
System.out.println(prices[i] + "\t");
}
}
System.out.println();
}else if (choose.equals("2")) { //添加商品功能
int addId;
String addName;
double addPrice;
while (true) {
//輸入添加的商品信息
System.out.println("請輸入要添加的商品編號");
addId = input.nextInt();
System.out.println("請輸入要添加的商品名稱");
addName = input.next();
System.out.println("請輸入要添加的商品價格");
addPrice = input.nextDouble();
boolean flag = true;
//判斷編號是否重複,重複從新輸入,不重複繼續添加
for (int i = 0; i < ids.length; i++) {
if (addId == ids[i]) {
System.out.println("編號重複請重試");
flag = false;
}
}
if(flag == false){
continue;
}
//新建數組
int[] newIds = new int[ids.length+1];
String[] newNames = new String[names.length+1];
double[] newPrices = new double[prices.length+1];
//將數據放入新的數組中
for (int i = 0; i < prices.length; i++) {
newIds[i] = ids[i];
newNames[i] = names[i];
newPrices[i] = prices[i];
}
newIds[newIds.length-1] = addId;
newNames[newNames.length-1] = addName;
newPrices[newPrices.length-1] = addPrice;
//數組賦值
ids = newIds;
names = newNames;
prices = newPrices;
System.out.println("Success!");
break;
}
}else if (choose.equals("3")) { //修改商品功能
int updateId;
String updateName;
double updatePrice;
while (true) {
//輸入添加的商品信息
System.out.println("請輸入要修改的商品編號");
updateId = input.nextInt();
System.out.println("請輸入要修改的商品名稱");
updateName = input.next();
System.out.println("請輸入要修改的商品價格");
updatePrice = input.nextDouble();
boolean flag = true;
//判斷編號是否存在,不存在從新輸入,存在修改
for (int i = 0; i < ids.length; i++) {
if (updateId == ids[i]) {
names[i] = updateName;
prices[i] = updatePrice;
flag = false;
System.out.println("修改成功!");
}else{
}
}
if(flag == false){
break;
}
System.out.println("編號不存在,請重新輸入!");
}
}else if (choose.equals("4")) { //刪除商品功能
while(true){
System.out.println("請輸入要刪除的商品編號:");
int delId = input.nextInt();
boolean flag = true;
for (int i = 0; i < ids.length; i++) {
if (delId == ids[i]) {
ids[i] = -1;
System.out.println("刪除成功!");
flag = false;
}else{
}
}
if (flag == false) {
break;
}
System.out.println("沒有該商品,請從新選擇!");
}
}else if (choose.equals("5")) { //退出
break;
}else{
System.out.println("您輸入的功能不存在,請重新輸入:");
}
}
System.out.println("程序結束,謝謝使用。");
}
}