简体   繁体   中英

New to Java - help on design

I'm looking to create a basic order system to get my head around the OO concepts as I'm learning from Head First Java.

As I see it my core classes would be:
Customer: id, name, zip
Order: id, date, customer_id, total_value, product_id, unit price, quantity
Stock: id, name, unit price

If i applied simple normalization of my objects, I would expect to break down order into order and order line eg:

Order:
Order ID
Customer ID
Total Value
Order Date

Order Line:
Order ID
Line ID
Product ID
Quantity
Unit Price
Total Line Value

The question is if I have an Array, List, LinkedList, how do I pass in the reference for another value for my order and order line eg What is the best way to handle passing in the customer ID from customer object to the order object and the product ID from Product object into my order line object?

Currently my basic quote looks like this:

Customer:

public class Customer {

   int id;
   String name;
   String zip;

public Customer() {

}

public Customer(int i, String n, String z) {
    setId(i);
    setName(n);
    setZip(z);
}`

Products:

public class Inventory {
  int id;
  String name;
  double price;

public Inventory(int i, String string, double d) {
    // TODO Auto-generated constructor stub

    setId(i);
    setName(string);
    setPrice(d);

}

Order:

public class Order {
int orderID;
String orderDate;
int productID;
int Quantity;
double unitPrice;
double orderTotal;

public Order(int o, String oD, int pI, int q, double uP) {
    setOrderID(o);
    setOrderDate(oD);
    setProductID(pI);
    setQuantity(q);
    setUnitPrice(uP);
    setOrderTotal(uP, q);

I then have the following in my main and don't think this is the right way to implement:

public class OrderSystem {

static List<Inventory> inventory = new ArrayList<Inventory>();
static List<Customer> customers = new ArrayList<Customer>();
static List<Order> orders = new ArrayList<Order>();

public OrderSystem() {
    inventory.add(new Inventory(1, "Nails", 2.50));
    inventory.add(new Inventory(2, "Screws", 2.95));
    inventory.add(new Inventory(3, "Pins", 3.50));
    inventory.add(new Inventory(4, "Tape", 4.50));
    inventory.add(new Inventory(5, "Wire", 2.99));
    customers.add(new Customer(1, "Bob", "01305"));
}

public static void main(String[] args) {
    new OrderSystem();
    mainMenu();
    // TODO Auto-generated method stub
}

public static void mainMenu() {
    Scanner input = new Scanner(System.in);

    clearConsole();
    System.out.println("Please enter a validation selection");
    System.out.println("Option 1: Inventory Add");
    System.out.println("Option 2: Inventory List");
    System.out.println("Option 3: Customer Add");
    System.out.println("Option 4: Customer List");
    System.out.println("Option 5: Create Order");
    System.out.println("Option 6: Print Orders");
    System.out.println("Option 7: Quit");
    int selection = input.nextInt();

    switch (selection) {
    case 1:
        addInventory();
        mainMenu();
    case 2:
        printInventory();
        mainMenu();
    case 3:
        addCustomer();
        mainMenu();
    case 4:
        printCustomer();
        mainMenu();
    case 5:
        addOrder();
        mainMenu();
    case 6:
        printOrder();
        mainMenu();
    case 7:
        System.exit(0);
    default:
        System.out.println("Invalid selection!");
        pressAnyKeyToContinue();
        mainMenu();
    }

}


private static void addOrder() {
    // TODO Auto-generated method stub
    clearConsole();
    Scanner input = new Scanner(System.in);
    System.out.println("Please enter Order ID: ");
    int orderID = input.nextInt();
    System.out.println("Please enter Order Date: ");
    String orderDate = input.next();
    printInventory();
    System.out.println("Please enter the Product ID: ");
    int productID = input.nextInt();

    List<Inventory> items = inventory.stream().filter(p -> p.getId() == productID).collect(Collectors.toList());

    int prodID = items.get(0).getId();
    System.out.println("Selected Product: " + items.get(0).getName());
    System.out.println("Unit Price: " + items.get(0).getPrice());

    System.out.println("Please enter Order Quantity: ");
    int orderQuantity = input.nextInt();
    double orderTotal = items.get(0).getPrice() * orderQuantity;

    System.out.println("Total Order Value: $" + orderTotal);
    input.nextLine();
    orders.add(new Order(orderID, orderDate, prodID, orderQuantity, items.get(0).getPrice()));
    pressAnyKeyToContinue();

}

What are peoples thoughts? Again not looking for you to re-write the code as i want to learn. I'm looking for whether i'm down wrong garden path, is there a better way. Should I use List, ArrayList or other?

why not have a List of Order objects for each Customer object? Also, for each Order object, have a List of Product objects. This way, a customer can have multiple orders and each order can have multiple products.

by the way, you should also think about how these classes would be used and how much of an overlap exists between various members. for example if its more Customer centric then we would like to be able to get all the information from the Customer class. If its more Order centric, then we can modify the Order class so that it gives us what we need

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM