案例介绍:
(1)输出所有图书的信息:包括每本书的编号、书名、单价、库存。
(2)顾客购买书时,根据提示输入购买书的数量。
(3)购买完毕后输出顾客的订单信息,包括:订单号、订单明细、订单总额。
运行结果:
//Book
class Book{ //Book类,用于存放图书的基本信息
private int id; //编号
private String name; //书名
private double price; //价格
private int storage; //库存
//有参构造函数
public Book(int id,String name,double price,int storage)
{
this.id = id;
this.name = name;
this.price = price;
this.storage = storage;
}
public int getId() //获取书籍编号
{
return id;
}
public String getName() //获取书名
{
return name;
}
public double getPrice() //获取价格
{
return price;
}
public int getStorage() //获取库存
{
return storage;
}
public int decreaseStorage(int decrement) //获取实际购买数量
{
if(this.storage < decrement)
{
decrement = this.storage;
}
this.storage -= decrement;
return decrement;
}
}
//OrderItem
class OrderItem{
private Book book; //图书Book类对象
private int num; //订购数量
//有参构造函数
public OrderItem(Book book,int num)
{
this.book = book;
this.num = num;
}
public Book getBook() //获取图书对象
{
return book;
}
public int getNum() //获取订购图书数量
{
return num;
}
}
//Order
//Order类,用于存放订单信息
class Order{
private String orderID; //订单ID
private OrderItem[] items; //订单项数组
private double total; //订单总额
//构造函数
public Order(String orderID)
{
this.orderID = orderID;
this.items = new OrderItem[3];
}
public String getOrderID() //获取订单号
{
return orderID;
}
public OrderItem[] getItems() //获取订单列表
{
return items;
}
public double getTotal() //获取订单总额
{
calTotal();
return total;
}
public void setItem(OrderItem item,int i) //指定一个订单项
{
this.items[i] = item;
}
public void calTotal() //计算订单总额
{
double total = 0;
for(OrderItem item :items)
{
total += item.getNum() * item.getBook().getPrice();
}
this.total = total;
}
}
//BuyBooks:outBooks()、buyBooks()、outOrder()文章来源:https://www.toymoban.com/news/detail-720528.html
public class BuyBooks {
public static void main(String[] args)
{
Book[] books = new Book[3];
outBooks(books); //模拟从数据库中读取图书信息并输出
Order order = buyBooks(books); //顾客购买图书
outOrder(order); //输出订单信息
}
//模拟从数据库中读取图书信息并输出
public static void outBooks(Book[] books)
{
books[0] = new Book(1,"Java教程",30.6,30);
books[1] = new Book(2,"JSP教程",42.1,40);
books[2] = new Book(3,"SSH架构",47.3,15);
System.out.println("\t图书列表");
System.out.println("图书编号\t图书名称\t\t图书单价\t库存数量");
System.out.println("---------------------------------------");
for(Book book : books)
{
System.out.println(book.getId()+"\t\t"+book.getName()+"\t\t"+book.getPrice()+"\t\t"+book.getStorage());
}
System.out.println("---------------------------------------");
}
//顾客购买图书
public static Order buyBooks(Book[] books)
{
Scanner scanner = new Scanner(System.in);
Order order = new Order("00001");
OrderItem item = null;
for(int i=0;i<3;i++)
{
System.out.print("请输入购买 "+books[i].getName()+" 图书数量:");
int buyNum = scanner.nextInt();
if(buyNum<0)
{
buyNum=0;
System.out.println("购买数量输入错误,实际购买数量为 0");
}
else if (buyNum>books[i].getStorage())
{
System.out.println("购买数量超出库存,实际购买数量为 "+books[i].getStorage());
}
buyNum = books[i].decreaseStorage(buyNum);
item = new OrderItem(books[i],buyNum);
order.setItem(item,i);
if(i<2)
{
System.out.println("请继续购买图书。");
}
}
return order;
}
//输出订单信息
public static 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(OrderItem item : items)
{
System.out.println(item.getBook().getName()+"\t\t"+item.getNum()+"\t"+item.getBook().getPrice());
}
System.out.println("---------------------------------------");
System.out.println("订单总额:\t\t"+order.getTotal());
}
}
完整代码:文章来源地址https://www.toymoban.com/news/detail-720528.html
import java.util.Scanner;
class Book{ //Book类,用于存放图书的基本信息
private int id; //编号
private String name; //书名
private double price; //价格
private int storage; //库存
//有参构造函数
public Book(int id,String name,double price,int storage)
{
this.id = id;
this.name = name;
this.price = price;
this.storage = storage;
}
public int getId() { return id; } //获取书籍编号
public String getName() { return name; } //获取书名
public double getPrice() { return price; } //获取价格
public int getStorage() { return storage; } //获取库存
public int decreaseStorage(int decrement) //获取实际购买数量
{
if(this.storage < decrement)
{
decrement = this.storage;
}
this.storage -= decrement;
return decrement;
}
}
class OrderItem{
private Book book; //图书Book类对象
private int num; //订购数量
//有参构造函数
public OrderItem(Book book,int num)
{
this.book = book;
this.num = num;
}
public Book getBook() { return book; } //获取图书对象
public int getNum() { return num; } //获取订购图书数量
}
//Order类,用于存放订单信息
class Order{
private String orderID; //订单ID
private OrderItem[] items; //订单项数组
private double total; //订单总额
//构造函数
public Order(String orderID)
{
this.orderID = orderID;
this.items = new OrderItem[3];
}
public String getOrderID() { return orderID; } //获取订单号
public OrderItem[] getItems() { return items; } //获取订单列表
public double getTotal() //获取订单总额
{
calTotal();
return total;
}
public void setItem(OrderItem item,int i) //指定一个订单项
{
this.items[i] = item;
}
public void calTotal() //计算订单总额
{
double total = 0;
for(OrderItem item :items)
{
total += item.getNum() * item.getBook().getPrice();
}
this.total = total;
}
}
public class BuyBooks {
public static void main(String[] args)
{
Book[] books = new Book[3];
outBooks(books); //模拟从数据库中读取图书信息并输出
Order order = buyBooks(books); //顾客购买图书
outOrder(order); //输出订单信息
}
//模拟从数据库中读取图书信息并输出
public static void outBooks(Book[] books)
{
books[0] = new Book(1,"Java教程",30.6,30);
books[1] = new Book(2,"JSP教程",42.1,40);
books[2] = new Book(3,"SSH架构",47.3,15);
System.out.println("\t图书列表");
System.out.println("图书编号\t图书名称\t\t图书单价\t库存数量");
System.out.println("---------------------------------------");
for(Book book : books)
{
System.out.println(book.getId()+"\t\t"+book.getName()+"\t\t"+book.getPrice()+"\t\t"+book.getStorage());
}
System.out.println("---------------------------------------");
}
//顾客购买图书
public static Order buyBooks(Book[] books)
{
Scanner scanner = new Scanner(System.in);
Order order = new Order("00001");
OrderItem item = null;
for(int i=0;i<3;i++)
{
System.out.print("请输入购买 "+books[i].getName()+" 图书数量:");
int buyNum = scanner.nextInt();
if(buyNum<0)
{
buyNum=0;
System.out.println("购买数量输入错误,实际购买数量为 0");
}
else if (buyNum>books[i].getStorage())
{
System.out.println("购买数量超出库存,实际购买数量为 "+books[i].getStorage());
}
buyNum = books[i].decreaseStorage(buyNum);
item = new OrderItem(books[i],buyNum);
order.setItem(item,i);
if(i<2)
{
System.out.println("请继续购买图书。");
}
}
return order;
}
//输出订单信息
public static 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(OrderItem item : items)
{
System.out.println(item.getBook().getName()+"\t\t"+item.getNum()+"\t"+item.getBook().getPrice());
}
System.out.println("---------------------------------------");
System.out.println("订单总额:\t\t"+order.getTotal());
}
}
到了这里,关于【Java案例】基于控制台的购书系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!