gpt4 book ai didi

java - java中的植入关联类

转载 作者:行者123 更新时间:2023-12-01 10:04:06 25 4
gpt4 key购买 nike

我尝试设计超市系统,我使用关联类来记录客户类别和商品中未包含的额外信息,(注意一个客户购买许多商品)..

客户类别:

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 信息吗?

最佳答案

我导入你的类,当我运行时,我得到一个 NullPointerException

您正在尝试通过 print 方法读取 Supermarket 类中的 AssociationClass,但您只启动了数组,而从未将数据放入数组中。

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

也许您可以更改您的 BuyItems 以在您的 Supermarket AssociationClass 中添加信息

关于java - java中的植入关联类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36582522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com