基于控制台的购书系统

基于控制台的购书系统



`

一、案例介绍

案例描述

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

二、案例目标

  • 学会分析“基于控制台的购书系统”程序任务实现的逻辑思路。
  • 能独立完成“基于控制台的购书系统”程序的源代码编写、编译及运行。
  • 理解和掌握面向对象的设计程序。
  • 会用类图进行面向对象设计。
  • 掌握封装的实现及好处。
  • 包和访问控制修饰符的使用。

三、案例分析

  • 通过任务描述可知,该系统中必须包括3个实体类,类名及属性设置如下:
    • 图书类(Book):
      • a)图书编号(id)
      • b)图书名称(name)
      • c)图书单价(price)
      • d)库存数量(storage)
    • 订单项类(OrderItem):
      • a)图书(book)
      • b)购买数量(num)
    • 订单类(Order):
      • a)订单号(orderId)
      • b)订单总额(total)
      • c)订单项列表(items)
  • 由于购买图书时,需要选择图书的数量,所以需要在订单项类里定义获取图书对象以及图书数量的方法。
  • 由于需要指定订单项以及获取订单的订单列表、订单号、订单总额等信息,所以需要有订单列表、订单号、订单总额指定订单项等方法。

四、总结

定义图书类Book,其代码如下所示:

package com.etime725.homework.test3.case31;

/**
 * @Author 妄念
 * Dtae 2022/7/26 21:49
 * @Version 1.0
 */
public class Book {
    private String id;      //编号
    private String name;    //书名
    private double price;   //价格
    private int storage;    //库存

    public Book() {
    }

    public Book(String id, String name, double price, int storage) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.storage = storage;
    }
    //获取书号
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    //获取书名
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    //获取价格
    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getStorage() {
        return storage;
    }

    public void setStorage(int storage) {
        this.storage = storage;
    }
}
//上述代码中,声明了图书标号id、书名name、价格price、库存storage,并提供了其getter和setter方法。

定义订单列表类OrderItem,其代码如下所示:

package com.etime725.homework.test3.case31;

/**
 * @Author 妄念
 * Dtae 2022/7/27 16:36
 * @Version 1.0
 */
public class OrderItem {
    private Book book;
    private int num;

    public OrderItem() {
    }
    //有参构造方法
    public OrderItem(Book book, int num) {
        this.book = book;
        this.num = num;
    }
    //获取图书对象
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }
    //获取图书订购数量
    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}
//上述代码中,声明了图书对象Book,购买数量num,并提供了其getter和setter方法。

定义订单类Order,其代码如下所示:

package com.etime725.homework.test3.case31;

/**
 * @Author 妄念
 * Dtae 2022/7/27 16:37
 * @Version 1.0
 */
public class Order {
    private String orderId;
    private double total;
    private OrderItem[] items;

    public Order() {
    }

    public Order(String orderId) {
        this.orderId = orderId;
        this.items = new OrderItem[3];
    }
    //获取订单号
    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }
    //获取订单总额
    public double getTotal() {
        total();
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }
    //获取订单列表
    public OrderItem[] getItems() {
        return items;
    }
    //指定一个订单项
    public void setItem(OrderItem item, int i) {
        this.items[i] = item;
    }
    //计算订单总额
    public void total(){
        double total = 0;
        for (int i = 0; i <items.length ; i++) {
            total = total + items[i].getNum() *items[i].getBook().getPrice();
        }
        this.total = total;

    }

}
上述代码中,声明了订单id、订单项数组,并声明了获取订单号方法getOrderId()、获取订单列表方法getItems()、获取订单总额方法getTotal()、指定一个订单项setItem()、计算订单总额的方法total()

测试类PayBooks ,其代码如下所示:

package com.etime725.homework.test3.case31;

import com.sun.org.apache.xpath.internal.operations.Or;

import java.util.Scanner;

/**
 * @Author 妄念
 * Dtae 2022/7/27 16:42
 * @Version 1.0
 */
public class PayBooks {
    public static void main(String[] args) {
        Book[] books = new Book[3];

        PayBooks payBooks = new PayBooks();
        payBooks.outBooks(books);
        Order buy = payBooks.buy(books);
        payBooks.outOrder(buy);
    }
    //购买图书
    public  Order buy(Book[] books){
        Order order = new Order("001");
        OrderItem item = null;
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入图书编号:");
            int cno = getScanner().nextInt();
            System.out.println("请输入购买数量:");
            int pnum = getScanner().nextInt();
            item = new OrderItem(books[cno-1],pnum);
            order.setItem(item,i);
            System.out.println("请继续购买图书!");
        }
        getScanner().close();
        return order;
    }
    //输出订单信息
    public void outOrder(Order order){
        System.out.println("\n\t图书订单");
        System.out.println("图书订单号:"+order.getOrderId());
        System.out.println("图书名称\t购买数量\t图书单价");
        System.out.println("------------------------------------------");
        OrderItem[] items = order.getItems();
        for (int i = 0; i < items.length; i++) {
            System.out.println(items[i].getBook().getName()+"\t"+items[i].getNum()+"\t"+items[i].getBook().getPrice());
        }
        System.out.println("----------------------------------------------");
        System.out.println("订单总额:\t\t"+order.getTotal());
    }
    //读取图书信息并输出
    public void outBooks(Book[] books){
        books[0] = new Book("1","java基础教程",69.9,30);
        books[1] = new Book("2","Android基础",59.9,30);
        books[2] = new Book("3","计算机网络原理",89.9,30);
        System.out.println("\t图书列表");
        System.out.println("图书编号\t图书名称\t\t图书单价\t库存数量");
        System.out.println("------------------------------------------------");
        for (int i = 0; i < books.length; i++) {
            System.out.println(i+1+"\t"+books[i].getName()+"\t"+books[i].getPrice()+"\t"+books[i].getStorage());
        }
        System.out.println("--------------------------------------------------------");
    }
    public Scanner getScanner(){
        Scanner scanner =  new Scanner(System.in);
        return scanner;
    }
}

上述代码中,输出了图书列表信息包括:图书编号、图书名称、图书单价、库存数量,用户根据图书列表信息,输入图书编号、购买数量等,系统根据用户输入的图书编号及购买数量计算总金额。

五、附录(运行截图)

在这里插入图片描述

  • 10
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
以下是一个基于控制台购书系统代码的示例,使用C++实现: ```cpp #include <iostream> #include <string> #include <vector> using namespace std; struct Book { string title; string author; double price; int quantity; }; vector<Book> books; void addBook() { Book book; cout << "Enter book title: "; getline(cin, book.title); cout << "Enter author name: "; getline(cin, book.author); cout << "Enter book price: "; cin >> book.price; cout << "Enter quantity: "; cin >> book.quantity; cin.ignore(); // ignore newline character books.push_back(book); cout << "Book added successfully!\n"; } void listBooks() { for (int i = 0; i < books.size(); i++) { cout << "Title: " << books[i].title << endl; cout << "Author: " << books[i].author << endl; cout << "Price: " << books[i].price << endl; cout << "Quantity: " << books[i].quantity << endl << endl; } } void purchaseBook() { string title; bool found = false; cout << "Enter book title to purchase: "; getline(cin, title); for (int i = 0; i < books.size(); i++) { if (books[i].title == title) { found = true; int quantity; cout << "Enter quantity to purchase: "; cin >> quantity; if (quantity > books[i].quantity) { cout << "Insufficient stock!\n"; } else { books[i].quantity -= quantity; double total = quantity * books[i].price; cout << "Purchase successful. Total cost: " << total << endl; } break; } } if (!found) { cout << "Book not found!\n"; } cin.ignore(); // ignore newline character } int main() { int choice; do { cout << "1. Add book\n"; cout << "2. List all books\n"; cout << "3. Purchase book\n"; cout << "4. Exit\n"; cout << "Enter your choice: "; cin >> choice; cin.ignore(); // ignore newline character switch (choice) { case 1: addBook(); break; case 2: listBooks(); break; case 3: purchaseBook(); break; case 4: cout << "Goodbye!\n"; break; default: cout << "Invalid choice!\n"; break; } } while (choice != 4); return 0; } ``` 这个示例中,我们定义了一个`Book`结构体来表示一本书的信息。`addBook()`函数允许用户添加一本新书,`listBooks()`函数列出库存中的所有书籍,`purchaseBook()`函数允许用户购买一本书。主函数则通过一个循环来处理用户输入的选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

妄念ol

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值