0

Recently, a friend of mine asked me to program something for him that would make managing his small business easier. Basically the user is supposed to select the name of a client, and the code retrieves information about the client like the amount this client owes, or the next time my friend is supposed to meet with this client. Right now, my program is doing a fine job reading from the file called AccountData.txt by using this small bit of code...

        myScanner = new Scanner(System.in);
        diskScanner = new Scanner(AccountData);

        System.out.println("Type an existing account name to get started or type 'new' to create another");

        name = myScanner.next();

        do {
            reading = diskScanner.next();
        } while(!reading.equals(name));


        weekday = diskScanner.next();
        day = diskScanner.nextInt();
        month = diskScanner.nextInt();
        Amount = diskScanner.nextDouble();
        TotalAmount = diskScanner.nextDouble();

        System.out.print("Next Job is ");
        System.out.print(weekday);
        System.out.print(" ");
        System.out.print(month);
        System.out.print("/");
        System.out.println(day);
        System.out.print("The total amount payed by this client is ");
        System.out.println(TotalAmount);
        System.out.print("The total amount owed is ");
        System.out.println(Amount);

         AddDates.AddingDates();

...but I'm having problems writing to the existing code.

public class AddDates {
     private static Scanner myScanner;
     static String reply;
          public static void AddingDates() throws IOException {
              myScanner = new Scanner(System.in);
              System.out.println("Add Dates?");
              reply = myScanner.next();
              AccountReader.adder = new PrintWriter(new FileWriter(AccountReader.AccountData, true));

               if (reply .equals("Y")) {
                    System.out.println("Day, Date, Month, Cost of Lawn");
                    AccountReader.weekday = myScanner.next();
                    AccountReader.day = myScanner.next();
                    AccountReader.month = myScanner.next();
                    AccountReader.Amount = myScanner.next();

                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.weekday);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.day);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.month);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.Amount);
                    AccountReader.adder.print(" ");
                    AccountReader.adder.print(AccountReader.TotalAmount);
                    AccountReader.adder.flush();
                } else {
                     System.out.println("OK");
                }
      }

}

because reading from the file is location sensitive, this bit of code that writes to the file doesn't work. That is mainly because it adds the information to the end of the file. (I should also add that the AccountData.txt file looks something like this)

Masterson Saturday 22 9 50 300
Johnson Sunday 17 8 20 140 Sunday 24 8 20 140
//Etc.

So my main question is... is there anyway to write to a file and have information be written to a specific place in the file (like next to other strings) or is there another way I should write my code to make it work for my purposes?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • I suggest you learn about random access files. – Code-Apprentice Jul 17 '14 at 00:29
  • 2
    You NEED to write to a file that as you are reading it? It sounds like a disaster waiting to happen. You should read the files into some data-structure, do your transformations, and write back out to the file. – Isaiah van der Elst Jul 17 '14 at 00:37
  • Your friend should learn some basics of running a business. Half of what he wants can be done by Outlook tasks, and the accounting part should be done by accounting software. Period. You can't run a business on toy software cobbled together by someone who basically doesn't know how, sorry. – user207421 Jul 17 '14 at 00:43

2 Answers2

1
client like the amount this client owes,

If you're not experienced in programming I would highly recommend stepping away from this untill you have a firmer grasp on what your doing, because dealing with money can get extremely dangerous and one small bug in your code could cause alot of damage.

Now I'm still not exactly clued in here on what your trying to do. are you trying to change the text in the file from

Masterson Saturday 22 9 50 300

to

Masterson Saturday 22 9 50 300 --> Processed Successfully!

in which case you would have to open both an input and an output stream on the file, simply write the output stream untill youve reached the line you want to modify, print the line, and continue putting the input stream into the output stream. If I knew exactly what you are looking for I can do some sample code for it

Arhowk
  • 901
  • 4
  • 11
  • 22
0

Please do not use doubles when doing money/currency calculations. See Why not use Double or Float to represent currency? for the reasons.

Quick summary - "floats and doubles cannot accurately represent the base 10 multiples we use for money." ... "Representing money as a double or float will probably look good at first as the software rounds off the tiny errors, but as you perform more additions, subtractions, multiplications and divisions on inexact numbers, you'll lose more and more precision as the errors add up. This makes floats and doubles inadequate for dealing with money, where perfect accuracy for multiples of base 10 powers is required."

Community
  • 1
  • 1
DavidPostill
  • 7,734
  • 9
  • 41
  • 60