Java实现基于控制台的购书系统基本操作
Java开发基于控制台的购书系统
题目
开发基于控制台的购书系统。具体要求如下:
(1)输出所有图书的信息:包括每本图书的图书编号、图书名称、图书单价和库存数量。
(2)顾客购买图书:根据提示输入图书编号来购买图书,并根据提示输入购买图书数量(用户必须连续购书三次)。
(3)购书完毕后输出顾客的订单信息:包括订单号、订单明细、订单总额和订单日期。
系统运行界面如图-1所示。
代码
Book.java
public class Book {
// 图书编号
private String number;
// 图书名称
private String name;
// 图书单价
private int price;
// 库存数量
private int stockNumber;
public Book() {
super();
}
public Book(String number, String name, int price, int stockNumber) {
super();
this.number = number;
this.name = name;
this.price = price;
this.stockNumber = stockNumber;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getStockNumber() {
return stockNumber;
}
public void setStockNumber(int stockNumber) {
this.stockNumber = stockNumber;
}
@Override
public String toString() {
return "编号:" + number + " 书名:" + name + " 价格:" + price + " 库存量:" + stockNumber;
}
}
Order.java
public class Order {
// 订单号
private long OrderNumber;
// 订单细节:书本编号、书本名字、单价
private String OrderInfo;
// 订单总额
private int OrderSum;
// 订单日期
private String OrderData;
public Order() {
super();
}
public Order(long orderNumber, String orderInfo, int orderSum, String orderData) {
super();
OrderNumber = orderNumber;
OrderInfo = orderInfo;
OrderSum = orderSum;
OrderData = orderData;
}
public long getOrderNumber() {
return OrderNumber;
}
public void setOrderNumber(long orderNumber) {
OrderNumber = orderNumber;
}
public String getOrderInfo() {
return OrderInfo;
}
public void setOrderInfo(String orderInfo) {
OrderInfo = orderInfo;
}
public int getOrderSum() {
return OrderSum;
}
public void setOrderSum(int orderSum) {
OrderSum = orderSum;
}
public String getOrderData() {
return OrderData;
}
public void setOrderData(String orderData) {
OrderData = orderData;
}
@Override
public String toString() {
return "个人订单" + " " + "订单编号:" + OrderNumber + " " + OrderInfo + " " + "图书总价:" + OrderSum
+ " " + "创建时间:" + OrderData;
}
PurchaseBookSystem.java
public class PurchaseBookSystem{
ArrayList<Book> list = new ArrayList<Book>();
//存放书本信息
Book[] arrBook = new Book[20];
//存放订单信息
Order[] arrOrder = new Order[20];
public static void main(String[] args) {
PurchaseBookSystem pbs = new PurchaseBookSystem();
//存入图书信息
pbs.InputBookInfo();
//购书系统主页面
pbs.mainPage();
}
/**
* 购书系统主页面
*/
public void mainPage(){
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 100; i++){
System.out.println("*****************************");
System.out.println("\t通理工购书系统");
System.out.println("\t1.图书信息");
System.out.println("\t2.购买图书");
System.out.println("\t3.订单信息");
System.out.println("\t4.退出系统");
System.out.print("\t请选择你需要的业务:");
int number = sc.nextInt();
switch (number) {
case 1:
//输出图书信息
bookInfo();
break;
case 2:
//购买图书
bookBought();
break;
case 3:
lookOrder();
break;
case 4:
System.out.println("退出成功!");
return;
default:
System.out.println("请输入正确的数字!");
break;
}
}
}
/**
* 存入图书信息
*/
public void InputBookInfo(){
Book book1 = new Book("0001","红楼梦",298,999);
Book book2 = new Book("0002","西游记",49,999);
Book book3 = new Book("0003","三国演义",98,999);
Book book4 = new Book("0004","水浒传",128,999);
Book book5 = new Book("0005","鲁滨逊漂流记",68,999);
arrBook[0] = book1;
arrBook[1] = book2;
arrBook[2] = book3;
arrBook[3] = book4;
arrBook[4] = book5;
for (Book book : arrBook) {
list.add(book);
}
}
/**
* 输出图书信息
*/
public void bookInfo(){
for(int i = 0; i < list.size(); i++){
if(list.get(i) != null){
System.out.println(list.get(i));
}
}
}
/**
* 购买图书
*/
public void bookBought(){
int k = 0;
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 3; i++){
System.out.print("请输入您需要购买的图书编号:");
String number = sc.next();
//此处判断是否存在图书编号有问题,输入0002的话,循环if判断不存在,会走到else打印输出错误,走第二次才能查找到,会多打印一次输入错误
// for(int z = 0; z < list.size(); z++){
// if(number.equals(arrBook[z].getNumber())){
// System.out.println(“输入正确”);
// }else{
// System.out.println(“输入错误”);
// return;
// }
// }
for(int j = 0; j < list.size(); j++){
if(number.equals(arrBook[j].getNumber())){
System.out.println(arrBook[j]);
System.out.print(“请输入您需要购买的数量:”);
int boughtNumber = sc.nextInt();
//当库存数量大于需要购买数量的时候可以购买
if(arrBook[j].getStockNumber() > boughtNumber && arrBook[j].getStockNumber() > 0){
//修改库存
arrBook[j].setStockNumber(arrBook[j].getStockNumber() - boughtNumber);
//生成订单
createOrder(k,arrBook[j],boughtNumber);
k++;
break;
}else{
System.out.println(“超过可购买数量!请重新购买!”);
break;
}
}
}
}
}
/**
* 生成订单
*/
public void createOrder(int i,Book arr,int boughtNumber){
long orderNumber = i + 10001;
int OrderSum = arr.getPrice() * boughtNumber;
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String OrderData = date.format(new Date());
String OrderInfo = "图书编号:" + arr.getNumber() + " "+ "图书名:" + arr.getName() + " " + "图书价格:" + arr.getPrice();
Order order = new Order(orderNumber,OrderInfo,OrderSum,OrderData);
arrOrder[i] = order;
System.out.println("订单已提交!");
System.out.println("订单号:" + orderNumber + " " + OrderInfo + " " + "订单金额:" + OrderSum + " " + "订单生成时间:" + OrderData);
}
/**
* 查看订单
*/
public void lookOrder(){
for(int i = 0; i < arrOrder.length; i++){
if(arrOrder[i] != null){
System.out.println(arrOrder[i]);
}
}
}
}
实现效果
本文地址:https://blog.csdn.net/weixin_45231452/article/details/108030653