Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: Advice on Java programming design

  1. #1
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Advice on Java programming design

    I am very new to java, basically i am self learning for fun and taking hints and tips from anywhere i can get them. I have written some small programs, nothing very difficult at all, i find i can solve many of the problems i encounter using (Do While - IF & IF Else), my problem is this, i don't know enough of the Java commands to vary how i structure my programming designs, there are obviously many other Java commands which can make the programming experience more interesting and efficient but not knowing which ones to use and how to construct them is a bit of a problem, i don't have a specific question but i will attach some code if your interested in viewing it and maybe giving me some feedback on how to re-write the code in a more efficient way, if that's possible. I don't want to be a Java programmer who only use's a small portion of the available commands.


    Java Code:
    import java.util.Scanner;
    
    public class Q10 {
    
       public static void main(String[] args) {
    
          Scanner myinput = new Scanner(System.in);
    
          int accno, balance = 0, ItemsCharged = 0, TotalCreditApplied = 0, CreditLimit = 2000;
    
          System.out.println("Please enter your account number ");
          accno = myinput.nextInt();
          System.out.println("Hi, your account number is "+accno+" your current Credit Limit is "+CreditLimit);
          System.out.println();
    
          do {
             System.out.println("Please enter the value of your current purchases for today ");
             ItemsCharged = myinput.nextInt();
             TotalCreditApplied = ItemsCharged+TotalCreditApplied;
             balance = CreditLimit - TotalCreditApplied;
    
             if(TotalCreditApplied > CreditLimit) {
                System.out.println("You have reached your available Credit Limit");
             }
             else {
                System.out.println("Total value of items charged to your account in this sale is "+ItemsCharged);
                System.out.println("Total overall Credit applied to your account is "+TotalCreditApplied);
                System.out.println("Your account balance available to spend is "+balance);
             }
          }
          while(TotalCreditApplied < CreditLimit);
       }
    }
    Last edited by JosAH; 11-01-2012 at 07:42 PM. Reason: fixed the [code] ... ]/code] tags (and indentation)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Advice on Java programming design

    My first comment (and only one at the moment) is to indent your code correctly.
    Unindented (unformatted) code is very hard to read.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Advice on Java programming design

    Hi Tolls,

    When you say indent code, what does this mean ?

    Helen

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Advice on Java programming design

    Java Code:
    public class SomeClass {
        private int someId;
    
        public void someMethod() {
            while(someBoolean) {
                doSomethingElse();
            }
        }
    }
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    helen1974 is offline Member
    Join Date
    Mar 2012
    Location
    Ireland
    Posts
    18
    Rep Power
    0

    Default Re: Advice on Java programming design

    I have tried to paste the code so that it looks like yours, indented, but it just appears on the screen the same way as my original post, i thought it would appear correctly once i entered [code] at the begining and end of the code i entered, but obviously not, any help in solving this would be appreciated.

    Helen
    Last edited by helen1974; 11-01-2012 at 07:36 PM.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,784
    Blog Entries
    7
    Rep Power
    21

    Default Re: Advice on Java programming design

    I was in a nice mood and also fixed the indentation (and curly bracket placement).

    kind regards,

    Jos
    helen1974 likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Quick Advice on bad design
    By garnaout in forum New To Java
    Replies: 1
    Last Post: 05-08-2012, 09:53 PM
  2. Replies: 1
    Last Post: 12-26-2011, 02:50 PM
  3. Replies: 2
    Last Post: 03-03-2011, 09:23 AM
  4. Replies: 1
    Last Post: 10-31-2010, 08:41 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
  •