Results 1 to 5 of 5
  1. #1
    moondy is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default Use of Arrays to store data?

    Hi,

    I completed a calculator for school which works fine.

    However my teacher is saying i need to use Arrays to store data?
    What do i need to modify?

    Java Code:
    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class HomeCalculator {
    
       public static void main(String[] args) { //added the mandatory class access specifier
           Scanner scanner = new Scanner(System.in); //i am referencing the scanner object
           String choice = null;
    
           DecimalFormat twoDForm = new DecimalFormat("#.##"); //for decimal formatting
           
           while (!"D".equals(choice)) {
               try
               {
                   System.out
                           .println("--------------------------------------------------------------------");
                   System.out.println("Welcome to Johnís Home calculator");
                   System.out.println("Please select from the choices below");
                   System.out
                           .println("--------------------------------------------------------------------");
                   System.out.println("Choice A: Rent Calculator");
                   System.out.println("Choice B: Electricity Bill Calculator");
                   System.out.println("Choice C: Loan Repayment Calculator");
                   System.out.println("Choice D: Quit calculator\n");
           
                   System.out.print("Choice: ");
                   choice = scanner.next("[ABCD]"); //ensure only ABCD can be inputted
                   
                   if ("A".equals(choice) || "a".equals(choice))
                   {
                       System.out
                       .println("--------------------------------------------------------------------");
                       System.out.println("You have selected to use the Rent Calculator");
                       System.out.println("--------------------------------------------------------------------");
                       
                       System.out.print("Please enter the weekly rent: ");
                       double weeklyRent = scanner.nextDouble(); //reads the rate from input
                       System.out.print("Please enter the number of students: ");
                       int studentsResiding = scanner.nextInt();
                       
                       System.out.println(); //using this statement to target one of multiple blocks of code to be executed
                       if (weeklyRent <= 0)
                       {
                           System.out.println("Weekly rent amount cannot be zero or less.");
                       }
                       else if (studentsResiding <= 0)
                       {
                           System.out.println("Students sharing the rent cannot be zero or less.");
                       }
                       else
                       {                    
                           System.out.print("The monthly rent paid by each student is: $ ");
                       
                           double monthlyRentPerStudent = weeklyRent / studentsResiding; //weekly rent divided by amount of people
                           System.out.println(twoDForm.format(monthlyRentPerStudent)); //output monthly rent per student
                       }
                   }
                   else if("B".equals(choice))
                   {
                       System.out
                       .println("--------------------------------------------------------------------");
                       System.out.println("You have selected to use the Electricity Bill Calculator");
                       System.out.println("--------------------------------------------------------------------");
                       
                       System.out.print("Please enter the initial meter reading: ");
                       int initialReading = scanner.nextInt();
                       System.out.print("Please enter the final meter reading: ");
                       int finalReading = scanner.nextInt();
                       int consumedUnits = finalReading - initialReading;
                       
                       double price = 0;
                       if (consumedUnits < 0 || initialReading < 0)
                       {
                           System.out.println("\nPlease enter units above 0.");
                           continue;
                       }
                       else if (consumedUnits <= 100)
                       {
                           price = consumedUnits * 0.5;
                       }
                       else if(consumedUnits > 100)
                       {
                           price = (( consumedUnits - 100 ) * 0.75) + 50; // where 50 is the price for the first 100 units
                       }
                       double billableAmount = price * 1.10; //where '.10' equals is the 10% GST
                       System.out.println("\nThe electricity bill amount to be paid: $ " + twoDForm.format(billableAmount));
                   }
                   else if("C".equals(choice))
                   {
                       System.out
                       .println("--------------------------------------------------------------------");
                       System.out.println("You have selected to use the Loan Repayment Calculator");
                       System.out.println("--------------------------------------------------------------------");
                       
                       System.out.print("Please enter the loan amount: $ ");
                       double billAmount = scanner.nextDouble();
                       System.out.print("No. of months for repayment: ");
                       int months = scanner.nextInt();
                       System.out.print("Intrest Rate(1-100): ");
                       double intrestRate = scanner.nextDouble();
                       
                       System.out.println();
                       if(billAmount < 1500)
                       {
                           System.out.println("The minimum loan taken out must be greater than $1500.");
                       }
                       else if(months > 36)
                       {
                           System.out.println("The maximum repayment plan must be lower than 36 months (3 years).");
                       }
                       else if(months < 1)
                       {
                           System.out.println("You cannot have less than 1 month!");
                       }
                       else if(intrestRate > 100 || intrestRate < 0)
                       {
                           System.out.println("Incorrect interest rate. Correct interest input can range from 1-100.");
                       }
                       else
                       {
                           double repaymentAmount = (billAmount + (billAmount * intrestRate/100)) / months; //loan amount divide amount of months times interest rate equals repayments
                       
                           System.out.println("Repayment Amount per month: $ " + twoDForm.format(repaymentAmount));
                       }
                   }
                   System.out.println();
                   System.out.println();
               }
               catch(Exception exp) //added a catch block to stop the program mishandling incorrect ABCD choices
               {
                   System.out.println("--------------------------------------------------------------------");
                   System.out.println("Error has occured due to incorrect data input. Please try again.");
                   System.out.println("--------------------------------------------------------------------\n\n");
                   
                   scanner.next();
               }
           }
           
           System.out.println("--------------------------------------------------------------------");
           System.out.println("Thank you for using Johnís Home calculator, Gíday!");
           System.out
           .println("--------------------------------------------------------------------");
           
           scanner.close();
       }
    }

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

    Default

    No idea where you are supposed to be using an array in that lot.
    There doesn't seem to be an obvious candidate to me.

  3. #3
    jankidudel is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    If think your teacher is lazy to read your code, because it has much of the text.(long lines)

    Places you may put data into array: messages like error... and ------ you can put into array and it would be more readable in the action code., also you can do as a constant 'CHOICE' and append the text to it. Try to work with your messages. But it's only my opinion,.

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

    Default

    They wouldn't be arrays...static final, yes, but not arrays.
    Anyway, you'd improve the clarity more by simply breaking that monster method in smaller ones. Even simply having each of the three options as a different method (or different class) would make mnore sense than employing arrays for the strings.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    Anyway, you'd improve the clarity more by simply breaking that monster method in smaller ones. Even simply having each of the three options as a different method (or different class) would make mnore sense than employing arrays for the strings.
    Of course, in one aspect it's really hard to read. So that it's really difficult to track something in your code at once.

    Secondly, it's bad coding practice, all about Object Oriented concepts. Keep your code is collection of smaller parts, know as functions mainly (involve some others too)

Similar Threads

  1. Using Throwable to store data in buffers and "throwing" data
    By BruteforceFtw in forum New To Java
    Replies: 7
    Last Post: 06-08-2010, 02:02 PM
  2. Reading txt files and store them in arrays
    By NeverHide in forum New To Java
    Replies: 5
    Last Post: 05-20-2010, 08:18 PM
  3. Store values in arrays
    By ŖŗΫ ỏÉ Ңόρę in forum New To Java
    Replies: 1
    Last Post: 03-21-2010, 07:53 AM
  4. How to store data.
    By djr1 in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 07:33 AM
  5. how to store the data in data base
    By eclipse3.4ide in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 04:25 AM

Posting Permissions

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