Question

(Reading & Writing Business Objects) I need to have my Classes be able to talk to...

(Reading & Writing Business Objects)

I need to have my Classes be able to talk to Files.

How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is:

Account a1 = new Account();

a1.select(“90001”);

a1.display();

Below is what it should look like for accounts

90000:3003:SAV:8855.90

& Below for customers.

3003:6789:Terry:March:Texas:[email protected]

I have some Accounts saved in an Accounts.txt file and Customers in a Cutomers.txt file.

I have my code below.

**********************************************************************************************

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Account2{
//Properties
private int accountNo;
private int customerId;
private String accountType;
private double balance;
//Constructors
public Account2(){
accountNo = 0;
customerId = 0;
accountType = "";
balance = 0.0;
}
public Account2(int acc, int cid, String accType, double bal){
accountNo = acc;
customerId = cid;
accountType = accType;
balance = bal;
}
//Behaviors
public void setAccNo(int acc){
accountNo = acc;
}
public int getAccNo(){
return accountNo;
}
public void setCid(int cid){
customerId = cid;
}
public int getCid(){
return customerId;
}
public void setAccType(String accType){
accountType = accType;
}
public String getAccType(){
return accountType;
}
public void setBal(double bal){
balance = bal;
}
public double getBal(){
return balance;
}
public void display(){
System.out.println("===================== ");
System.out.println("Account Number: " + getAccNo());
System.out.println("Customer ID: " + getCid());
System.out.println("Account Type: " + getAccType());
System.out.println("Balance: $" + getBal());
System.out.println("");
}
public void select(String accNo){   
accountNo = 0;
accountType = "";
balance = 0;
customerId = 0;
try{
String line;
File f1 = new File("Accounts.txt");
Scanner inFile = new Scanner(f1);
while(inFile.hasNextLine()){
line = inFile.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line, ":");
String token = tokenizer.nextToken();
System.out.println(token);
if(token.equals(accNo)){
accountNo = Integer.parseInt(token);
customerId = Integer.parseInt(tokenizer.nextToken());
accountType = tokenizer.nextToken();
balance = Double.parseDouble(tokenizer.nextToken());
break;
}
}
inFile.close();
}
catch(IOException ie){
System.out.println(ie);
}
}
public static void main(String[] args){
Account2 a1 = new Account2();
a1.select("90001");
a1.display();
}
}

*******************************************************************************************

public class Person2{
//Properties
private String firstName;
private String lastName;
private String address;
private String email;
//Constructors
public Person2(){
firstName = "";
lastName = "";
address = "";
email = "";
}
public Person2(String fn, String ln, String addr, String em ){
firstName = fn;
lastName = ln;
address = addr;
email = em;
}
//Behaviors
public void setFn(String fn){
firstName = fn;
}
public String getFn(){
return firstName;
}
public void setLn(String ln){
lastName = ln;
}
public String getLn(){
return lastName;
}
public void setAddr(String addr){
address = addr;
}
public String getAddr(){
return address;
}
public void setEm(String em){
email = em;
}
public String getEm(){
return email;
}
public void display(){
System.out.println("");
System.out.println("First Name: " + getFn());
System.out.println("Last Name: " + getLn());
System.out.println("Address: " + getAddr());
System.out.println("E-Mail: " + getEm());
System.out.println("");
}

public static void main(String[] args){
Person2 p2;
p2 = new Person2("Billy","Carter","Georgia","[email protected]");
p2.display();
Person2 p3;
p3 = new Person2("Tony","Danza","Atlanta","[email protected]");
p3.display();
}
}

******************************************************************************************************

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Customer2 extends Person2{
//Properties
public int password;
public int customerID;
public AccountList2 list;
//Constructors
public Customer2(){
super();
list = new AccountList2();
password = 0;
customerID = 0;
}
public Customer2(int pw, int cid ,String fn, String ln, String addr, String em){
super(fn, ln, addr, em);
password = pw;
customerID = cid;
list = new AccountList2();
}
//Behaviors
public void setPw(int pw){
password = pw;
}
public int getPw(){
return password;
}
public void setList(AccountList2 li){
list = li;
}
public AccountList2 getList(){
return list;
}
public void setCid(int cid){
customerID = cid;
}
public int getCid(){
return customerID;
}
public void addAccount(Account2 acc){
list.addAccount2(acc);
}

public void display(){
System.out.println("");
System.out.println("Password: " + getPw());
System.out.println("Customer ID: " + getCid());
super.display();
list.display();
System.out.println("");
}
public void select(String custId){
try{
customerID = 0;
password = 0;
setFn(null);
setLn(null);
setEm(null);
setAddr(null);
  
String line;
File f1 = new File("Customer.txt");
Scanner inFile = new Scanner(f1);
while(inFile.hasNextLine()){
line = inFile.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line, ":");
String token = tokenizer.nextToken();
System.out.println(token);
if(token.equals(custId)){
customerID = Integer.parseInt(token);
password = Integer.parseInt(tokenizer.nextToken());
setFn(tokenizer.nextToken());
setLn(tokenizer.nextToken());
setAddr(tokenizer.nextToken());
setEm(tokenizer.nextToken());
list = new AccountList2();
Scanner accFile = new Scanner(new File("Accounts.txt"));
while(accFile.hasNextLine())
{
tokenizer = new StringTokenizer(accFile.nextLine(), ":");
String accNo = tokenizer.nextToken(); // acount number
String cid = tokenizer.nextToken();
if(cid.equals(custId))
{
acc.setAccNo(Integer.parseInt(accNo));
acc.setCid(Integer.parseInt(cid));
acc.setAccType(tokenizer.nextToken());
acc.setBal(Double.parseDouble(tokenizer.nextToken()));
list.add(Integer.parseInt(accNo));
}
}
accFile.close();
break;
}
}
inFile.close();
}
catch(IOException ie){
System.out.println(ie);
}   
}
public static void main(String[] args){
Customer2 c2;
c2 = new Customer2();
c2.display();

c2.select("3003");
c2.display();
}
}

******************************************************************************************************************

public class AccountList2{
//Properties
public int count = 0;
public Account2 aList[] = new Account2[4];
//Constructors
public AccountList2(){
}
//Behaviors
public void addAccount2(Account2 a2){
aList[count] = a2;
count++;
}
public void display(){
System.out.println("=====================");
System.out.println("Account List Display");
for(int x = 0; x < count; x++){
aList[x].display();
}
}
public static void main(String[] args){
AccountList2 myList = new AccountList2();
myList.addAccount2(new Account2(90000,3003,"SAV",8855.90));
myList.addAccount2(new Account2(90001,3003,"CHK",786.54));
myList.display();
}
}

*****************************************************************************************************************************

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, Inorder to open your file in a scanner you must follow these steps.

//get the url of your text file
URL path = ClassLoader.getSystemResource("Customer.txt");
// give the path to file
File f = new File(path.toURI());
//open the file in scanner

Scanner inFile = new Scanner(f);

YOUR CODE:

Customer2.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 Accounts;

/**
*
* @author miracle
*/
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Customer2 extends Person2{
//Properties
public int password;
public int customerID;
public AccountList2 list;
//Constructors
public Customer2(){
super();
list = new AccountList2();
password = 0;
customerID = 0;
}
public Customer2(int pw, int cid ,String fn, String ln, String addr, String em){
super(fn, ln, addr, em);
password = pw;
customerID = cid;
list = new AccountList2();
}
//Behaviors
public void setPw(int pw){
password = pw;
}
public int getPw(){
return password;
}
public void setList(AccountList2 li){
list = li;
}
public AccountList2 getList(){
return list;
}
public void setCid(int cid){
customerID = cid;
}
public int getCid(){
return customerID;
}
public void addAccount(Account2 acc){
list.addAccount2(acc);
}
public void display(){
System.out.println("");
System.out.println("Password: " + getPw());
System.out.println("Customer ID: " + getCid());
super.display();
list.display();
System.out.println("");
}
public void select(String custId) throws URISyntaxException{
try{
customerID = 0;
password = 0;
setFn(null);
setLn(null);
setEm(null);
setAddr(null);
Account2 acc=new Account2();
String line;

URL path = ClassLoader.getSystemResource("Customer.txt");
System.out.println("path is "+path);
File f = new File(path.toURI());


// Scanner input = new Scanner(f);
// File f1 = new File("Customer.txt");
Scanner inFile = new Scanner(f);
while(inFile.hasNextLine()){
line = inFile.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line, ":");
String token = tokenizer.nextToken();
System.out.println(token);
if(token.equals(custId)){
customerID = Integer.parseInt(token);
password = Integer.parseInt(tokenizer.nextToken());
setFn(tokenizer.nextToken());
setLn(tokenizer.nextToken());
setAddr(tokenizer.nextToken());
setEm(tokenizer.nextToken());
list = new AccountList2();

URL path1 = ClassLoader.getSystemResource("Accounts.txt");
System.out.println("path1 is "+path);
File f1 = new File(path1.toURI());


// Scanner input = new Scanner(f);
Scanner accFile = new Scanner(f1);
while(accFile.hasNextLine())
{
tokenizer = new StringTokenizer(accFile.nextLine(), ":");
String accNo = tokenizer.nextToken(); // acount number
String cid = tokenizer.nextToken();
if(cid.equals(custId))
{
acc.setAccNo(Integer.parseInt(accNo));
acc.setCid(Integer.parseInt(cid));
acc.setAccType(tokenizer.nextToken());
acc.setBal(Double.parseDouble(tokenizer.nextToken()));
//list.add(Integer.parseInt(accNo));
}
}
accFile.close();
break;
}
}
inFile.close();
}
catch(IOException ie){
System.out.println(ie);
}   
}
public static void main(String[] args) throws URISyntaxException{
Customer2 c2;
c2 = new Customer2();
c2.display();

c2.select("3003");
c2.display();
}
}

Account2.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 Accounts;

/**
*
* @author miracle
*/

import java.io.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Account2{
//Properties
private int accountNo;
private int customerId;
private String accountType;
private double balance;
//Constructors
public Account2(){
accountNo = 0;
customerId = 0;
accountType = "";
balance = 0.0;
}
public Account2(int acc, int cid, String accType, double bal){
accountNo = acc;
customerId = cid;
accountType = accType;
balance = bal;
}
//Behaviors
public void setAccNo(int acc){
accountNo = acc;
}
public int getAccNo(){
return accountNo;
}
public void setCid(int cid){
customerId = cid;
}
public int getCid(){
return customerId;
}
public void setAccType(String accType){
accountType = accType;
}
public String getAccType(){
return accountType;
}
public void setBal(double bal){
balance = bal;
}
public double getBal(){
return balance;
}
public void display(){
System.out.println("===================== ");
System.out.println("Account Number: " + getAccNo());
System.out.println("Customer ID: " + getCid());
System.out.println("Account Type: " + getAccType());
System.out.println("Balance: $" + getBal());
System.out.println("");
}
public void select(String accNo) throws FileNotFoundException, URISyntaxException{   
accountNo = 0;
accountType = "";
balance = 0;
customerId = 0;
try{
String line;

URL path = ClassLoader.getSystemResource("Account.txt");
System.out.println("path is "+path.toString());
File f = new File(path.toURI());
Scanner inFile = new Scanner(f);
while(inFile.hasNextLine()){
line = inFile.nextLine();
StringTokenizer tokenizer = new StringTokenizer(line, ":");
String token = tokenizer.nextToken();
System.out.println(token);
if(token.equals(accNo)){
accountNo = Integer.parseInt(token);
customerId = Integer.parseInt(tokenizer.nextToken());
accountType = tokenizer.nextToken();
balance = Double.parseDouble(tokenizer.nextToken());
break;
}
}
inFile.close();
}
catch(IOException ie){
System.out.println(ie);
}
}
public static void main(String[] args) throws FileNotFoundException, URISyntaxException{
Account2 a1 = new Account2();
a1.select("9000");
a1.display();
}
}

Please do comment in case of queries. Thanks.

Add a comment
Know the answer?
Add Answer to:
(Reading & Writing Business Objects) I need to have my Classes be able to talk to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with adding comments to my code and I need a uml diagram for...

    I need help with adding comments to my code and I need a uml diagram for it. PLs help.... Zipcodeproject.java package zipcodesproject; import java.util.Scanner; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Zipcodesproject { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); BufferedReader reader; int code; String state,town; ziplist listOFCodes=new ziplist(); try { reader = new BufferedReader(new FileReader("C:UsersJayDesktopzipcodes.txt")); String line = reader.readLine(); while (line != null) { code=Integer.parseInt(line); line =...

  • NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the...

    NETBEANS JAVA BANK PROGRAM (TAKE SCREENSHOTS FROM NETBEANS INCLUDING OUTPUT PLEASE) Display the accounts for the current displayed customer PLEASEEEEEEEEEE package bankexample; import java.util.UUID; public class Customer { private UUID id; private String email; private String password; private String firstName; private String lastName;    public Customer(String firstName, String lastName, String email, String password) { this.id = UUID.randomUUID(); this.firstName = firstName; this.lastName = lastName; this.email = email; this.password = password; } public String getFirstName() { return firstName; } public void setFirstName(String...

  • (How do I remove the STATIC ArrayList from the public class Accounts, and move it to...

    (How do I remove the STATIC ArrayList from the public class Accounts, and move it to the MAIN?) import java.util.ArrayList; import java.util.Scanner; public class Accounts { static ArrayList<String> accounts = new ArrayList<>(); static Scanner scanner = new Scanner(System.in);    public static void main(String[] args) { Scanner scanner = new Scanner(System.in);    int option = 0; do { System.out.println("0->quit\n1->add\n2->overwirte\n3->remove\n4->display"); System.out.println("Enter your option"); option = scanner.nextInt(); if (option == 0) { break; } else if (option == 1) { add(); } else...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • JAVA: How do I output all the data included for each employee? I can only get...

    JAVA: How do I output all the data included for each employee? I can only get it to output the name, monthly salary and annual salary, but only from the Employee.java file, not Salesman.java or Executive.java. Employee.java package project1; public class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int getAnnualSalary() { int totalPay = 0; totalPay = 12 * monthlySalary; return totalPay; } public String...

  • Modify the program that you wrote for the last exercise in a file named Baseball9.java that...

    Modify the program that you wrote for the last exercise in a file named Baseball9.java that uses the Player class stored within an array. The program should read data from the file baseball.txt for input. The Player class should once again be stored in a file named Player.java, however Baseball9.java is the only file that you need to modify for this assignment. Once all of the input data from the file is stored in the array, code and invoke a...

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • I need to add a method high school student, I couldn't connect high school student to...

    I need to add a method high school student, I couldn't connect high school student to student please help!!! package chapter.pkg9; import java.util.ArrayList; import java.util.Scanner; public class Main { public static Student student; public static ArrayList<Student> students; public static HighSchoolStudent highStudent; public static void main(String[] args) { int choice; Scanner scanner = new Scanner(System.in); students = new ArrayList<>(); while (true) { displayMenu(); choice = scanner.nextInt(); switch (choice) { case 1: addCollegeStudent(); break; case 2: addHighSchoolStudent(); case 3: deleteStudent(); break; case...

  • FOR JAVA: Summary: Create a program that adds students to the class list (see below). The...

    FOR JAVA: Summary: Create a program that adds students to the class list (see below). The solution should be named Roster402_v2.java. Allow the user to control the number of students added to the roster. Ask if the user would like to see their new roster to confirm additions. If yes, then display contents of the file, if no, end the program. ------------------------------------------------------------------------------------- List of student names and IDs for class (this will be your separate text file): Jones, Jim,45 Hicks,...

  • Below I have my 3 files. I am trying to make a dog array that aggerates...

    Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main {    public static void main(String[] args)    {    System.out.print("There are 5 humans.\n");    array();       }    public static String[] array()    {       //Let the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT