-2
\$\begingroup\$

I'm a new Java learner and I'm trying to build my portfolio and already finished my program. But I want to reduce my code lines as much as possible and possibly classes through for loop. In my program I wasted a lot of line by manually creating object in which I'm figuring out if for loop is possible to create new different objects.

This is part of code which will be implemented by another class

public interface BlockInterface {
        int blockNum();
        LotInterface[] lot();
    }

    public interface LotInterface {
        int size();
        int price();
        String setStatus(int choice);
    }
}   
public class Status {
  private String status = "Available";
    
    public String buying() {
        return "Available";
    }

    public String Setstatus(String status) {
        this.status = status;
        return status;
    }

    public String Getstatus() {
        return status;
    }
}
public class Sold extends Status{
        @Override
        public String buying () {
            return "Sold";
    }
}
public class Reserved extends Status{
        @Override
        public String buying () {
            return "Reserved";
        }
}

This is the area of code that I want to reduce. Is it possible to not manually declare these object so if ever I set a huge array list like 200, it will save a lot of code line

public class Block implements BlockInterface{

    @Override
    public int blockNum() {
        return 1;
    }

    @Override
    public LotInterface[] lot() {
        LotInterface[] lots = new LotInterface[3]; //The array size is only 3, but if set it to like 200 or more, then it will eat alot of code line, so I'm trying to figure out if it is possible to use loop so I can save code lines
             
        LotInterface lot1;
        lot1 = new LotNum1();
        lots[0] = lot1;
        
        LotInterface lot2;
        lot2 = new LotNum2();
        lots[1] = lot2;
        
        LotInterface lot3;
        lot3 = new LotNum3();
        lots[2] = lot3;
        }
 }

I'm also having a problem on classes, because if I set a total of 200 lot or more, then I will create a one-by-one 200+ java classes for it. Is there anyway I can avoid it by also using loop? And as the lot goes higher, the size and price also goes higher and check Status like in my code below.

public class LotNum1 implements LotInterface{

    @Override
    public int size() {
       return 100;
    }

    @Override
    public int price() {
        return 500;
    }

    @Override
    public String setStatus(int choice) {
        Status stats = new Status(); 
        Reserved res = new Reserved(); 
        Sold sol = new Sold();    
        if(choice == 0)
            stats.setDefStatus(stats.buying());
        else if(choice == 1)
            stats.setDefStatus(res.buying());
        if(choice == 2)
            stats.setDefStatus(sol.buying());
        
        return status.getDefStatus();
    }

}
public class LotNum2 implements LotInterface{

    @Override
    public int size() {
       return 200;
    }

    @Override
    public int price() {
        return 1000;
    }

    @Override
    public String setStatus(int choice) {
        Status stats = new Status(); 
        Reserved res = new Reserved(); 
        Sold sol = new Sold();    
        if(choice == 0)
            stats.setDefStatus(stats.buying());
        else if(choice == 1)
            stats.setDefStatus(res.buying());
        if(choice == 2)
            stats.setDefStatus(sol.buying());
        
        return status.getDefStatus();
    }

}
public class LotNum3 implements LotInterface{

    @Override
    public int size() {
       return 300;
    }

    @Override
    public int price() {
        return 1500;
    }

    @Override
    public String setStatus(int choice) {
        Status stats = new Status(); 
        Reserved res = new Reserved(); 
        Sold sol = new Sold();    
        if(choice == 0)
            stats.setDefStatus(stats.buying());
        else if(choice == 1)
            stats.setDefStatus(res.buying());
        if(choice == 2)
            stats.setDefStatus(sol.buying());
        
        return status.getDefStatus();
    }

}
\$\endgroup\$
3
  • \$\begingroup\$ It works perfectly. But I find it a bit redudant because I manually declare object and add new classes, so if ever there are 500 lots, then I might waste a lot of lines. Here is the full code of my work: drive.google.com/drive/folders/… Really appreciate in advance if someone could help me because it will give me a lot of knowledge :) \$\endgroup\$
    – Anonymous
    Aug 4 at 1:49
  • 2
    \$\begingroup\$ Welcome to Code Review. You haven't really described what task this code is designed to accomplish, so it's hard for us to advise you properly. How is the code used? Where do those lot size and price numbers come from? Under what conditions would you call .setStatus() on a lot? Please read How to Ask and edit the question to add more detail. \$\endgroup\$ Aug 4 at 5:28
  • 1
    \$\begingroup\$ As far as the example goes, the differences between the implementations of the Lot interface are only in the values of size and price, which doesn't look like a strong reason for different implementation classes. Much more context is needed before we can give you any decent feedback. \$\endgroup\$ Aug 4 at 9:51

0

Browse other questions tagged or ask your own question.