1

I am working on a project to help get me back into Java coding and it is a text based game (I know, not much, but I have to start somewhere). Anyway, I have come across a problem. I need to be able to put names of parts (it is a hardware tycoon game) and prices along with them into an array. For example, a desktop computer has parts such as a CPU, and the game would list your CPU choices. I need a way of storing this data, and it's pretty complicated to me because I need to not only store all of the names of CPUs for the player's benefit, but also store the prices alongside the names. On top of that, I have multiple product types such as desktops, laptops, and consoles, which each pretty much have different part names and prices. I thought of a 3 dimensional array to store the product types such as desktop (in columns), the part names (in rows), and the prices (behind the rows, if that makes sense in a 3 dimensional way. But I do not know how to initialize such an array and how to set the values on initialization.

Also, I thought of creating classes for each product type and putting arrays in each class to define parts and prices (2d arrays), but it is still complex and I would like to know how to sort this data and potentially make a system where certain parts are unlocked as game time progresses. Thank you in advance.

Arwrock
  • 13
  • 3
  • Model your problem domain (products, parts, etc) as [`class`es](https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html); that's what OO programming is about. After you've done that, probably a one-dimensional `List` is all you need for storing the data. – Mick Mnemonic Aug 07 '15 at 16:13

4 Answers4

1

I might take you in a different direction. Why don't you create classes and objects? Then create instances of those objects? Java is an object oriented language. Creating objects would allow you to hold all of the values you need. Quick example,

Public abstract class CPU {

    // Declare fields
    private float price;
    private String name;

    // Declare constructors 
}

Public class intelCPU extends CPU {

    // GetPrice
    public int getPrice() {
        return price;
    }

    // Set Name
    public void setName(n) {
        name = n;
    }

}
Regis
  • 166
  • 4
  • 17
  • Thanks for the answer, but I was going for having a list of parts that are from different decades and this list would be a little extensive. How would I combine that list into these classes? So for example I could create a new object from the class Desktop and then specify (numerically) the part within another class. Sorry for the confusion. – Arwrock Aug 07 '15 at 19:05
  • I don't think it would be as extensive as you think. You would need to create a class with all of the features you need and then build an object for every different kind of computer part you wanted. For instance you could create a CPU object with 2.4Ghs and another CPU object with 1.7Ghs. Both would be created from the same class. The objects would just be given different variable amounts. Thus if you wanted 10 different kinds of CPU's you would could create 10 CPU objects all from the same class. The parts list you mentioned below can all be set up within the class. – Regis Aug 07 '15 at 21:02
0

How about using a Map instead. A Map let's you store information in Key/Value pairs. You can make a Map that has a key type of String (that represents the product type) and a value type of Map (that represents individual products) that has a String key (product name) and a value that stores the price (perhaps a double) or the info (as a String or something). That way you can do something like outerMap.get("laptops").get("laptop1") and that would return the information of laptop1. The first get, gets the Map that contains all of the laptop products (or whatever product type ypu would want). The second returns the information for the specific product in that category.

You can implement this like this.

Map<String, Map<String, String>> productMap = new HashMap<>();

Using this you would place a product type like this:

productMap.put("laptops", new HashMap<String,String>());

And then add a product like this:

productMap.get("laptops").put("laptop1","This is information about laptop 1");

PS: In case you aren't aware, the reason I used = new HashMap instead of = new Map is because Map is an interface not a class. A HashMap is a class that implements the Map interface.

cbender
  • 2,231
  • 1
  • 14
  • 18
0

More than multidimensional arrays, you could tag this with Object Oriented Programming (OOP). From your description it looks like you would need a class hierarchy, something like:

abstract class Product

class Desktop extends Product
class Laptop extends Product
class Console extends Product

Put all common fields/methods that can be used by Desktop, Laptop, Console etc into your Product class.

//Just an example
abstract class Product {
   String name;
   ArrayList<Component> components;
   public String getName(){
      return name;
   }
}

Since each product has several components the product needs to have a list of components as shown above.

abstract class Component{
   Double price;
   public Double getPrice(){
      return price;
   }
}

Now you can have components like CPU, Power supply etc, they have some common behavior and fields like price that is put into Component class. Any specialized behavior / fields for each component can be put into the corresponding class like clock frequency shown below:

class CPU extends Component {
  Double clockFreq;
}

So if your list of parts is 3 items long it could be written to a text file like so:

name,type,price
intelCPU6600,CPU,200
amdCPU7789,CPU,150
PS1Power,PSU,120
newPart,unknown,500

This list could be 100's of items without any problem. Read it into your program using Scanner & for each line do something like:

String line = scanner.nextLine();
String[] fields = line.split(",");
if("CPU".equals(fields[1]){
 CPU cpu = new CPU(fields[0],fields[1],fields[2]);
 //Product is an object of the class Product that you should have created earlier
 product.components.add(cpu);
} else if("PSU".equals(fields[1]) {
 PSU psu = new PSU(fields[0],fields[1],fields[2]);
 product.components.add(psu);
} //..so on

if there is a generic product that you don't have a class for that's where the abstract class can be used:

if("unknown".equals(fields[1]){
 Component comp = new Component(fields[0],fields[1],fields[2]);
 product.components.add(comp);
} 
Sid
  • 465
  • 6
  • 14
  • This was what I was thinking of but as I mentioned to @Regis I need to define a parts list which was why I thought of arrays and pre setting them. I don't think it's possible to describe a parts list with a class and allow a choice when I create a new object of that class. – Arwrock Aug 07 '15 at 19:09
  • Your parts list should be analogus to the Component list in the example above. If you need to pre set them its easy to read a file for a part name & type along with other attributes and create objects in a loop. I did not understand what you mean by "allow a choice when I create a new object of the class".. – Sid Aug 07 '15 at 19:14
  • Basically I would create a class with a subclass that contains all of the parts, and then I could create an object of that class and choose which part (specify the subclass) it is comprised of. – Arwrock Aug 07 '15 at 19:28
  • I have updated my answer, please check if it adds any clarity to your problem. – Sid Aug 07 '15 at 19:31
  • While I appreciate the update, I need to predefine dozens of parts before the game starts, and then retrieve those part names and allow the player to choose. After they choose, it will create an object based on the class of product they want, with those parts they chose. – Arwrock Aug 07 '15 at 19:42
  • Ah I see, you are choosing parts first then the product & you would like to add the chosen parts to the product ? What I did not understand is how choosing Desktop compatible parts would work in a Console (this is all upto your game logic though and does not affect how the design classes would be) Since you need to define dozens of part names you will need to put them in some sort of text file, I don't know of a way around that..it has to be defined somewhere. – Sid Aug 07 '15 at 19:51
0

I agree with @Regis that you should be using a custom object to describe your products, and then storing them in a some kind of data structure.

However, you can solve the problem you described by declaring a multi-dimensional array of the type "Object," which will allow you to put basically anything into it, even primitives (autoboxing and autounboxing was added in Java 5 and will seamlessly translate between primitive types like int and the Java wrapper class for that type, like java.lang.Integer).

Of course such an array would be very weakly typed and there would be nothing stopping you from adding doubles to the product name column, or vice versa. You'd also have to do a lot of casting, which is a code smell.

Bobby StJacques
  • 724
  • 4
  • 9
  • I was thinking of the fact that I could add values to the product name column myself. I thought I could just leave them null. – Arwrock Aug 07 '15 at 19:06