๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

Java Project: Stock Room Inventory

A stock room program keeps items in a map keyed by name. It saves the stock to a file and reads it back for sorted reports.

The Item class, equal by name

Two items share the name Pen but differ in price and count. The equals and hashCode checks both print true.

public class Main {
  public static void main(String[] args) {
    Item first = new Item("Pen", 2.5, 40);
    Item second = new Item("Pen", 3.0, 10);
    System.out.println(first.equals(second));
    System.out.println(first.hashCode() == second.hashCode());
  }
}
class Item {
  String name;
  double price;
  int count;
  Item(String name, double price, int count) {
    this.name = name;
    this.price = price;
    this.count = count;
  }
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (!(other instanceof Item)) {
      return false;
    }
    Item item = (Item) other;
    return name.equals(item.name);
  }
  public int hashCode() {
    return name.hashCode();
  }
}

Save the stock to a file

The map holds three items, and the writer saves them in sorted name order. Reading the file back shows the saved lines.

import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
public class Main {
  public static void main(String[] args) throws Exception {
    HashMap<String, Item> stock = new HashMap<String, Item>();
    stock.put("Pen", new Item("Pen", 2.5, 40));
    stock.put("Notebook", new Item("Notebook", 4.0, 25));
    stock.put("Eraser", new Item("Eraser", 1.5, 8));
    List<String> names = new ArrayList<String>(stock.keySet());
    Collections.sort(names);
    try (BufferedWriter writer = Files.newBufferedWriter(Path.of("stock.txt"))) {
      for (String name : names) {
        Item item = stock.get(name);
        writer.write(item.name + "," + item.price + "," + item.count);
        writer.newLine();
      }
    }
    List<String> lines = Files.readAllLines(Path.of("stock.txt"));
    for (String line : lines) {
      System.out.println(line);
    }
  }
}
class Item {
  String name;
  double price;
  int count;
  Item(String name, double price, int count) {
    this.name = name;
    this.price = price;
    this.count = count;
  }
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (!(other instanceof Item)) {
      return false;
    }
    Item item = (Item) other;
    return name.equals(item.name);
  }
  public int hashCode() {
    return name.hashCode();
  }
}

Example

import java.io.BufferedWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
public class Main {
  public static void main(String[] args) throws Exception {
    HashMap<String, Item> stock = new HashMap<String, Item>();
    stock.put("Pen", new Item("Pen", 2.5, 40));
    stock.put("Notebook", new Item("Notebook", 4.0, 25));
    stock.put("Eraser", new Item("Eraser", 1.5, 8));
    List<String> names = new ArrayList<String>(stock.keySet());
    Collections.sort(names);
    try (BufferedWriter writer = Files.newBufferedWriter(Path.of("stock.txt"))) {
      for (String name : names) {
        Item item = stock.get(name);
        writer.write(item.name + "," + item.price + "," + item.count);
        writer.newLine();
      }
    }
    HashMap<String, Item> fresh = new HashMap<String, Item>();
    List<String> lines = Files.readAllLines(Path.of("stock.txt"));
    for (String line : lines) {
      String[] parts = line.split(",");
      Item item = new Item(parts[0], Double.parseDouble(parts[1]), Integer.parseInt(parts[2]));
      fresh.put(item.name, item);
    }
    List<Item> items = new ArrayList<Item>(fresh.values());
    Collections.sort(items, new NameOrder());
    for (Item item : items) {
      System.out.println(item.name + ": " + item.count + " at " + item.price);
    }
    items.stream().filter(item -> item.count < 10).forEach(item -> System.out.println("Low stock: " + item.name));
    double total = items.stream().mapToDouble(item -> item.price * item.count).sum();
    System.out.println("Total value: " + total);
    Item found = fresh.get("Pen");
    System.out.println("Found: " + found.name + ": " + found.count + " at " + found.price);
  }
}
class Item {
  String name;
  double price;
  int count;
  Item(String name, double price, int count) {
    this.name = name;
    this.price = price;
    this.count = count;
  }
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (!(other instanceof Item)) {
      return false;
    }
    Item item = (Item) other;
    return name.equals(item.name);
  }
  public int hashCode() {
    return name.hashCode();
  }
}
class NameOrder implements Comparator<Item> {
  public int compare(Item first, Item second) {
    return first.name.compareTo(second.name);
  }
}

Key points

  • An Item class holds a name, a price, and a count for one stock line.
  • The equals and hashCode methods agree on the name, so two items with one name count as the same item.
  • A HashMap keyed by name finds any item from its name alone.
  • The saved file holds one line per item, in name, price, count order.
  • Reading the file back builds fresh Item objects from the saved lines.
  • A Comparator sorts the items by name before any listing prints.
  • A stream filters the low-stock items and sums price times count for the total value.

In this example, the program saves three items to stock.txt in name order, then reads the file back into fresh Item objects. The inventory prints sorted by name through the NameOrder comparator, the stream filter names Eraser as the only low-stock item, the stream sum totals the stock value at 212.0, and the lookup by name finds Pen.

Note

Every listing in this lesson prints in name order, never in map order.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

Which collection keys the stock by item name in this project?