6 Replies - 250 Views - Last Post: 06 December 2017 - 08:15 AM Rate Topic: -----

#1 leornungtarda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 17

Help condensing / simplifying code

Posted 04 December 2017 - 11:16 PM

Hello all, I've decided to stop lurking and ask for help.
Below is my code, which looks like a mess to me, but it's semi-functional at the moment.
The end goal is to create a new .dat file with the outcome of the charges/purchases/payments.
Any advice would be greatly appreciated.
import java.util.Scanner;        
import java.io.*;                 

public class StillWorkingOnIt
{
   public static void main(String[] args) throws IOException
   {
      int[] numbers = {111, 222};
      double[] begBal = {100.00, 200.00};
      double[] purchases = {200.00, 300.00};
      double[] payments = {50.00, 100.00};
      double financeCharge = .01;
      PrintWriter outputFile = new PrintWriter("BeginningBalance.dat");
      
      printInfo();
      
      for (int index = 1; index<numbers.length; index++)
         outputFile.println(numbers[0]);
         outputFile.println(numbers[1]);
      for (int index = 1; index<begBal.length; index++)
         outputFile.println(begBal[0]);
         outputFile.println(begBal[1]);
      for (int index = 1; index<purchases.length; index++)
         outputFile.println(purchases[0]);
         outputFile.println(purchases[1]);
         
         System.out.print(numbers[0]);
         System.out.print("\t\t"+begBal[0]);
         System.out.printf("\t\t\t%.2f",financeCharge*begBal[0]);
         System.out.print("\t\t\t"+purchases[0]);
         System.out.print("\t\t\t"+payments[0]);
         System.out.print("\t\t"+(((begBal[0]*financeCharge)+begBal[0]+purchases[0])- payments[0]));
         System.out.print("\n"+numbers[1]);
         System.out.print("\t\t"+begBal[1]);
         System.out.printf("\t\t\t%.2f",financeCharge*begBal[1]);
         System.out.print("\t\t\t"+purchases[1]);
         System.out.print("\t\t\t"+payments[1]);
         System.out.print("\t\t"+(((begBal[1]*financeCharge)+begBal[1]+purchases[1])- payments[1]));
         
         System.out.println("\n\nTotals\t"+(begBal[0]+begBal[1])+
                            "\t\t\t"+((begBal[0]*financeCharge)+(begBal[1]*financeCharge)) +
                            "\t\t\t" + (purchases[0]+purchases[1]) + "\t\t\t" +
                            (payments[0]+payments[1]) + "\t\t" + (begBal[0]+(financeCharge*begBal[0])+(purchases[0]- payments[0])+
                            ((begBal[1]*financeCharge)+begBal[1]+purchases[1])- payments[1]));
      outputFile.close();
   }

   public static void printInfo()
   {
      System.out.println("Cust No\tBeg.Bal.\tFinance Charge"+
                         "\tPurchases\tPayments\tEnding Bal.");
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help condensing / simplifying code

#2 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12278
  • View blog
  • Posts: 45,364
  • Joined: 27-December 08

Re: Help condensing / simplifying code

Posted 04 December 2017 - 11:29 PM

What specific problems or errors are you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 leornungtarda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 17

Re: Help condensing / simplifying code

Posted 04 December 2017 - 11:46 PM

View Postmacosxnerd101, on 04 December 2017 - 11:29 PM, said:

What specific problems or errors are you encountering?


Hey mac,

I'm not very familiar with arrays, or java in general to be honest and I am hoping to get some advice on how to either condense this code down by turning some of the code into methods, or some other technique I haven't been introduced to yet. When attempting to use methods I get errors such as:
StillWorkingOnIt.java:17: error: illegal start of expression
StillWorkingOnIt.java:17: error: ';' expected (after the method name)
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,483
  • Joined: 12-December 12

Re: Help condensing / simplifying code

Posted 05 December 2017 - 06:48 AM

The biggest change you can make is to move away from parallel arrays. Java is an object-oriented programming language, so use classes to encapsulate related data into individual entities (objects). For example, this

System.out.print("\t\t"+(((begBal[0]*financeCharge)+begBal[0]+purchases[0])- payments[0]));

tells us - because they all use the same 0-index value - that these items are part of a single object instance (a transaction?).

Your loops are also redundant, as you do nothing with the 'index' values. You aren't iterating your array elements. (You just flatten them by duplicating all the code for the 0 and 1st elements.)
Was This Post Helpful? 1
  • +
  • -

#5 leornungtarda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 17

Re: Help condensing / simplifying code

Posted 05 December 2017 - 08:00 PM

View Postandrewsw, on 05 December 2017 - 06:48 AM, said:

The biggest change you can make is to move away from parallel arrays. Java is an object-oriented programming language, so use classes to encapsulate related data into individual entities (objects). For example, this

System.out.print("\t\t"+(((begBal[0]*financeCharge)+begBal[0]+purchases[0])- payments[0]));

tells us - because they all use the same 0-index value - that these items are part of a single object instance (a transaction?).

Your loops are also redundant, as you do nothing with the 'index' values. You aren't iterating your array elements. (You just flatten them by duplicating all the code for the 0 and 1st elements.)


Andrew,

I rewrote the entire code, thanks for the earlier advice. My main goal now is to create a new .dat file with the new Ending Balance amounts for each account. Also, I would ideally get the original array information (BeginningBalance.dat) from an existing .dat file, but I can't figure out how to do that at all.

This is my current code:
import java.util.Scanner;     
import java.io.*;             

public class StillWorkingOnIt
{
   public static void main(String[] args) throws IOException
   {
      Scanner keyboard = new Scanner(System.in);
      int customerNumber;              // as I post this I realize I'm not using any of this.
      double beginningBalance;         // or this
      double purchases;                // or this
      double payments;                 // or this
      double[] numbers = {111, 100.00, 200.00, 50.00,
                          222, 200.00, 300.00, 100.00};
      
      PrintWriter inputFile = new PrintWriter("BeginningBalance.dat");
      
      inputFile.printf("%.0f",numbers[0]);
      inputFile.println("\n"+numbers[1]);      
      inputFile.println(numbers[2]);
      inputFile.println(numbers[3]);
      inputFile.printf("%.0f",numbers[4]);
      inputFile.println("\n"+numbers[5]);
      inputFile.println(numbers[6]);
      inputFile.println(numbers[7]);
      inputFile.close();
      
      printInfo();
      firstLine(numbers);
      begBal(numbers);
      financeCharge(numbers);
      purchases(numbers);
      payments(numbers);
      endingBal(numbers);
      listTotals(numbers);
   }
   
   public static void printInfo()
   {
       System.out.println("Cust No\tBeg.Bal.\tFinance Charge"+
                         "\tPurchases"+"  "+"Payments\tEnding Bal.");
   }
   public static void firstLine(double[] numbers)
   {
      double fiCh = .01 * numbers[1];
      double endBal = ((numbers[1]+numbers[2]+fiCh)-numbers[3]);
      System.out.printf("%.0f",numbers[0]);
      System.out.printf("\t\t%.2f",numbers[1]);
      System.out.printf("\t\t%.2f",fiCh);
      System.out.printf("\t\t\t%.2f",numbers[2]);
      System.out.printf("\t\t%.2f",numbers[3]);
      System.out.printf("\t\t%.2f",endBal);
      System.out.printf("\n%.0f",numbers[4]); 
   }  
   
   public static void begBal(double[] numbers)
   {
      System.out.printf("\t\t%.2f",numbers[5]);
   }
   
   public static void financeCharge(double[] numbers)
   {
      double fiCh = .01;
      System.out.printf("\t\t%.2f",(numbers[5]*fiCh));
   }
   
   public static void purchases(double[] numbers)
   {
      System.out.printf("\t\t\t%.2f",numbers[6]);
   }
   
   public static void payments(double[] numbers)
   {
      System.out.printf("\t\t%.2f",numbers[7]);
   }
   public static void endingBal(double[] numbers)
   {
      double fiCh = numbers[5]*.01;
      double endBal = ((numbers[5]+numbers[6]+fiCh)-numbers[7]);
      System.out.printf("\t%.2f",endBal);
   }
   public static void listTotals(double[] numbers)
   {
      double begBal = numbers[1]+numbers[5];
      double fiCh = ((numbers[1]*.01)+numbers[5]*.01);
      double pur = numbers[2]+numbers[6];
      double pay = numbers[3]+numbers[7];
      double endBal = begBal + fiCh + pur - pay;
      System.out.print("\n\nTotals");
      System.out.printf("\t%.2f",begBal);
      System.out.printf("\t\t%.2f",fiCh);
      System.out.printf("\t\t\t%.2f",pur);
      System.out.printf("\t\t%.2f",pay);
      System.out.printf("\t%.2f",endBal);
   }
   
}


Was This Post Helpful? 0
  • +
  • -

#6 leornungtarda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-December 17

Re: Help condensing / simplifying code

Posted 05 December 2017 - 08:55 PM

Below is my functional and (almost) final code. If anyone could give me some advice on how to read ints and doubles from a file and turn them into arrays, that'd be great. I've asked my professor for clarification on the assignment, since I might not even have to gather the data from the file, but just in case. Thanks.

import java.util.Scanner;     
import java.io.*;  
           
public class StillWorkingOnIt
{
   public static void main(String[] args) throws IOException
   {
      Scanner keyboard = new Scanner(System.in);
      double[] numbers = {111, 100.00, 200.00, 50.00,
                          222, 200.00, 300.00, 100.00};
      
      PrintWriter inputFile = new PrintWriter("BeginningBalance.dat");
      
      inputFile.printf("%.0f",numbers[0]);
      inputFile.println("\n"+numbers[1]);      
      inputFile.println(numbers[2]);
      inputFile.println(numbers[3]);
      inputFile.printf("%.0f",numbers[4]);
      inputFile.println("\n"+numbers[5]);
      inputFile.println(numbers[6]);
      inputFile.println(numbers[7]);
      inputFile.close();
      
      printInfo();
      firstLine(numbers);
      begBal(numbers);
      financeCharge(numbers);
      purchases(numbers);
      payments(numbers);
      endingBal(numbers);
      listTotals(numbers);
   }
   
   public static void printInfo()
   {
       System.out.println("Cust No\tBeg.Bal.\tFinance Charge"+
                         "\tPurchases"+"  "+"Payments\tEnding Bal.");
   }
   public static void firstLine(double[] numbers)
   {
      double fiCh = .01 * numbers[1];
      double endBal = ((numbers[1]+numbers[2]+fiCh)-numbers[3]);
      System.out.printf("%.0f",numbers[0]);
      System.out.printf("\t\t%.2f",numbers[1]);
      System.out.printf("\t\t%.2f",fiCh);
      System.out.printf("\t\t\t%.2f",numbers[2]);
      System.out.printf("\t\t%.2f",numbers[3]);
      System.out.printf("\t\t%.2f",endBal);
      System.out.printf("\n%.0f",numbers[4]); 
   }  
   
   public static void begBal(double[] numbers)
   {
      System.out.printf("\t\t%.2f",numbers[5]);
   }
   
   public static void financeCharge(double[] numbers)
   {
      double fiCh = .01;
      System.out.printf("\t\t%.2f",(numbers[5]*fiCh));
   }
   
   public static void purchases(double[] numbers)
   {
      System.out.printf("\t\t\t%.2f",numbers[6]);
   }
   
   public static void payments(double[] numbers)
   {
      System.out.printf("\t\t%.2f",numbers[7]);
   }
   public static void endingBal(double[] numbers)
   {
      double fiCh = numbers[5]*.01;
      double endBal = ((numbers[5]+numbers[6]+fiCh)-numbers[7]);
      System.out.printf("\t%.2f",endBal);
   }
   public static void listTotals(double[] numbers) throws IOException
   {
      double acc1 = numbers[0];
      double acc2 = numbers[4];
      double begBal = numbers[1]+numbers[5];
      double fiCh = ((numbers[1]*.01)+numbers[5]*.01);
      double pur = numbers[2]+numbers[6];
      double pay = numbers[3]+numbers[7];
      double endBalA = ((numbers[1]*.01)+numbers[1]+numbers[2]-numbers[3]);
      double endBalB = ((numbers[5]*.01)+numbers[5]+numbers[6]-numbers[7]);
      double endBalTotal = endBalA + endBalB;
      System.out.print("\n\nTotals");
      System.out.printf("\t%.2f",begBal);
      System.out.printf("\t\t%.2f",fiCh);
      System.out.printf("\t\t\t%.2f",pur);
      System.out.printf("\t\t%.2f",pay);
      System.out.printf("\t%.2f",endBalTotal);
      
      PrintWriter outputFile = new PrintWriter("NewBalance.dat");
      outputFile.printf("%.0f",acc1);
      outputFile.printf("\n%.2f",endBalA);
      outputFile.printf("\n%.0f",acc2);
      outputFile.printf("\n%.2f",endBalB);
      outputFile.close();
   }
   
}


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,028
  • Joined: 20-September 08

Re: Help condensing / simplifying code

Posted 06 December 2017 - 08:15 AM

Your question is unclear, as is your code. It's difficult to understand since your naming is wrong. Method names should be verbs.

http://technojeeves....yle-conventions

But i don't really know what the problem is, since you can read your file as doubles, whether a decimal point exists or not. Also Scanner has next(), hasNextInt() and hasNextDouble() so you should be able to do something with that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1