1 answer

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

Question:

(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();
}
}

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


Answers

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.

.

Similar Solved Questions

1 answer
7. A fair coin is flipped multiple times until it lands on heads. If the probability...
7. A fair coin is flipped multiple times until it lands on heads. If the probability of landing on ( point) heads is 50%, what is the probability of first landing on heads on the third attempt? ○ 0,096 0.107 o 0.121 00.125...
1 answer
Two point charges of equal magnitude are 8.5 cm apart. At the midpoint of the line...
Two point charges of equal magnitude are 8.5 cm apart. At the midpoint of the line connecting them, their combined electric field has a magnitude of 46 N/C PartA Find the magnitude of the charges. Express your answer using two significant figures. q= pC Submit Previous Answers Request Answer X Incor...
1 answer
The following table presents sales forecasts for Golden Gelt Giftware. The unit price is $40. The...
The following table presents sales forecasts for Golden Gelt Giftware. The unit price is $40. The unit cost of the giftware is $30. Year Unit Sales 36,000 44,000 13,000 7,000 Thereafter It is expected that net working capital will amount to 20% of sales in the following year. For example, the store ...
1 answer
Please explain and the formulas you used to answer the question. The following cost data pertain...
Please explain and the formulas you used to answer the question. The following cost data pertain to the operations of Montgomery Department Stores, Inc., for the month of Corporate legal office salaries Apparel Department cost of sales-Evendale Store Corporate headquarters building lease Store manag...
1 answer
A galvanic cell is assembled from one half cell with a nickel electrode immersed in Ni...
A galvanic cell is assembled from one half cell with a nickel electrode immersed in Ni 2+ aqueous solutions in one beaker where [Ni2+] = 1.2 M coupled to a second half cell with a chromium electrode immersed in a Cr3+ aqueous solution to give Ecell = +0.55 V at 25 °C. 3Ni2+(aq) + 2Cr(s) → 3...
1 answer
Outer mito. membrane Time meme en Saree APP SYNTHAO 1. Peaton Park Zawad Mo Moment Mitociloudere...
outer mito. membrane Time meme en Saree APP SYNTHAO 1. Peaton Park Zawad Mo Moment Mitociloudere merk NADH -- 20 ADP+P. 32 mitochondria UN 1 (FADH co < - > 1. Glycolysis glucose You may receive limited assistance from a classmate, a tutor, or the instructor Name: BIO 121 Introduction to Biolog...
1 answer
Arallel Enterprises has collected the following data on one of its products. During the period the...
arallel Enterprises has collected the following data on one of its products. During the period the company produced 25,000 units. The direct materials price variance is: Direct materials standard (7 kg. @ $1.75/kg.) $12.25 per finished unit Actual cost of materials purchased $275,850 ...
1 answer
Your firm needs to raise $200M for a plant expansion. $60M will be raised by borrowing...
Your firm needs to raise $200M for a plant expansion. $60M will be raised by borrowing money and your firm will use 30-year coupon bonds. You have been asked to complete the estimate of your firm's cost of debt. Your firm has one issue of debt outstanding. The debt has 5 years to maturity, has a...
1 answer
The following are transactions between Larkspur Corp., the consignor, and Crane Stores Ltd., the consignee, for...
The following are transactions between Larkspur Corp., the consignor, and Crane Stores Ltd., the consignee, for the month of June 2020. Larkspur uses a perpetual inventory system and has a separate perpetual record for inventory sent out on consignment. At the end of each month, sales are reported b...
1 answer
What is the general solution of trigonometric equation #sqrt3cotx+1=0#?
What is the general solution of trigonometric equation #sqrt3cotx+1=0#?...
1 answer
15) Reproductive system disorders and conditions Give 2 (two) examples for each of the questions relating...
15) Reproductive system disorders and conditions Give 2 (two) examples for each of the questions relating to the disorders of the reproductive system. Question Answers 15.1) Common symptoms of disorders of the female reproductive system 15.2) Common tumors of the reproductive system 15.3) Common dis...
1 answer
For positive constant a and b, a pair of random variables has joint PDF specified by...
For positive constant a and b, a pair of random variables has joint PDF specified by x.y(a,y)-abe-(ax+by) a. Find the joint CDF, Fx.y (x, y), b. Find the marginal PDFs, fx (x) and fy O) c. Find probability that X > Y d. Find probability that X > Y2...
1 answer
There are usually lots of ways to solve a circuit. Choosing a method should not be...
There are usually lots of ways to solve a circuit. Choosing a method should not be because you only know how to do one well, but rather what is the best (or quickest) for the given situation. Also, you will need to get use to different ways in which circuits are drawn. Solve for all voltages and cur...
1 answer
Compare and contrast the structures of T cell receptors and MHC class II molecules. Relate the...
Compare and contrast the structures of T cell receptors and MHC class II molecules. Relate the differences in structure to the function of the molecules in an immune response. (Min 2 pages)...
1 answer
Mike was recently diagnosed with type 2 diabetes. He is feeling good and has not noticed...
Mike was recently diagnosed with type 2 diabetes. He is feeling good and has not noticed any changes in his health. Sam, his twin, has not been diagnosed with type 2 diabetes. They both like the same activities and foods. Mike does not understand why he has diabetes, but his brother does not and why...
1 answer
The combustion of propane may be described by the chemical equation, C3H8(g) + 5%g) -> 3C02(g)...
The combustion of propane may be described by the chemical equation, C3H8(g) + 5%g) -> 3C02(g) +4H2 How many grams of O2(g) are needed to completely burn 33.5 g of C3H8(g)?...
2 answers
A -17nC charge is located at the origin. 1. What is the strength of the electric...
A -17nC charge is located at the origin. 1. What is the strength of the electric field at the position (x,y)=(0cm,5.0cm)? Express your answer using two significant figures. 2. What is the strength of the electric field at the position (x,y)=(?5.0cm,?5.0cm)? Express your answer using two significant ...
1 answer
For the equation, give the x-intercept and the y-intercept. Then graph the equation. 5x +9y =...
For the equation, give the x-intercept and the y-intercept. Then graph the equation. 5x +9y = 15 matut.com student mew h omeworld S19721&question date d 567507 continyes | Math 15 (1) Aliyah Montgomery Homework: Sec 8.2 Score: 0 of tot و * * * 82.71 The r more 2010 och 2017 Com The slope o...
1 answer
In this era of rapid change and short product lifetimes (e.g. Apple Corp.), what can be...
In this era of rapid change and short product lifetimes (e.g. Apple Corp.), what can be done with process technology to be able to continue to produce several generations of a product? How might this fact enter into the decisions related to capacity/facility planning? Then, based on your discussions...
1 answer
At December 31, 2022, Martinez Corporation reported the following plant assets. Land $ 3,783,000 Buildings $26,590,000...
At December 31, 2022, Martinez Corporation reported the following plant assets. Land $ 3,783,000 Buildings $26,590,000 Less: Accumulated depreciation—buildings 15,037,425 11,552,575 Equipment 50,440,000 Less: Accumulated depreciation—equipment ...