Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: I Need some help for a java project

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I Need some help for a java project

    ok, here is the deal , i am given the following instuctions and through them i must write a program :
    A bookstore has a system for managing its customers. The system keeps information about the books each customer has bought,his debit balance[ the customers have the option of buying with credit (in other words to pay after a few months)], and the total amount of money he has spent till now( the cost of the books he bought so far). For each customers ,are available the below data(integers):

    his code ( which constructed by 3 capital letters and 5(number) digits) > (Code)
    his age >(Age)
    the number of Books he already bought > (Books)
    Total amount he has paid(spent) so far> (Total)
    Debit Balance > (Decifit)


    (in parenthesis are the name of the variables)


    The file 'bookstore.csv' contains 1000 records of clients, unclassified, which are 'printed' from source file 'printCSVFile.java'.



    And Below are the "Questions"for this Project :

    B1. to write a class named 'client', which contains the above elements.
    B2. to write a method that calculates the average price of books purchased by the customer (to the nearest whole number). Sought retroactive implementation of the method.
    B3. to write a method, which calculates the net profit per customer of the bookshop, ( the money already spent minus the debit balance.)
    B4. to write a method, which calculates the amount of monthly installment, if the outstanding balance must be paid within 3 months for a balance of 100 € and within six months on balance over 100 € (without regard to any rate) .
    B5. to write a method, which calculates the importance of a client. A customer is considered 'important':
    a. if you are over 40 years, has purchased at least 30 books and the outstanding balance does not
    exceed 5% of the amount already spent or
    b. if between 25 and 40 years and has purchased at least 10 books on average at least 15 € or
    c. if you are over 40 years and the amount already spent at least 1500 €.
    B6. to write a method (out of the class) that outputs to the minimum, maximum, average and standard deviation of the number of books already purchased by customers, the amount already spent and their debt balance. Finally, print and the percentage of customers that is important.
    B7. to write a method (out of the class) which can print all customers who have already spent more than a certain amount or customers who have already bought more than a specified number of books. The criterion of 'amount' or 'books' should be selected by the user, as well as the minimum for the selected criteria.
    B8. to write a method (out of the Class) which is to seek a client based on his code, using the method of BinarySearch.


    HERE is the link to bookstore.csv http://eduportal.dmst.aueb.gr/html/d...ore__13821.csv
    and HERE is the printCSV.java http://eduportal.dmst.aueb.gr/html/d...le__13820.java


    I thought that i need to first read the contents of csv , split the values wherever ther is a ";" an then insert them into an array. Thats for the first step. The problem is that i havent the slightest idea how to do so!( since the havent even mentioned this in class ...) I tried using scannner class but failed.

    I experemented a litle and came up with this
    int [][] customers = new int [1000][5];
     
    	File file = new File("bookstore.csv");
     
    	Scanner scanner = new Scanner(file);
     
    	for (int i=0;i<1000;i++){
    		for (int j=0;j<5;j++){
    			pelates[i][j]= scanner.nextInt();}
    		}
    		System.out.println(customers[i][j]);
    //System.out.println(customers[999][1]);
    but it returns a"input missmatch Exception" I believe that it is caused by the fact that the first coloumn is a read as a string( cause it has the form ex: AAA12345)

    Ive searched the net, and found about the StringTokenizer method, but i think that method reads all data as strings, meaning that customers array must be string type. if i do so though isnt it "imposible" to do the calculations mentioned afterwards?? ( ex: the minimun,max, average etc) .

    Please i could really need some help on this, and if you could be kind enough to explain to me what to do . I dont want someone to solve it for me, just to help me . mostly with the arrays part
    Last edited by george; April 20th, 2010 at 02:51 PM.


  2. #2
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Need some help for a java project

    ok i think i've made some progress on this but i still need help.

    This time i get an "NullPointerException"

    import java.io.*;
    import java.text.*;
    import java.util.*;
    import java.util.Scanner;
     
    class Client{
    	String code;
    	int age;
    	int books;
    	long total;
    	long decifit;
    }
     
    public class test3 {
     
      public static void main(String[] args) throws Exception {
    //**********************************************************//
      //test arrays//
        Client[] clients=new Client[1000];
        int pos=0;
        String[] code=new String[1000];
        int[] age=new int[1000];
     
     
     
     
    //**********************************************************//
    	int i;
    	int rowCounter;
    	int payments=0;
     
        String inputDocument = "bookstore.csv";
        FileInputStream is = new FileInputStream(inputDocument);
        Reader iD = new InputStreamReader(is);
     
     
        BufferedReader buf = new BufferedReader(iD);
    	String inputLine;
    	while ((inputLine = buf.readLine()) != null) {
    		String[] lineParts = inputLine.split(";");
    		clients[pos].code=lineParts[0];
    		clients[pos].age= Integer.parseInt(lineParts[1]);
    		clients[pos].books= Integer.parseInt(lineParts[2]);
    		clients[pos].total= Integer.parseInt(lineParts[3]);
    		clients[pos].decifit= Integer.parseInt(lineParts[4]);
           pos++;
     
     
     
    	}
     
     
     
      }
     
    }

    any ideas ??

  3. #3
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Need some help for a java project

    Ok , Solved that problem
     
     
     
     
    /**
     *
     * @author George
     */
    import java.io.*;
    import java.text.*;
    import java.util.*;
    import java.util.Scanner;
     
     
    class Client{
    	String code;
    	int age;
    	int books;
    	int total;
    	String decifit;
    }
     
     
    public class Main {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws Exception {
           //**********************************************************//
      //test arrays//
        Client[] clients=new Client[1000];
        int pos=0;
        String[] code=new String[1000];
        int[] age=new int[1000];
        for (int i = 0; i < clients.length; i ++) {
    	    clients[i] = new Client();
    	}
     
     
     
    //**********************************************************//
    	int i;
    	int rowCounter;
    	int payments=0;
     
        String inputDocument = "F://bookstore.csv";
        FileInputStream is = new FileInputStream(inputDocument);
        Reader iD = new InputStreamReader(is);
     
     
        BufferedReader buf = new BufferedReader(iD);
    	String inputLine;
    	while ((inputLine = buf.readLine()) != null) {
    		String[] lineParts = inputLine.split(";");
    		clients[pos].code=lineParts[0];
    		clients[pos].age= Integer.parseInt(lineParts[1]);
    		clients[pos].books= Integer.parseInt(lineParts[2]);
    		clients[pos].total= Integer.parseInt(lineParts[3]);
    		clients[pos].decifit= /*Long.parseLong*/(lineParts[4]);
                    pos++;
     
     
     
     
    	}
    //TEST IS ALL WEN ALRIGHT
    for (i=0;i<1000;i++){
    System.out.println(clients[i].code +"   "+ clients[i].age+"   "+ clients[i].books+"   "+ clients[i].total+"   "+ clients[i].decifit);
     
    }
     
     
     
     
     
        }
     
    }

    now i bupmped into another problem. The last column of the scv file contains some numbers wich are like this" 23,7" . I tried to read them as int,long,double and float but it returned an error, so i read them as Strings. The pbolem is later, if i want to convert them to (lets say Long) to compare if they are bigger oe less than a number( ex: 40), how do i do it?? ( since i canr use Long.parseLong ??

    i need to do that for
    B2. to write a method that calculates the average price of books purchased by the customer (to the nearest whole number). Sought retroactive implementation of the method.
    B3. to write a method, which calculates the net profit per customer of the bookshop, ( the money already spent minus the debit balance.) and every thing that requires to write a method.

    Here i have another problem : How do i write the method so that it gets the data from clients.books (for example) ??

    if t do it like this
        static long Average(int[] clients) {
            int sum=0;
             for (int i = 0; i < 1000; i++) {
     		     sum=clients.books[i]+sum;
     
     		   }
     
            int c=(int)sum/1000; return (c);
        }
    it doesnt work and only return errors...

    Basicaly , i dont know how the method parameters should be contructes so that it gets its values from clients[].books.

    Can someone please help me ??
    Last edited by george; April 21st, 2010 at 11:13 AM.

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Need some help for a java project

    can anyone help ??

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: I Need some help for a java project

    It's reading it as one string because they aren't separated by spaces. The simple answer is to change the separator type when reading that last line to be separated by a comment also.

    Scanner reader = new Scanner("23,7");
    reader.useDeliminators(",");
    System.out.println("First read number: " + reader.nextInt());
    System.out.println("Second read number: " + reader.nextInt());

  6. #6
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Need some help for a java project

    Quote Originally Posted by helloworld922 View Post
    It's reading it as one string because they aren't separated by spaces. The simple answer is to change the separator type when reading that last line to be separated by a comment also.

    Scanner reader = new Scanner("23,7");
    reader.useDeliminators(",");
    System.out.println("First read number: " + reader.nextInt());
    System.out.println("Second read number: " + reader.nextInt());
    thanks helloworld922 , you're the only one who replied after 60 + views

    can you help me with the recursive method ??

    I need to write a recursive method which calculates the average value of the books the customers have bought so far.
    i've tried different things but ended up only with errors.
    for ex : i've tried
    nt av(Client[] clients,int sum){
                int i=0;
                for (i=0; i<1000;i++  ){
     
     
                   return (sum+ av(clients[i].books));  //>> i get :
                    //" operator   cannot be applied to int,av and 2 more errors.
                   // i've also tried : return (sum+=av(clients[i].books)); >>again errors...
     
                }
                 return(); //i know here its wrong...
     
            }

    i know the method has to read from clients[i].books and each time it calls it self to do the adding and with the final return to do (ex: sum/1000). but i dont know how to do that, while hadling object arrays.(or any type off arrays)...the only example they've shown us so far, is the factorial recursive example, which i must say does not really help me with this...

  7. #7
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: I Need some help for a java project

    A recursive algorithm for finding the average can go something like this (I hope this works):

    // pseudo-code
    double Average(double[] data, int startingLocation)
     
    if startingLocation is greater than or equal to data.length - 1:
    // base case
     There's only one number left to average, return the value at startingLocation
    else:
    // recursive step
     average = (Average(data, startingLocation + 1) + data[startingLocation]) / 2
    return average

    basically, the premise of this algorithm is that it only ever averages two numbers at a time, but will average out all the sums and percolate until one final average is reached.

    ex.:

    1, 2, 3, 4, 5
    average = average(1,average(2,average(3,average(4,5))))
    (where here average is taking the average of only 2 numbers at a time)
    Last edited by helloworld922; April 23rd, 2010 at 01:47 AM.

  8. #8
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Need some help for a java project

    Based on what youve given me, i tried :

    class Client{
    	String code;
    	int age;
    	int books;
    	int total;
    	String decifit;
            int average;
     
            long av(long[] box, int sum){
     
                if (sum >=box.length - 1)
                 // base case
                 return average;
                else
                  // recursive step
                 average = (int)((av(box, sum + 1) + box[sum]) / 2);
                return average;
     
            }
     
     
     
    }
            }

    and call it like this in main

    Client ave=new Client();
    int average2=0;
     
    ave.av(books, average2);

    but it printss something like : "bookstore2.Client@1de3f2d"

    and when i put
     if (sum >=box.length - 1)
                 // base case
                {System.out.println(average);
                 return average;}
                else
    to see what happens it prints 0

  9. #9
    Junior Member
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I Need some help for a java project

    ok solved this problem ( up to B6)

Similar Threads

  1. Programmer for a Java based game project
    By Takkun in forum Project Collaboration
    Replies: 4
    Last Post: June 14th, 2010, 05:47 PM
  2. Java project
    By monika88a in forum AWT / Java Swing
    Replies: 5
    Last Post: January 19th, 2010, 11:10 PM
  3. Short java project
    By derky in forum Paid Java Projects
    Replies: 3
    Last Post: October 28th, 2009, 09:48 PM
  4. Java stock project help
    By lotus in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: July 12th, 2009, 04:16 AM
  5. Best way to architect my java and XML project...
    By samiles in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: June 16th, 2009, 08:43 AM