Results 1 to 4 of 4
Thread: Access to Class Methods
- 10-23-2013, 01:50 AM #1
Senior Member
- Join Date
- Mar 2013
- Location
- Greece
- Posts
- 183
- Rep Power
- 7
Access to Class Methods
I have to deal with java much time so i am trying to remember some basic staff
I have one Big Class called Item and 4 other class (CPU , Mobo , Ram , Box) which extends the Item class then in my main program i create an ArrayList<ArrayList<Item>> and inside the ArrayList i have add 4 arraylist (ramList , moboList , cpuList , boxList) now i want to check for example if an object inside ramList is equals with an other object in ramList so i create a method in Ram class (which extends Item) which check if this object is equals with onother but I am trying to make a small example to see if it's works i have an error .. check my code below :
Item File
Java Code:import java.util.Scanner; public class Item { int productID; String companyName; String manufactureName; String date; String model; int cost; public int getPrice(){ return cost; } } class CPU extends Item{ int pins; int [] availablePins = {1150,1155,1556}; Scanner input = new Scanner(System.in); public CPU(String model,int pins , int cost){ this.cost = cost; this.model = model; boolean pinsChoose = false; while(!pinsChoose){ for(int i = 0 ; i < availablePins.length ; i++){ if(availablePins[i] == pins){ pinsChoose = true; } } if(!pinsChoose){ System.out.print("Wrong pins for the CPU.You can choose 1150 , 1555 , 1556 try again :"); pins = input.nextInt(); } } this.pins = pins; } void printInfo() { System.out.println(model + " " + pins ); } boolean checkEquals(CPU other) { if(this.model.equalsIgnoreCase(other.model) && this.pins == other.pins){ return true; } else{ return false; } } } class Mobo extends Item{ public Mobo(String model , int cost){ this.model = model; this.cost = cost; } void printInfo() { System.out.println(model); } boolean checkEquals(Mobo object) { if(this.model.equalsIgnoreCase(object.model)){ return true; } else{ return false; } } } class Ram extends Item{ String size ; Scanner input = new Scanner(System.in); String [] availableSize = {"2Gb","4GB"}; String [] availableModel = {"DDR2" , "DDR3"}; boolean wrongRamSize = true; boolean wrongModel = true; public Ram(String model , String size , int cost){ this.cost = cost; while(wrongModel){ for(int i = 0 ; i < availableModel.length ; i ++){ if(availableModel[i].equalsIgnoreCase(model)){ wrongModel = false; } } if(wrongModel){ System.out.print("Wrong model of the Ram. Available models are DDR2 and DDR3 try again :"); this.model = input.next(); } } this.model = model; while(wrongRamSize){ for(int i = 0 ; i < availableSize.length ; i ++){ if(availableSize[i].equalsIgnoreCase(size)){ wrongRamSize = false; } } if(wrongRamSize){ System.out.print("Wrong size of the Ram. Available size are 2Gb and 4Gb try again :"); this.size = input.next(); } } this.size = size; } public void printInfo(){ System.out.println(model + " " + size); } boolean checkEquals(Ram other) { if(this.model.equalsIgnoreCase(other.model) && this.size.equals(other.size)){ return true; } else{ return false; } } } class Box extends Item{ String [] availableModel = {"mini","med","max"}; Boolean wrongModel = true; Scanner input = new Scanner(System.in); public Box(String model , int cost){ this.cost = cost; while(wrongModel){ for(int i = 0 ; i < availableModel.length ; i ++){ if(availableModel[i].equalsIgnoreCase(model)){ wrongModel = false; } } if(wrongModel){ System.out.print("Wrong Size of the Box. Available Sizes are mini , med and max try again :"); this.model = input.next(); } } this.model = model; } void printInfo() { System.out.println(model); } boolean checkEquals(Box other) { if(this.model.equalsIgnoreCase(other.model)){ return true; } else{ return false; } } }
Java Code:public class WareHouse { ArrayList<Item> ramList = new ArrayList<>(); ArrayList<Item> cpuList = new ArrayList<>(); ArrayList<Item> boxList = new ArrayList<>(); ArrayList<Item> moboList = new ArrayList<>(); ArrayList<ArrayList<Item>> products = new ArrayList<>(); OrderProducts orderManager ; SellProducts purchaseManager ; public WareHouse(){ products.add(ramList); products.add(cpuList); products.add(moboList); products.add(boxList); Scanner inputReader = null; try{ inputReader =new Scanner(new FileInputStream("C:\\users\\ShadowWalker\\Desktop\\stocks.txt")); }catch(FileNotFoundException e){ e.printStackTrace(); } while(inputReader.hasNext()){ String line = inputReader.nextLine(); String [] tokens = line.split(" "); Item element ; if(tokens[0].equalsIgnoreCase("CPU")){ element = new CPU(tokens[1],Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3])); products.get(1).add(element); }else if(tokens[0].equalsIgnoreCase("Ram")){ element = new Ram(tokens[1],tokens[2], Integer.parseInt(tokens[3])); products.get(0).add(element); }else if(tokens[0].equalsIgnoreCase("Mobo")){ element = new Mobo(tokens[1],Integer.parseInt(tokens[2])); products.get(2).add(element); }else{ element = new Box(tokens[1],Integer.parseInt(tokens[2])); products.get(3).add(element); } } inputReader.close(); } public static void main(String args[]){ WareHouse element = new WareHouse(); if(element.products.get(0).get(0).checkEquals( element.products.get(0).get(2))){ System.out.println("yes"); } } }
and i making a small test to see if one object in ramList is equals with onother one inside the ram list.. but i get error because i didn't have the checkEquals in Item class ... but i don't understand why is that a problem.. cause element.products.get(0) is an ArrayList which holds Ram Objects ...
i can change line 46 with this one : if(((Ram) element.products.get(0).get(0)).checkEquals( (Ram) element.products.get(0).get(2))){
but i don't like this way.. i want to know why i have wrong with the code above..Last edited by ShadowWalker; 10-23-2013 at 02:14 AM.
- 10-23-2013, 05:06 AM #2
Re: Access to Class Methods
When you get an error copy and paste the full and exact message here.
BTW, storing all your Lists in another List is not a good idea. It just makes the code hard to read and maintain.
- 10-23-2013, 11:31 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Re: Access to Class Methods
Also, the usual way of checking for equals is to override the base equals() method found in the Object class, not to write a brand new one.
Please do not ask for code as refusal often offends.
** This space for rent **
- 10-23-2013, 03:15 PM #4
Senior Member
- Join Date
- Mar 2013
- Location
- Greece
- Posts
- 183
- Rep Power
- 7
Similar Threads
-
Access child methods from parent class
By bmL in forum New To JavaReplies: 2Last Post: 07-29-2012, 03:25 PM -
access methods of grandparent class
By shruthi1 in forum New To JavaReplies: 7Last Post: 10-13-2011, 11:27 PM -
using string representation of component to access its methods
By d3n1s in forum Advanced JavaReplies: 15Last Post: 09-21-2011, 02:25 AM -
Ask for help on Java access to protected methods
By fangzhong in forum Advanced JavaReplies: 3Last Post: 02-17-2009, 02:50 PM -
[SOLVED] Unable to access array defiened in constructor in other methods.
By Shyam Singh in forum New To JavaReplies: 1Last Post: 07-20-2008, 05:42 PM
Bookmarks