java中的植入关联类

时间:2016-04-12 19:36:32

标签: java

我尝试设计超市系统,并且我使用关联类来记录不包含在客户类和项目中的额外信息,(请注意,一个客户购买了许多项目)..

客户类:

package com.company;

import java.util.Scanner;
public class Customer {

    protected long CPR ;
    protected String name ;
    protected int Tel ;
    protected String Adrs ;

    private int next ;
    protected AssociationClass A[];
    protected Item items[];

    public Customer ()
    {
        CPR = 0 ;
        name = "Non" ;
        Tel = 0 ;
        Adrs = "Non" ;
        A = new AssociationClass[5];
        items = new Item[5];
    }

    public Customer ( long c , String n , int t , String ad )
    {
        CPR = c ;
        name = n ;
        Tel = t ;
        Adrs = ad ;
        A = new AssociationClass[5];
        items = new Item[5];
    }

    public void AddItem ( Item I )
    {
        items[next]= I;
        next++;
    }

    public void CustomerPrint()
    {
        System.out.print("\n" + "CPR = " + CPR + "\n");
        System.out.print("name = " + name + "\n");
        System.out.print("Tel = " + Tel + "\n" );
        System.out.print("Adrs = " + Adrs + "\n" );
    }

}

项目类别:

package com.company;

import java.util.Scanner;
public class Item {

    protected long ID ;
    protected String name ;
    protected double price ;
    protected int quantity ;

    protected Customer customer;

    public Item ()
    {
        ID = 0 ;
        name = "Non" ;
        price = 0 ;
        quantity = 0 ;
    }

    public Item ( long id , String n , double p , int q )
    {
        ID = id ;
        name = n ;
        price = p ;
        quantity = q ;
    }

    public void SetCustomer ( Customer C )
    {
        customer = C ;
        customer.AddItem(this);
    }

    public double getPrice() {
        return price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void ItemPrint()
    {
        System.out.print("\n" + "ID = " + ID + "\n" );
        System.out.print("name = " + name + "\n");
        System.out.print("price = " + price + "\n");
        System.out.print("quantity = " + quantity + "\n" );
    }
}

关联类(在客户和项目之间):

package com.company;

import java.util.Scanner;
public class AssociationClass {

    protected String date ;
    protected String time ;
    protected double TotalPrice ;

    protected Item item[];
    protected Customer customer;

    public AssociationClass ()
    {
        date = "Non" ;
        time = "Non" ;
        TotalPrice = 0 ;
        item = new Item[5] ;
        customer = new Customer();
    }



    public void setDate(String date) {
        this.date = date;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public void BuyItems ( Item i , Customer c )
    { c.AddItem(i);
      i.SetCustomer(c); }

    public double TotalPRICE ()
    {
      double sum = 0 ;
        for ( int i=0 ; i < item.length ; i++ )
        { sum += item[i].getQuantity() + item[i].getPrice(); }

        TotalPrice = sum ;
        return TotalPrice ;
    }

    public void Aprint ()
    {
        customer.CustomerPrint();
        System.out.print("\n");
        for ( int i=0 ; i < 5 ; i++ )
        { item[i].ItemPrint();
            System.out.print("\n\n\n");}
        System.out.print("date = " + date + "\n");
        System.out.print("time = " + time + "\n");
        System.out.print("TotalPrice = " + TotalPrice + "\n");
    }


}

超市:

package com.company;
import java.util.Scanner;
public class Supermarket {


    protected AssociationClass A[];

    protected int next ;

    public Supermarket ()
    {
        A = new AssociationClass[3];
    }

    public void Buy ( Item i , Customer c )
    {
        for ( int j=0 ; j < 3 ; j++ )
        { A[j].BuyItems(i, c); }
    }

    public void print ( long id )
    {
        for ( int i = 0 ; i < 3 ; i++ )
        {
            if ( id == A[i].customer.CPR )
            {
                System.out.print("Customer Information :" + "\n");
                A[i].Aprint();
            }
        }
    }

}

主要:

package com.company;

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {

        Supermarket S;
        S = new Supermarket();

        AssociationClass A ;
        A = new AssociationClass();

        Customer C1;
        C1 = new Customer(1234567,"Jawad",36118806,"barbar");
        Customer C2;
        C2 = new Customer();

        Item T1;
        T1 = new Item(111,"product1",1.500,1);

        Item T2;
        T2 = new Item(222,"product2",0.500,5);

        Item T3;
        T3 = new Item(333,"product3",2.850,3);

        A.BuyItems(T1,C1);
        A.BuyItems(T2,C1);

        S.print(1234567);




    }
}

我的代码不起作用,不打印我给打印功能的客户CPR的信息?

1 个答案:

答案 0 :(得分:1)

我导入你的课程,当我跑步时,我得到了一个NullPointerException

您正试图在打印方法上阅读超市类中的AssociationClass但是您只启动数组,您从不将数据放入数组中。

在这种情况下,当您尝试访问空数组的属性时,它会为您提供NullPointerException

也许您可以更改BuyItems以在Supermarket AssociationClass

中添加信息