Results 1 to 4 of 4
  1. #1
    luckylucky is offline Member
    Join Date
    Mar 2015
    Posts
    1
    Rep Power
    0

    Default Looks like I知 doing procedural programming

    Any tips to make it more OOP?
    It痴 my first Java program to calculate the total cost of elixir during an attack in the game Clan of Clash.

    Java Code:
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    
    public class ClashOfClan {
    
    	private static String[] units = {"Barbarians ", "Archers ", "Giants ", "Goblins ", "Wall Breakers ", "Balloons ", "Wizards ", "Healers ", "Dragons ", "Pekka "};
    	private static int[] elixirCost = {100, 200, 2250, 100, 3000, 4000, 3500, 8000, 30000, 35000};
    	static int counter = 0;
    	static String zero = "0";
    	static int value = 0;
    
    	public static void main(String[] args) {
    
    		System.out.println("How many.... ");	
    		finalValue();
    		System.out.println("Total elixir used: " + value);
    
    	}
    
    	private static void finalValue() {
    		for (int i = 0; i < units.length; i++) {
    			value += totalCost();
    		}
    	}
    
    	private static int convertInput() {
    		String unit = checkUnit();
    		String input = getInput(unit);
    		if (input.isEmpty()) {
    			input = zero;
    		}
    		int convertInput = Integer.parseInt(input);
    		return convertInput;
    		
    	}
    
    	private static int totalCost() {
    		int addingCost = convertInput();
    		int totalCost = addingCost * elixirCost[counter];
    		counter++;
    		return totalCost;
    	}
    	
    	private static String checkUnit() {
    		String unit = units[counter];
    		return unit;
    	}
    	
    	private static String getInput(String prompt) {
    		BufferedReader stdin = new BufferedReader(
    				new InputStreamReader(System.in));
    
    		System.out.print(prompt);
    		System.out.flush();
    		
    		try {
    			return stdin.readLine();
    		} catch (Exception e) {
    			return "Error: " + e.getMessage();
    		}
    	}
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Looks like I知 doing procedural programming

    Get rid of your static methods and variables for one. If units and elixarCosts are related, why not combine them in a class? And it seems like some of your methods are gratuitous. For example, you only call checkUnit() and getInput() at one location. So why use methods?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Juan2114 is offline Member
    Join Date
    Feb 2015
    Posts
    5
    Rep Power
    0

    Default Re: Looks like I知 doing procedural programming

    Is quite a bare bones class, so not much to do, maybe an Unit class that holds (name, price), as i said not much to look at so yea.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Looks like I知 doing procedural programming

    Quote Originally Posted by luckylucky View Post
    Looks like I知 doing procedural programming
    Also looks like you're cross posting.

    first program in Java, how to make it less procedural and more OOP (Beginning Java forum at JavaRanch)

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Java programming or Android programming
    By sieciowiec in forum Android
    Replies: 1
    Last Post: 11-11-2014, 07:11 PM
  2. Replies: 3
    Last Post: 05-24-2014, 08:26 PM
  3. android programming vs game programming using java
    By vgoel38 in forum Forum Lobby
    Replies: 2
    Last Post: 09-08-2012, 12:11 AM
  4. Replies: 2
    Last Post: 12-05-2011, 03: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
  •