Skip to content

Commit 85eb2fe

Browse files
committed
Add to git
1 parent de755f2 commit 85eb2fe

File tree

6 files changed

+373
-0
lines changed

6 files changed

+373
-0
lines changed

.idea/uiDesigner.xml

Lines changed: 124 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**
2+
* Fig. 17.3: FileDemonstration
3+
* File class used to obtain file and directory information.
4+
**/
5+
import java.io.File;
6+
import java.util.Scanner;
7+
8+
public class FileDemonstration
9+
{
10+
public static void main(String [] args)
11+
{
12+
Scanner input = new Scanner(System.in);
13+
14+
System.out.print("Enter file or directory name: ");
15+
analyzePath(input.nextLine());
16+
} // end main
17+
18+
// display information about file user specifies
19+
public static void analyzePath(String path)
20+
{
21+
// create File object based on user input
22+
File name = new File(path);
23+
24+
if (name.exists()) // if name exists, output information about it
25+
{
26+
// display file (or directory) information
27+
System.out.printf(
28+
"%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s",
29+
name.getName(), " exists",
30+
(name.isFile() ? "is a file" : "is not a file"),
31+
(name.isDirectory() ? "is a directory" : "is not a directory"),
32+
(name.isAbsolute() ? "is absolute path" : "is not absolute path"),
33+
"Last modified: ",
34+
name.lastModified(), "Lengeth: ", name.length(),
35+
"Path: ", name.getPath(), "Absolute path: ",
36+
name.getAbsolutePath(), "Parent: ", name.getParent());
37+
38+
if (name.isDirectory()) // output directory listing
39+
{
40+
String[] directory = name.list();
41+
System.out.println("\n\nDirectory contents:\n");
42+
43+
for (String directoryName : directory)
44+
System.out.println(directoryName);
45+
} // end if
46+
}
47+
else // not file or directory, output error message
48+
{
49+
System.out.printf("%s %s", path, "does not exist.");
50+
} // end else
51+
} // end method analyzePath
52+
} // end class FileDemonstration
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="jdk" jdkName="1.7" jdkType="JavaSDK" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Fig. 17.4: AccountRecord.java
3+
* AccountRecord class maintains information for one account.
4+
*/
5+
6+
public class AccountRecord
7+
{
8+
private int account;
9+
private String firstName;
10+
private String lastName;
11+
private double balance;
12+
13+
// no-argument constructor calls other constructor with default values
14+
public AccountRecord()
15+
{
16+
this(0, "", "", 0.0); // call four-argument contsructor
17+
}
18+
19+
// initialize a record
20+
public AccountRecord(int acct, String first, String last, double bal)
21+
{
22+
setAccount(acct);
23+
setFirstName(first);
24+
setLastName(last);
25+
setBalance(bal);
26+
} // end four-argument AccountRecord constructor
27+
28+
// set account number
29+
public void setAccount(int acct)
30+
{
31+
account = acct;
32+
} // end method setAccount
33+
34+
// get account number
35+
public int getAccount()
36+
{
37+
return account;
38+
} // end method getAccount
39+
40+
// set first name
41+
public void setFirstName(String first)
42+
{
43+
firstName = first;
44+
} // end method setFirstName
45+
46+
// get first name
47+
public String getFirstName()
48+
{
49+
return firstName;
50+
} // end method getFirstName
51+
52+
// set last name
53+
public void setLastName(String last)
54+
{
55+
lastName = last;
56+
} // end method setLastName
57+
58+
// get last name
59+
public String getLastName()
60+
{
61+
return lastName;
62+
} // end method getLastName
63+
64+
// set balance
65+
public void setBalance(double bal)
66+
{
67+
balance = bal;
68+
} // end method setBalance
69+
70+
// get balance
71+
public double getBalance()
72+
{
73+
return balance;
74+
} // end method getBalance
75+
} // end class AccountRecord
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Fig. 17.5: CreateTextFile.java
3+
* Writing data to a sequential text file with class Formatter.
4+
*/
5+
6+
import java.io.FileNotFoundException;
7+
import java.lang.SecurityException;
8+
import java.util.Formatter;
9+
import java.util.FormatterClosedException;
10+
import java.util.NoSuchElementException;
11+
import java.util.Scanner;
12+
13+
public class CreateTextFile
14+
{
15+
private Formatter output; // object used to output text to file
16+
17+
// enable user to open file
18+
public void openFile()
19+
{
20+
try
21+
{
22+
output = new Formatter("clients.txt"); // open the file
23+
} // end try
24+
catch (SecurityException securityException)
25+
{
26+
System.err.println(
27+
"You do not have write access to this file.");
28+
System.exit(1); // terminate the program
29+
} // end catch
30+
catch (FileNotFoundException fileNotFoundException)
31+
{
32+
System.err.println("Error opening or creating file.");
33+
System.exit(1); // terminate the program
34+
} // end catch
35+
} // end method openFile
36+
37+
// add records to file
38+
public void addRecords()
39+
{
40+
// object to be written to file
41+
AccountRecord record = new AccountRecord();
42+
Scanner input = new Scanner(System.in);
43+
44+
System.out.printf("%s\n%s\n%s\n%s\n\n",
45+
"To terminate input, type the ned-of-file indicator ",
46+
"when you are prompted to enter input.",
47+
"On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
48+
"On Windows type <ctrl> z then press Enter");
49+
50+
System.out.printf("%s\n%s",
51+
"Enter account number (> 0), first name, last name and balance.",
52+
"? ");
53+
54+
while (input.hasNext()) // loop until end-of-file indicator
55+
{
56+
try // output values to file
57+
{
58+
// retrieve data to be output
59+
record.setAccount(input.nextInt());
60+
record.setFirstName(input.next());
61+
record.setLastName(input.next());
62+
record.setBalance(input.nextDouble());
63+
64+
if (record.getAccount() > 0)
65+
{
66+
// write new record
67+
output.format("%d %s %s %.2f\n", record.getAccount(),
68+
record.getFirstName(), record.getLastName(),
69+
record.getBalance());
70+
} // end if
71+
else
72+
{
73+
System.out.println(
74+
"Account number must be greater than 0."
75+
);
76+
} // end else
77+
} // end try
78+
catch (FormatterClosedException formatterClosedException)
79+
{
80+
System.err.println("Error writing to file.");
81+
return;
82+
} // end catch
83+
catch (NoSuchElementException elementException)
84+
{
85+
System.err.println("Invalid input. Please Try again.");
86+
input.nextLine(); // discard input so user can try again
87+
} // end catch
88+
89+
System.out.printf("%s %s\n%s", "Enter account number (>0),",
90+
"first name, last nawe and balance.", "? ");
91+
} // end while
92+
} // end method addRecords
93+
94+
// close file
95+
public void closeFile()
96+
{
97+
if (output != null)
98+
output.close();
99+
} // end method closeFile
100+
} // end class CreateTextFile

0 commit comments

Comments
 (0)