How to use Java to implement warehouse management functions
3.1 Create Java classes and related attributes
First, We need to create a "Goods" class to represent goods, which contains attributes such as the name, model, quantity, and purchase date of the goods.
public class Goods { private String name; private String model; private int quantity; private Date purchaseDate; // 构造方法、getters和setters省略... }
3.2 Implement the function of entering and viewing goods
Considering that the warehouse may store a large amount of goods, we use a List collection to save the goods objects and provide corresponding methods to implement the function of entering and viewing the goods. .
import java.util.ArrayList; import java.util.List; public class Warehouse { private List<Goods> goodsList; public Warehouse() { goodsList = new ArrayList<>(); } public void addGoods(Goods goods) { goodsList.add(goods); } public List<Goods> getGoodsList() { return goodsList; } }
3.3 Implement the outbound function of goods
The outbound function requires selecting the goods to be outbound based on the inventory status of the goods and filling in the outbound quantity. We can retrieve goods based on their name or model and update the inventory quantity.
public class Warehouse { // ... public void outGoods(String name, int quantity) { for (Goods goods : goodsList) { if (goods.getName().equals(name) && goods.getQuantity() >= quantity) { goods.setQuantity(goods.getQuantity() - quantity); return; } } System.out.println("库存不足,无法出库。"); } }
3.4 Implementing the query function of goods
In order to facilitate querying cargo information based on keywords, we can provide a query method in the warehouse class and return a list of goods that meet the conditions.
public class Warehouse { // ... public List<Goods> searchGoods(String keyword) { List<Goods> result = new ArrayList<>(); for (Goods goods : goodsList) { if (goods.getName().contains(keyword) || goods.getModel().contains(keyword)) { result.add(goods); } } return result; } }
3.5 Data persistence
In order to achieve persistent storage of data, we can use Java's file operations to save data to files and read the data when needed.
import java.io.*; import java.util.List; public class DataIO { public void saveData(List<Goods> goodsList, String fileName) { try (PrintWriter writer = new PrintWriter(new FileWriter(fileName))) { for (Goods goods : goodsList) { writer.println(goods.getName() + "," + goods.getModel() + "," + goods.getQuantity() + "," + goods.getPurchaseDate()); } } catch (IOException e) { e.printStackTrace(); } } public List<Goods> loadData(String fileName) { List<Goods> goodsList = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { String line; while ((line = reader.readLine()) != null) { String[] parts = line.split(","); Goods goods = new Goods(); goods.setName(parts[0]); goods.setModel(parts[1]); goods.setQuantity(Integer.parseInt(parts[2])); goods.setPurchaseDate(new SimpleDateFormat("yyyy-MM-dd").parse(parts[3])); goodsList.add(goods); } } catch (IOException | ParseException e) { e.printStackTrace(); } return goodsList; } }
The above is the detailed content of How to use Java to implement warehouse management functions. For more information, please follow other related articles on the PHP Chinese website!