求帮忙,用java面向对象的思想写一个购书系统,输出结果如果图:
3个回答
展开全部
有时间帮你搞个
import java.util.Calendar;
import java.util.Scanner;
/*
* 功能说明,只能输入1 2 或者3如果输入其他就视为下订单或者取消订单
*/
public class ShoppingSystem {
private static final String SPACE=" ";
private int count=1;
private boolean go =true;
int[] book_p =new int[3];
int[] book_n = new int[3];
private book[] books = {
new book(1, "Java教程", 30.6, 30),
new book(2, "JSP 指南", 42.1, 40),
new book(3, "SSH 架构", 47.3, 15)
};
//图书列表
public void booklist(){
System.out.println("图书列表:");
System.out.println("图书编号 图书名称 图书单价 库存数量");
System.out.println("-----------------------------------------");
for (int i = 0; i < books.length; i++) {
System.out.println(books[i].id+SPACE+books[i].name+SPACE+books[i].moneny+SPACE+books[i].number);
}
System.out.println("-----------------------------------------");
}
//购买行为
public void buy(){
Scanner in = new Scanner(System.in);
try {
int chooses;
while(go){
System.out.println("请输入图书的编号选择图书");
chooses = in.nextInt();
if(chooses == 1 || chooses == 2 || chooses == 3){
}
else{
buylist();
go =false;
return;
}
if(chooses == 1){
book_p[0]=1;
System.out.print("请输入购买图书的数量 ");
chooses = in.nextInt();
if(chooses <= books[0].number){
book_n[0]=book_n[0]+chooses;
}
}else if(chooses == 2){
book_p[1]=1;
System.out.print("请输入购买图书的数量 ");
chooses = in.nextInt();
if(chooses <= books[1].number){
book_n[1]=book_n[1]+chooses;
}
}else if(chooses == 3){
book_p[2]=1;
System.out.print("请输入购买图书的数量 ");
chooses = in.nextInt();
if(chooses <= books[2].number){
book_n[2]=book_n[2]+chooses;
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
//订单列表
private void buylist(){
double sum=0;
System.out.println(SPACE+"图书订单:");
System.out.println("图书订单号:"+count);
System.out.println("图书名称 购买数量 图书单价");
System.out.println("-------------------------------");
for (int i = 0; i < 3; i++) {
if(book_p[i] != 0){
System.out.println(books[i].name + SPACE+book_n[i]+SPACE+books[i].moneny);
sum =sum + book_n[i]*books[i].moneny;
}
}
System.out.println("-------------------------------");
System.out.print("订单的总额 ");
System.out.println(sum);
Calendar c = Calendar.getInstance();
System.out.println("日期"+c.getTime());
}
public static void main(String[] args) {
ShoppingSystem sh = new ShoppingSystem();
sh.booklist();
sh.buy();
}
class book{
private int id;
private String name;
private double moneny;
private int number;
public book(int id, String name, double moneny, int number) {
this.id = id;
this.name = name;
this.moneny = moneny;
this.number = number;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getMoneny() {
return moneny;
}
public void setMoneny(double moneny) {
this.moneny = moneny;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
}
追问
int[] book_p =new int[3];
int[] book_n = new int[3];
定义这两个数组有什么用,没看懂!!
追答
一个表示你到底买了哪几种书,一个表示对应的书你买了几本
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询