我的构造函数无法正确读取文件问题

时间:2019-05-06 03:46:49

标签: java file class

我正在尝试读取CSV文件,当我尝试执行代码时,它将返回所有null。我对如何在不创建新构造函数的情况下正确打印出来感到困惑。

我尝试重做CustomerList Multiple中的read方法。它会打印我想要的所有字段,但不会返回我想要的数据。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment08;

import java.io.File;
import java.io.FileNotFoundException;

/**
 *
 * @author banth
 */
public class Assignment08 {

    /**
     * @param args the command line arguments
     * @throws java.io.FileNotFoundException
     */

    public static void main(String[] args) throws FileNotFoundException {
        boolean b = false;
    // Read file
        System.out.println("Read file");
        CustomerList cl = CustomerList.read("Customers.csv");
        if(cl != null) {
            System.out.println("Read: " + cl.size() + " records");
            System.out.println(cl.toString() + "\n\n\n");
        } else {
            System.out.println("File read error.");
            return;
        }
    // Test get and set for CustomerList
        System.out.println("Test get and set for CustomerList");
        System.out.println("x = " + cl.get(0));
        Customer c = cl.get(0);
        c.setFirstName("Homer");
        cl.set(c, 0);
        System.out.println("x = " + cl.get(0));
        System.out.println("\n\n\n");

    // Test indexOf and update
        System.out.println("Test indexOf and update");
        System.out.println("idx = " + cl.indexOf(34));
        System.out.println("Customer with id 35 = \n" + cl.get(cl.indexOf(35)));
    // Update an existing Customer
        b = cl.update(35, 100.0);
        if(b) {
            System.out.println("Update successful");
        } else {
            System.out.println("Update not successful");
        }
        System.out.println("Customer with id 35 = \n" + cl.get(cl.indexOf(35)));
        cl.update(41, 150.0);
        cl.update(59, 200.0);
        cl.update(72, 250.0);
    // Update a non-existing Customer
        b = cl.update(350, 100.0);
        if(b) {
            System.out.println("Update successful");
        } else {
            System.out.println("Update not successful");
        }
        System.out.println("\n\n\n");
    // Test remove
        System.out.println("Test remove");
        System.out.println("x = " + cl.get(1));
    // Remove an existing Customer
        c = cl.remove(1);
        if(c != null) {
            System.out.println("Deleted\n" + c.toString());
        } else {
            System.out.println("No Customer deleted");
        }   
    // Remove an existing Customer
        c = cl.remove(101);
        if(c != null) {
            System.out.println("Deleted\n" + c.toString());
        } else {
            System.out.println("No Customer deleted");
        }
        System.out.println("\n\n\n");
    // Write file
        b = CustomerList.write(cl, "NewCustomers.csv");
        if(b) {
            System.out.println("File write successful");
        } else {
        System.out.println("File write failed");
        }
    }



}

// Person.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment08;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

/**
 *
 * @author banth
 */
public class Person {
    private String firstName;
    private String lastName;
    private String address;
    private String city;
    private String state;
    private String zipCode;    

    public Person(String firstName, String lastName, String address, String city, String state, String zipCode) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;    
    }

    protected Person() {

    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return this.city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Override
    public String toString() {
        return "FirstName: " + firstName + "\nLastName: " + lastName + "\nAddress: " + address + "\nCity: " + city + "\nState: " + state + "\nZipCode: " + zipCode;
    }

    public String toCSV() {
        return this.firstName + "," + this.lastName + "," + this.address + "," + this.city
                + "," + this.state + "," + this.zipCode;
    }

    public void copy(Person p) {       
        firstName = p.firstName;
        lastName = p.lastName;
        address = p.address;
        city = p.city;
        state = p.state;
        zipCode = p.zipCode;
    }

    public void copy(String firstName, String lastName, String address, String city, String state, String zipCode) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
        this.city = city;
        this.zipCode = zipCode;
    }

    @Override
    public Person clone() {
        Person p = new Person(this.firstName, this.lastName, this.address, this.city, this.state, this.zipCode);
        return p;
    }
}

// Customer.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assignment08;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author banth
 */
public class Customer extends Person{
    private int customerID;
    private double grossSales;

    public Customer(int customerID, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
        super(firstName, lastName, address, city, state, zipCode);
        this.customerID = customerID;
        this.grossSales = grossSales;
    }

    public Customer(String s, int customerID, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
        super(firstName, lastName, address, city, state, zipCode);
        copyCSV(s);
    }

    protected Customer() {

    }

    public int getCustomerID() {
        return customerID;
    }

    public void setCustomerID(int customerID) {
        this.customerID = customerID;
    }

    public double getGrossSales() {
        return grossSales;
    }

    public void setGrossSales(double grossSales) {
        this.grossSales = grossSales;
    }

    @Override
    public String toString() {
        return "CustomerID: " + customerID + "\nGrossSales: " + grossSales + super.toString();
    }

    public String toCSV() {
        return this.customerID + "," + this.grossSales + "," + super.toCSV();
    }

    public void copy(Customer c) {
        super.copy(c);
        customerID = c.customerID;
        grossSales = c.grossSales;
    }

    public void copy(int customerId, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
        super.copy(firstName, lastName, address, city, state, zipCode);
        this.customerID = customerId;
        this.grossSales = grossSales;
    }

    public Customer clone() {
        Customer c = new Customer(this.customerID, this.grossSales, this.getFirstName(), this.getLastName(), this.getAddress(), this.getCity(), this.getState(), this.getZipCode());
        return c;    
    }

    public int compareTo(Customer c) {
        int returnValue = 0;

        if (this.customerID > c.customerID) {
            returnValue = -1;
        } else if (this.customerID < c.customerID) {
            returnValue = 1;
        } else {
            returnValue = 0;
        }

        return returnValue;
    }

    public void copyCSV(String s) {
        List<String> list = new ArrayList<>();

        String[] a = s.split(",");

        list = Arrays.asList(a);

        this.copy(Integer.parseInt(list.get(0)), Double.parseDouble(list.get(1)), list.get(2), 
                list.get(3), list.get(4), list.get(5), list.get(6), list.get(7));
    }

}

CUSTOMERLIST.java

///*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package assignment08;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.PrintWriter;
    import java.util.Arrays;
    import java.util.Scanner;

    /**
     *
     * @author banth
     */
    public class CustomerList {
        public Customer[] cl;
        public int size; 

        public CustomerList() {
            this.cl = new Customer[4];
        }

        public int size() {
            return this.size = cl.length;
        }

        public Customer get(Integer i) {
            if (i < 0 || i >= size) {
                throw new IndexOutOfBoundsException("null");
            }
            return cl[i];
        }


        public boolean set(Customer c,Integer i) {
            if (i < 0 || i >= size) {
                throw new IndexOutOfBoundsException("null");
            } else {
                cl[i] = c;
                return true;
            }
        } 

        public void add(Customer c) {
            for (int i = 0; i < size; i++) {
                if (cl[i] == null) {
                    cl[i] = c;
                } else {
                    if (i == size) {
                        Customer[] temp = new Customer[size * 2];
                        for (int j = 0; j < size; j++) {
                            temp[j] = cl[j];
                        }
                        cl[size] = c;
                        size++;

                        }
                    }   
                }
        }

        public Customer remove(Integer i) {
            if (i < 0 || i >= size) {
                throw new IndexOutOfBoundsException("null");
            }
                Customer temp = cl[i];
                for (int j = 0; j < cl.length - 1; j++) {
                    cl[i] = cl[i + 1];
            }
            return temp;
        }

        @Override
        public String toString() {
          String s = " ";
            double sum = 0;
            for(Customer c: cl){
                if(c == null)
                    s = "";
                else
                    s += c + " \n";

            }


            for(Customer c: cl){
                if (c ==  null)
                    sum += 0;
                else
                    sum += c.getGrossSales();

            }

            return s + "\n" +  "Total Gross Sales = " + Double.toString(sum);
        }

        public static CustomerList read(String fn) throws FileNotFoundException {
            Scanner input = new Scanner(new File(fn));
            CustomerList ab = new CustomerList();
            String s;
            input.nextLine();
                while(input.hasNextLine()) {
                    s = input.nextLine();
                    Customer c = new Customer(s);
                    System.out.println(c);
                    ab.add(c);
                }
                input.close();
                return ab;
        }


    //Write is facing the same problems as read. We can't access the toCSV() method from Customer.
    public static boolean write(CustomerList cl, String fn) throws FileNotFoundException {
        boolean a = false;
        PrintWriter output = new PrintWriter(new File(fn));
       // try {
            for (int i = 0; i < cl.size; i++) {
                Customer cd = new Customer();
                cd.toCSV();

         //   }
       // } catch (FileNotFoundException s) {
         //   System.out.println("File does not exist please try again: ");
           // return a;
        } return a = true;
    }

    //I utilized the sort function it said to and assume this will work as expected.
    public void sort() {
        Arrays.sort(cl);
    }


    public int indexOf(int id) {
        for (int i = 0; i < size; i++) {
            if(cl[i].getCustomerID() == id) {
                return i;
            }
        }
        return -1;
    }

    public boolean update(int id, double amt) {
        boolean test;
        int index = indexOf(id);
        if(index == -1) {
            System.out.println("CustomerID not present");
            test = false;
        } else {
            amt += cl[index].getGrossSales();
            test = true;
        }
        return test;
    }
}

我的预期结果是该文件可以打印出customerID:每个人的销售总额,名字,姓氏,地址,城市,州和邮政编码。

我想在我的客户列表类中阅读我的问题;我正在尝试创建一个新字符串以接收数据。

0 个答案:

没有答案