Results 1 to 4 of 4
- 03-01-2015, 02:42 AM #1
Member
- Join Date
- Mar 2015
- Posts
- 1
- Rep Power
- 0
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(); } } }
- 03-01-2015, 03:59 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 03-01-2015, 04:54 AM #3
Member
- Join Date
- Feb 2015
- Posts
- 5
- Rep Power
- 0
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.
- 03-01-2015, 05:13 AM #4
Re: 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)
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Java programming or Android programming
By sieciowiec in forum AndroidReplies: 1Last Post: 11-11-2014, 08:11 PM -
Learning Java - Switch from structured programming to object oriented programming
By arnor in forum New To JavaReplies: 3Last Post: 05-24-2014, 09:26 PM -
android programming vs game programming using java
By vgoel38 in forum Forum LobbyReplies: 2Last Post: 09-08-2012, 01:11 AM -
Does passing array to methods can lead to procedural paradigm?
By Shikatsu in forum New To JavaReplies: 2Last Post: 12-05-2011, 04:42 PM
Bookmarks