Java基础案例3-1:基于控制台的购书系统

【案例3-1】基于控制台的购书系统

【案例介绍】

伴随互联网的蓬勃发展,网络购书系统作为电子商务的一种形式,正以其高效、低成本的优势逐步成为新兴的经营模式,人们已经不再满足互联网的用途仅仅局限于信息的浏览和发布,更渴望着能够充分享受互联网所带来的更多便利。网络购书系统正适应了当今社会快节奏地生活,使顾客足不出户便可以方便快捷轻松地选购自己喜欢的图书。
本任务要求,使用所学知识编写一个基于控制台的购书系统,实现购书功能。

  • 输出所有图书的信息:包括每本书的编号、书名、单价、库存。
  • 顾客购买书时,根据提示输入图书编号来选购需要的书,并根据提示输入购买书的的数量。
  • 购买完毕后输出顾客的订单信息,包括:订单号、订单明细、订单总额。

【分析】

(1)通过任务描述可知,该系统中必须包括3个实体类,类名及属性设置如下:

  1. 图书类(Book):
    a) 图书编号(id)
    b) 图书名称(name)
    c) 图书单价(price)
    d) 库存数量(storage)
  2. 订单项类(OrderItem):
    a) 图书(book)
    b) 购买数量(num)
  3. 订单类(Order):
    a) 订单号(orderID)
    b) 订单总额(total)
    c) 订单项列表(items)

(2)由于购买图书时,需要选择图书的数量,所以需要在订单项类里定义获取图书对象以及图书数量的方法。
(3)由于需要指定订单项以及获取订单的订单列表、订单号、订单总额等信息,所以需要有订单列表、订单号、订单总额指定订单项等方法。

【代码】

Book.java

package com.j2se.myInstances.example3_1;

public class Book {
    private int bno;
    private String bid;
    private String name;
    private double price;
    private int stocks;

    public Book(int bno, String bid, String name, double price, int stocks) {
        this.bno = bno;
        this.bid = bid;
        this.name = name;
        this.price = price;
        this.stocks = stocks;
    }

    public int getBno() {
        return bno;
    }

    public String getBid() {
        return bid;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public int getStocks() {
        return stocks;
    }
}

OrderItem.java

package com.j2se.myInstances.example3_1;

public class OrderItem {
    private Book book;
    private int nums;

    public OrderItem(Book book, int nums) {
        this.book = book;
        this.nums = nums;
    }

    public Book getBook() {
        return book;
    }

    public int getNums() {
        return nums;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public void setNums(int nums) {
        this.nums = nums;
    }
}

Order.java

package com.j2se.myInstances.example3_1;

public class Order {
    private String oid;
    private OrderItem[] items;
    private double total;

    public Order(String oid) {
        this.oid = oid;
        this.items = new OrderItem[3];
    }

    public String getOid() {
        return oid;
    }

    public void setOid(String oid) {
        this.oid = oid;
    }

    public OrderItem[] getItems() {
        return items;
    }

    public void setItems(OrderItem item, int idx) {
        this.items[idx] = item;
    }

    public double getTotal() {
        calcTotal();
        return total;
    }

    private void calcTotal() {
        double total = 0;
        for (int i = 0; i < items.length; ++i) {
            total += items[i].getNums() * items[i].getBook().getPrice();
        }
        this.total = total;
    }

    public void setTotal(double total) {
        this.total = total;
    }
}

BookDemo.java

package com.j2se.myInstances.example3_1;

import java.time.LocalTime;
import java.util.Scanner;

public class BookDemo {

    public static void main(String[] args) {
        Book[] books = new Book[3];  // 存储图书

        outBookInfo(books);
        // 购买图书
        Order order = purchase(books);
        // 打印订单
        outOrderInfo(order);
    }

    private static void outOrderInfo(Order order) {
        System.out.println("-------------------\t图书订单\t--------------------");
        System.out.println("订单编号:" + order.getOid());
        System.out.println("\t图书名称\t\t购买数量\t图书单价");
        OrderItem[] items = order.getItems();
        String line = null;
        for (int i = 0; i < items.length; ++i) {
            line = items[i].getBook().getName() + "\t" + items[i].getNums() + "\t" + items[i].getBook().getPrice();
            System.out.println(line);
        }
        System.out.println("订单总额:¥" + order.getTotal());
        System.out.println("------------------------------------------------");
    }

    private static Order purchase(Book[] books) {
        Scanner sc = new Scanner(System.in);
        OrderItem item = null;

        Order order = new Order("book-order-" + LocalTime.now().toString());
        for (int i = 0; i < books.length; i++) {
            System.out.print("请选择需要购买的图书编号:");
            int bno = sc.nextInt();
            System.out.print("购买数量:");
            int num = sc.nextInt();
            item = new OrderItem(books[bno - 1], num);
            order.setItems(item, i);
            System.out.println("请继续购买图书。");
        }
        sc.close();
        return order;

    }

    private static void outBookInfo(Book[] books) {
        books[0] = new Book(1, "b978-7-115-548", "Java基础案例编程", 19.9, 100);
        books[1] = new Book(2, "b328-3-124-431", "Python数据分析", 29.9, 56);
        books[2] = new Book(3, "b112-8-765-904", "机器学习实战(上)", 39.9, 220);

        System.out.println("-------------------\t图书列表\t--------------------");
        System.out.println("编号\t\tISBN\t\t\t名称\t\t\t价格\t\t库存");
        String line = null;
        for (int i = 0; i < books.length; i++) {
            line = books[i].getBno() + "\t" + books[i].getBid() + "\t" + books[i].getName() + "\t" + books[i].getPrice()
                    + "\t" + books[i].getStocks();
            System.out.println(line);
        }
        System.out.println("------------------------------------------------");
    }
}

【运行结果】

Java基础案例3-1:基于控制台的购书系统_第1张图片

你可能感兴趣的:(Java基础案例)