简体   繁体   中英

How can I use my object array at every method?

I'm still learning java. I'm working on object array. Hard topic for me. Because i've never use OOP programming language so this is pushing me harder. I have a problem now.

this is my main code

package deneme;
import java.util.Scanner;
public class Deneme {
static int index=0;
public static void main(String[] args) 
{           
    menu();  
}

public static void menu()
{
    int secim=0;
    Scanner menuSecim = new Scanner(System.in);
    System.out.println("Pazar Listesine Hoşgeliniz.");
    System.out.println("Menu");
    System.out.println("1. Listeyi Görüntüle");
    System.out.println("2. Listeye Ekle");
    System.out.println("3. Listeden Sil");
    System.out.println("4. Çıkış");
    System.out.print("Seçiminiz: ");
    secim = menuSecim.nextInt();
    if(secim==1)
    {

    }
    else if(secim==2)
    {//listeye ekleme
        int eklenilmekIstenen;
        Scanner sayi = new Scanner(System.in);
        System.out.print("Kaç Adet Ürün Eklemek İstiyorsunuz: ");
        eklenilmekIstenen=sayi.nextInt();
        listeyeEkle(eklenilmekIstenen);
    }

}
public static void listeyeEkle(int eklenecekSayisi)
{
    Scanner str = new Scanner(System.in);
    Scanner sayi = new Scanner(System.in);
    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();
    }

    for(int j=index;j<(index+eklenecekSayisi);j++)
    {
        System.out.print((j+1)+". Ürünün Adı: ");
        urun[j].isim = str.nextLine();
        System.out.print((j+1)+". Ürünün Miktarı: ");
        urun[j].miktar = sayi.nextFloat();
        System.out.print((j+1)+". Ürünün Fiyatı(Kg Bazında): ");
        urun[j].fiyat = sayi.nextFloat();
    }
    index=index+eklenecekSayisi;
    System.out.println("Şu an ki İndex: "+index);
    for(int j=0;j<index;j++)
    {
        System.out.println((j+1)+". Ürünün Adı: "+urun[j].isim);
        System.out.println((j+1)+". Ürünün Miktarı: "+urun[j].miktar);
        System.out.println((j+1)+". Ürünün Fiyat: "+urun[j].fiyat);
    }
    menu();
}
public static void hepsiniGor()
{
    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();
    }
    tutarHesapla();

}
public static void listedenSil()
{

}
public static void tutarHesapla()
{
    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();            
    }
    for(int i=0;i<index;i++)
    {
        urun[i].tutar=urun[i].miktar*urun[i].fiyat;
    }
}

}

this is the next object class

package deneme;
import java.util.Scanner;
public class PazarListesi 
{
Deneme giris = new Deneme();
String isim;
float miktar;
float fiyat;
float tutar;

}

I have to use this block in every method to access the object I created.

    PazarListesi[] urun = new PazarListesi[300];
    for(int i=0; i<urun.length;i++)
    {
        urun[i] = new PazarListesi();            
    }

How can I access the object without writing this block?

I couldn't understand your question exactly but following code can help you I think. You can create methods to set values of array objects and get values of array objects.

public class PazarListesi  {
    String isim;
    float miktar;
    float fiyat;
    float tutar;

    public String getPazarListesiIsim() {
        return this.isim;
    }

    public void setPazarListesiIsim(String paramString) {
        this.isim = paramString;
    }
//Add methods for other variables too
}

Based on the code you provided and what I believe I understand from your question, Try this:

public class Deneme {
    private static int index = 0;
    private static PazarListesi[] urun;

    public static void main(String[] args) {

        urun = new PazarListesi[300];

        for (int i = 0; i < urun.length; i++) {
            urun[i] = new PazarListesi();
        }

        menu();
    }

    public static void menu() {
        int secim = 0;
        Scanner menuSecim = new Scanner(System.in);

        System.out.println("Pazar Listesine Hoşgeliniz.");
        System.out.println("Menu");
        System.out.println("1. Listeyi Görüntüle");
        System.out.println("2. Listeye Ekle");
        System.out.println("3. Listeden Sil");
        System.out.println("4. Çıkış");
        System.out.print("Seçiminiz: ");
        secim = menuSecim.nextInt();

        if (secim == 1) {
            //
        } else if (secim == 2) {//listeye ekleme
            int eklenilmekIstenen;
            Scanner sayi = new Scanner(System.in);
            System.out.print("Kaç Adet Ürün Eklemek İstiyorsunuz: ");
            eklenilmekIstenen = sayi.nextInt();
            listeyeEkle(eklenilmekIstenen);
        }
    }

    public static void listeyeEkle(int eklenecekSayisi) {
        Scanner str = new Scanner(System.in);
        Scanner sayi = new Scanner(System.in);

        for (int j = index; j < (index + eklenecekSayisi); j++) {
            System.out.print((j + 1) + ". Ürünün Adı: ");
            urun[j].isim = str.nextLine();
            System.out.print((j + 1) + ". Ürünün Miktarı: ");
            urun[j].miktar = sayi.nextFloat();
            System.out.print((j + 1) + ". Ürünün Fiyatı(Kg Bazında): ");
            urun[j].fiyat = sayi.nextFloat();
        }

        index = index + eklenecekSayisi;
        System.out.println("Şu an ki İndex: " + index);

        for (int j = 0; j < index; j++) {
            System.out.println((j + 1) + ". Ürünün Adı: " + urun[j].isim);
            System.out.println((j + 1) + ". Ürünün Miktarı: " + urun[j].miktar);
            System.out.println((j + 1) + ". Ürünün Fiyat: " + urun[j].fiyat);
        }

        menu();
    }

    public static void hepsiniGor() {
        tutarHesapla();
    }

    public static void listedenSil() {
        //
    }

    public static void tutarHesapla() {
        for (int i = 0; i < index; i++) {
            urun[i].tutar = urun[i].miktar * urun[i].fiyat;
        }
    }
}

It extracts initialization of the model class into main , so you don't need to repeat it in every method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM