Results 1 to 4 of 4
  1. #1
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default 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;
    		}
    	}
    
    
    }
    and my main Class
    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");
    		}
    	}
    }
    in my main class i read a file which has all the products inside the warehouse and i put them in ramList , moboList , cpuList and boxList
    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 01:14 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default 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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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 **

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

Similar Threads

  1. Access child methods from parent class
    By bmL in forum New To Java
    Replies: 2
    Last Post: 07-29-2012, 02:25 PM
  2. access methods of grandparent class
    By shruthi1 in forum New To Java
    Replies: 7
    Last Post: 10-13-2011, 10:27 PM
  3. Replies: 15
    Last Post: 09-21-2011, 01:25 AM
  4. Ask for help on Java access to protected methods
    By fangzhong in forum Advanced Java
    Replies: 3
    Last Post: 02-17-2009, 01:50 PM
  5. Replies: 1
    Last Post: 07-20-2008, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •