源代码:
import java.util.Scanner;
// 数据类 WarehouseInformation
class WarehouseInformation {
private String productId;
private String productName;
private String supplier;
private String entryDate;
private int warehouseNumber;
private String location;
private int stockQuantity;
public WarehouseInformation(String productId, String productName, String supplier, String entryDate, int warehouseNumber, String location, int stockQuantity) {
this.productId = productId;
this.productName = productName;
this.supplier = supplier;
this.entryDate = entryDate;
this.warehouseNumber = warehouseNumber;
this.location = location;
this.stockQuantity = stockQuantity;
}
public void setProductName(String productName) {
this.productName = productName;
}
public void setSupplier(String supplier) {
this.supplier = supplier;
}
public void setEntryDate(String entryDate) {
this.entryDate = entryDate;
}
public void setWarehouseNumber(int warehouseNumber) {
this.warehouseNumber = warehouseNumber;
}
public void setLocation(String location) {
this.location = location;
}
public void setStockQuantity(int stockQuantity) {
this.stockQuantity = stockQuantity;
}
public String getProductId() {
return productId;
}
public String getProductName() {
return productName;
}
public String getSupplier() {
return supplier;
}
public String getEntryDate() {
return entryDate;
}
public int getWarehouseNumber() {
return warehouseNumber;
}
public String getLocation() {
return location;
}
public int getStockQuantity() {
return stockQuantity;
}
}
// 主类 WarehouseManagement
public class WarehouseManagement {
private static WarehouseInformation[] warehouse; // 仓库信息数组
private static int warehouseCount = 0;
public static void main(String[] args) {
// 初始化仓库数组
warehouse = new WarehouseInformation[100];
// 在这里调用主界面方法
mainFrame();
}
public static void mainFrame() {
// 绘制主界面,获取用户输入,判断选择功能界面
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("请选择功能:");
System.out.println("1. 商品入库");
System.out.println("2. 商品修改");
System.out.println("3. 商品出库");
System.out.println("4. 统计商品库存数量");
System.out.println("0. 退出");
choice = scanner.nextInt();
switch (choice) {
case 1:
goodsWarehousing();
break;
case 2:
modifyWarehouse();
break;
case 3:
outboundWarehouse();
break;
case 4:
countGoods();
break;
case 0:
System.out.println("退出程序");
break;
default:
System.out.println("请选择有效的功能!");
break;
}
} while (choice != 0);
}
public static void goodsWarehousing() {
// 商品入库方法的实现
Scanner scanner = new Scanner(System.in);
System.out.println("请输入商品编号:");
String productId = scanner.next();
// 检查商品是否已存在
int existingIndex = findWarehouseIndex(productId);
if (existingIndex != -1) {
System.out.println("商品已存在,无法入库。");
return;
}
System.out.println("请输入商品名称:");
String productName = scanner.next();
System.out.println("请输入供货商信息:");
String supplier = scanner.next();
System.out.println("请输入入库时间:");
String entryDate = scanner.next();
System.out.println("请输入存放仓库号:");
int warehouseNumber = scanner.nextInt();
System.out.println("请输入存放位置信息:");
String location = scanner.next();
System.out.println("请输入入库数量:");
int stockQuantity = scanner.nextInt();
if (stockQuantity <= 0) {
System.out.println("入库数量必须大于0。");
return;
}
System.out.println("是否确认入库(Y/N):");
String confirmation = scanner.next();
if (confirmation.equalsIgnoreCase("Y")) {
// 调用addData()方法追加一条新记录
WarehouseInformation newWarehouse = new WarehouseInformation(productId, productName, supplier, entryDate, warehouseNumber, location, stockQuantity);
addData(newWarehouse);
System.out.println("商品入库成功!");
}
}
public static void modifyWarehouse() {
// 商品修改方法的实现
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要修改的商品编号:");
String productId = scanner.next();
int index = findWarehouseIndex(productId);
if (index == -1) {
System.out.println("商品不存在,无法修改。");
return;
}
WarehouseInformation warehouseInfo = warehouse[index];
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("6. 入库数量");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("请输入新的商品名称:");
String newProductName = scanner.next();
warehouseInfo.setProductName(newProductName);
break;
case 2:
System.out.println("请输入新的供货商信息:");
String newSupplier = scanner.next();
warehouseInfo.setSupplier(newSupplier);
break;
case 3:
System.out.println("请输入新的入库时间:");
String newEntryDate = scanner.next();
warehouseInfo.setEntryDate(newEntryDate);
break;
case 4:
System.out.println("请输入新的存放仓库号:");
int newWarehouseNumber = scanner.nextInt();
warehouseInfo.setWarehouseNumber(newWarehouseNumber);
break;
case 5:
System.out.println("请输入新的存放位置信息:");
String newLocation = scanner.next();
warehouseInfo.setLocation(newLocation);
break;
case 6:
System.out.println("请输入新的入库数量:");
int newStockQuantity = scanner.nextInt();
warehouseInfo.setStockQuantity(newStockQuantity);
break;
default:
System.out.println("请选择有效的信息编号。");
break;
}
System.out.println("商品信息修改成功!");
}
public static void outboundWarehouse() {
// 商品出库方法的实现
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要出库的商品编号:");
String productId = scanner.next();
int index = findWarehouseIndex(productId);
if (index == -1) {
System.out.println("商品不存在,无法出库。");
return;
}
WarehouseInformation warehouseInfo = warehouse[index];
int stockQuantity = warehouseInfo.getStockQuantity();
System.out.println("商品库存数量为:" + stockQuantity);
System.out.println("请输入出库数量:");
int quantityToOutbound = scanner.nextInt();
if (quantityToOutbound <= 0 || quantityToOutbound > stockQuantity) {
System.out.println("出库数量无效,无法出库。");
return;
}
warehouseInfo.setStockQuantity(stockQuantity - quantityToOutbound);
System.out.println("商品出库成功!");
}
public static void countGoods() {
// 统计商品库存数量方法的实现
for (int i = 0; i < warehouseCount; i++) {
WarehouseInformation warehouseInfo = warehouse[i];
String productId = warehouseInfo.getProductId();
String productName = warehouseInfo.getProductName();
int stockQuantity = warehouseInfo.getStockQuantity();
System.out.println("商品编号:" + productId);
System.out.println("商品名称:" + productName);
System.out.println("库存数量:" + stockQuantity);
System.out.println();
}
}
public static void addData(WarehouseInformation newWarehouse) {
if (warehouseCount < warehouse.length) {
warehouse[warehouseCount++] = newWarehouse;
} else {
System.out.println("仓库已满,无法继续入库。");
}
}
public static int findWarehouseIndex(String productId) {
for (int i = 0; i < warehouseCount; i++) {
if (warehouse[i].getProductId().equals(productId)) {
return i;
}
}
return -1;
}
}
标签:scanner,String,int,System,println,源代码,out From: https://www.cnblogs.com/Hugo-Martin/p/17708209.html