面向对象练习代码

package com.foreknow.ordercar;

public class Book {
    private int pid;
    private String pname;
    private double pprice;
    private int pstorage;
    
    public Book(int pid,String pname,double pprice,int pstorage){
        this.pid = pid;
        this.pname = pname;
        this.pprice = pprice;
        this.pstorage = pstorage;
    }
    
    public int getPid() {
        return pid;
    }
    public void setPid(int pid) {
        this.pid = pid;
    }
    public String getPname() {
        return pname;
    }
    public void setPname(String pname) {
        this.pname = pname;
    }
    public double getPprice() {
        return pprice;
    }
    public void setPprice(double pprice) {
        this.pprice = pprice;
    }
    public int getPstorage() {
        return pstorage;
    }
    public void setPstorage(int pstorage) {
        this.pstorage = pstorage;
    }
    
    
}

package com.foreknow.ordercar;

public class Order {
    private String orderid;
    private double total;
    private OrderItem[] Items;
    
    public Order(String orderid){
        Items = new OrderItem[3];
        this.orderid = orderid;
    }
    //计算总额
    public void summoney() {
        //遍历数组Items
        double sum = 0;
        for (int i = 0; i < Items.length; i++) {
            sum += (Items[i].getInum())*(Items[i].getIprice());
        }
        this.total = sum;
    }
    
    public String getOrderid() {
        return orderid;
    }
    public void setOrderid(String orderid) {
        this.orderid = orderid;
    }
    public double getTotal() {
        summoney();
        return total;
    }
    public void setTotal(double total) {
        this.total = total;
    }
    public OrderItem[] getItems() {
        return Items;
    }
    public void setItems(OrderItem items,int i) {
        Items[i] = items;
    }
    
}

package com.foreknow.ordercar;

public class OrderItem {
    private String iname;
    private int inum;
    private double iprice;
    
    public OrderItem(String iname,int inum,double iprice){
        this.iname = iname;
        this.inum = inum;
        this.iprice = iprice;
    }
    
    public String getIname() {
        return iname;
    }
    public void setIname(String iname) {
        this.iname = iname;
    }
    public int getInum() {
        return inum;
    }
    public void setInum(int inum) {
        this.inum = inum;
    }
    public double getIprice() {
        return iprice;
    }
    public void setIprice(double iprice) {
        this.iprice = iprice;
    }
    
}

package com.foreknow.ordercar;

import java.util.Scanner;

public class Test {
    
    public void outBooks(Book[] book) {

        System.out.println("\t\t        图书列表");
        System.out.println("图书编号"+"\t"+"图书名称"+"\t    "+"图书单价"+"\t"+"图书库存");
        System.out.println("----------------------------------------------------");
        for (int i = 0; i < book.length; i++) {
            System.out.println(book[i].getPid()+"\t\t"+book[i].getPname()+"\t\t"+book[i].getPprice()+"\t\t    "+book[i].getPstorage()+"\t");
        }
        System.out.println("----------------------------------------------------");
        
    }
    public void buyBook(Book[] book) {
        Scanner input = new Scanner(System.in);
        Order order = new Order("00001");
        for (int i = 0; i < book.length; i++) {
            System.out.println("请输入图书编号选择图书:");
            int bno = input.nextInt();
            System.out.println("请输入购买图书数量:");
            int bnum = input.nextInt();
            OrderItem orderItem = new OrderItem(book[bno-1].getPname(), bnum, book[bno-1].getPprice());
            order.setItems(orderItem,i);
            //System.out.println(orderItem[i].getIname()+"\t"+orderItem[i].getIname()+"\t"+orderItem[i].getIname()+"\t");
        }
    }
    
    public static void main(String[] args) {
        //输出图书的信息
        
        //创建一个Book类型的数组
        //创建三个Book对象并将这些对象保存到数组中
        //输出Book对象的信息
        Book[] books = new Book[3];
        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);
        Test test = new Test();
        test.outBooks(books);
        test.buyBook(books);
        
    }
}

你可能感兴趣的:(面向对象练习代码)