急,要求Java全部代码,经测试通过后,马上给分,分数不够可以加。要求如下,现C盘有一test.txt文件,里面有一些部门信息如下(现有两个部门,分别为0001,0002):
0001
13/5 People St.
Beijin
21500
10
135
Vancant0002
15B Nanjin Road
Shanghai
215330
15
160
Kingsoft要求1,提供用户查询功能:
当用户输入ID号查询时,将所属的下面记录显示出来。例如,用户查询0002。将得到一下结果:
0002
15B Nanjin Road
Shanghai
215330
15
160
Kingsoft要求2,提供用户可以修改信息功能: (1)用户可以添加新的部门记录(一个部门包含7条记录,添加部门时,部门ID不能重复,并且要比 和前面的ID成升序);
(2)用户可以删除整个部门记录;
(3)用户可以修改部门中的某条记录。
(4)以上操作都需要让用户确认才能生效,也就是说要提供一个save功能,当添加一条记录后,输 入save,才将记录添加到test.txt文件中。注:不能使用MySql等工具。
以下是我写的部分程序:
//*****************Main and a part interface
import java.io.*;
import java.util.*;public class Main {
public static void main(String[] args) {
String txtfile = "c:/sampledb.txt";
File samplefile = new File(txtfile);
if (!samplefile.exists()) // if sampledb does not exist, create it.
{
try {
FileWriter newfile = new FileWriter(txtfile);
} catch (IOException iox) {
System.err.println(iox);
}
} else {
File logFile = new File(samplefile, txtfile);
if (!logFile.exists()) { Scanner myScanner = new Scanner(System.in);
System.out.println("r: read file");
System.out.println("p: print apartment");
System.out.println("a: add records");
System.out.println("d: delect records");
System.out.println("m: modify records");
System.out.print(">"); char c = myScanner.nextLine().charAt(0);
switch (c) {
case 'r': //Q1:read database
DBReader myReader = new DBReader();
myReader.readDB(txtfile);
break;
case 'p': //Q2:search database
System.out.println("Pleas Input ID:");
int input = myScanner.nextInt();
Apartment myApartment = new Apartment(input);
myApartment.print(System.out);
break;
case 'a':
RecordOption add = new RecordOption();
add.addfuction();
break;
case 'd': break;
case 'm':
break;
default:
System.out.println("Invalid");
System.exit(0);
}
}
}
}
}//**************Read text file*************************
import java.io.*;public class DBReader
{ private String filename;
public String DBaReader(String filename_p)
{
filename = filename_p;
return filename;
} public int readDB(String Dblistfile)
{
String curLine = null;
try
{
File myFile = new File(filename);
FileReader freader = new FileReader(myFile);
BufferedReader buf = new BufferedReader(freader);
while ((curLine = buf.readLine()) != null)
{ System.out.println (curLine);
}
buf.close();
freader.close();
}
catch (FileNotFoundException fnf)
{
System.err.println("Exception: File not found");
fnf.printStackTrace();
return 1;
}
catch (IOException ioe)
{
System.err.println("Exception: IO Exception");
ioe.printStackTrace();
return 1;
}
catch (Exception e)
{
System.err.println("Error: Unexpected Exception");
e.printStackTrace();
return -1;
}
return 0;
}
}//************Apartment info(查询部门信息) *******************
import java.io.*;
import java.util.*;public class Apartment { private int id;
String txtFile = "c:/sampledb.txt";
public Apartment(int id_) {
id = id_;
} /**
* Prints Apartment information to the given PrintStream.
* It can print to streams other than System.out
*/
public void print(PrintStream ps) {
File f = new File(txtFile);
try {
String ch = null;
BufferedReader tp = new BufferedReader(new FileReader(f));
String tep = tp.readLine();
int temp = Integer.parseInt(tep);//??????????????????????????????????这里有很多问题我不能解决
while ((ch = tp.readLine()) != null) {//????????????????????????????????????????????????
System.out.print(id);
if (temp==id){
System.out.print("4");
for (int i = 7; i > 0; i--) {
ps.println(tp.readLine());
}
tp.close();
}
} } catch (IOException e1) {
System.out.println("Couldn't read " + txtFile);
} }
}//********************添加,删除修改记录***************************import java.util.*;
import java.io.*;
import java.io.PrintWriter;public class RecordOption { static String data[] = new String[999999];
String filename = "c:/sampledb.txt"; public RecordOption() {
} public void addfuction() {
// add record
Scanner myScanner = new Scanner(System.in);
System.out.println("Recod add function:");
try { FileOutputStream f = new FileOutputStream(filename, true);
String nextLine = System.getProperty("line.separator"); // new line
System.out.println("Pls input ID:");
String nid1 = Integer.toString(myScanner.nextInt());
data[1] = nid1; data[9] = nid1;// System.out.println("Pls input Address:");
// Scanner maScanner = new Scanner(System.in);
// String nid2 = Integer.toString(maScanner.nextInt());
// data[2] = nid2;
// data[9] = data[1] + data[1];
//*****************这里有很多问题没有解决,现在只能添加一条记录,还不能同时添加整个部门的
//**7条记录
try {
System.out.println("Are u confirm to save? Y/N -");
Scanner maScanner = new Scanner(System.in);
char s = maScanner.nextLine().charAt(0);
if (s == 'Y' || s == 'y') { FileWriter filewriter = new FileWriter(filename, true);
filewriter.write(data[9] + nextLine);
filewriter.close();
System.out.println("Add Successful:");
} else if (s == 'N' || s == 'n') {
System.out.println("Nothing be changed");
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} }
}
0001
13/5 People St.
Beijin
21500
10
135
Vancant0002
15B Nanjin Road
Shanghai
215330
15
160
Kingsoft要求1,提供用户查询功能:
当用户输入ID号查询时,将所属的下面记录显示出来。例如,用户查询0002。将得到一下结果:
0002
15B Nanjin Road
Shanghai
215330
15
160
Kingsoft要求2,提供用户可以修改信息功能: (1)用户可以添加新的部门记录(一个部门包含7条记录,添加部门时,部门ID不能重复,并且要比 和前面的ID成升序);
(2)用户可以删除整个部门记录;
(3)用户可以修改部门中的某条记录。
(4)以上操作都需要让用户确认才能生效,也就是说要提供一个save功能,当添加一条记录后,输 入save,才将记录添加到test.txt文件中。注:不能使用MySql等工具。
以下是我写的部分程序:
//*****************Main and a part interface
import java.io.*;
import java.util.*;public class Main {
public static void main(String[] args) {
String txtfile = "c:/sampledb.txt";
File samplefile = new File(txtfile);
if (!samplefile.exists()) // if sampledb does not exist, create it.
{
try {
FileWriter newfile = new FileWriter(txtfile);
} catch (IOException iox) {
System.err.println(iox);
}
} else {
File logFile = new File(samplefile, txtfile);
if (!logFile.exists()) { Scanner myScanner = new Scanner(System.in);
System.out.println("r: read file");
System.out.println("p: print apartment");
System.out.println("a: add records");
System.out.println("d: delect records");
System.out.println("m: modify records");
System.out.print(">"); char c = myScanner.nextLine().charAt(0);
switch (c) {
case 'r': //Q1:read database
DBReader myReader = new DBReader();
myReader.readDB(txtfile);
break;
case 'p': //Q2:search database
System.out.println("Pleas Input ID:");
int input = myScanner.nextInt();
Apartment myApartment = new Apartment(input);
myApartment.print(System.out);
break;
case 'a':
RecordOption add = new RecordOption();
add.addfuction();
break;
case 'd': break;
case 'm':
break;
default:
System.out.println("Invalid");
System.exit(0);
}
}
}
}
}//**************Read text file*************************
import java.io.*;public class DBReader
{ private String filename;
public String DBaReader(String filename_p)
{
filename = filename_p;
return filename;
} public int readDB(String Dblistfile)
{
String curLine = null;
try
{
File myFile = new File(filename);
FileReader freader = new FileReader(myFile);
BufferedReader buf = new BufferedReader(freader);
while ((curLine = buf.readLine()) != null)
{ System.out.println (curLine);
}
buf.close();
freader.close();
}
catch (FileNotFoundException fnf)
{
System.err.println("Exception: File not found");
fnf.printStackTrace();
return 1;
}
catch (IOException ioe)
{
System.err.println("Exception: IO Exception");
ioe.printStackTrace();
return 1;
}
catch (Exception e)
{
System.err.println("Error: Unexpected Exception");
e.printStackTrace();
return -1;
}
return 0;
}
}//************Apartment info(查询部门信息) *******************
import java.io.*;
import java.util.*;public class Apartment { private int id;
String txtFile = "c:/sampledb.txt";
public Apartment(int id_) {
id = id_;
} /**
* Prints Apartment information to the given PrintStream.
* It can print to streams other than System.out
*/
public void print(PrintStream ps) {
File f = new File(txtFile);
try {
String ch = null;
BufferedReader tp = new BufferedReader(new FileReader(f));
String tep = tp.readLine();
int temp = Integer.parseInt(tep);//??????????????????????????????????这里有很多问题我不能解决
while ((ch = tp.readLine()) != null) {//????????????????????????????????????????????????
System.out.print(id);
if (temp==id){
System.out.print("4");
for (int i = 7; i > 0; i--) {
ps.println(tp.readLine());
}
tp.close();
}
} } catch (IOException e1) {
System.out.println("Couldn't read " + txtFile);
} }
}//********************添加,删除修改记录***************************import java.util.*;
import java.io.*;
import java.io.PrintWriter;public class RecordOption { static String data[] = new String[999999];
String filename = "c:/sampledb.txt"; public RecordOption() {
} public void addfuction() {
// add record
Scanner myScanner = new Scanner(System.in);
System.out.println("Recod add function:");
try { FileOutputStream f = new FileOutputStream(filename, true);
String nextLine = System.getProperty("line.separator"); // new line
System.out.println("Pls input ID:");
String nid1 = Integer.toString(myScanner.nextInt());
data[1] = nid1; data[9] = nid1;// System.out.println("Pls input Address:");
// Scanner maScanner = new Scanner(System.in);
// String nid2 = Integer.toString(maScanner.nextInt());
// data[2] = nid2;
// data[9] = data[1] + data[1];
//*****************这里有很多问题没有解决,现在只能添加一条记录,还不能同时添加整个部门的
//**7条记录
try {
System.out.println("Are u confirm to save? Y/N -");
Scanner maScanner = new Scanner(System.in);
char s = maScanner.nextLine().charAt(0);
if (s == 'Y' || s == 'y') { FileWriter filewriter = new FileWriter(filename, true);
filewriter.write(data[9] + nextLine);
filewriter.close();
System.out.println("Add Successful:");
} else if (s == 'N' || s == 'n') {
System.out.println("Nothing be changed");
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
} }
}
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;public final class DepartmentInformation {
private static final String DATA_FILE_NAME = "D:/test.txt"; private Hashtable<String, DepartmentInfo> departments; public DepartmentInformation() throws Exception {
this.departments = new Hashtable<String, DepartmentInfo>();
this.readRecord();
} public static void main(String[] args) {
try {
DepartmentInformation departmentInformation = new DepartmentInformation();
String ID = null;
String addr = null;
String info3 = null;
String info4 = null;
String info5 = null;
String info6 = null;
String info7 = null;
Scanner scanner = new Scanner(System.in);
System.out.println("p: print apartment");
System.out.println("a: add records");
System.out.println("d: delect records");
System.out.println("m: modify records");
System.out.print("> "); char c = scanner.nextLine().charAt(0);
switch (c) {
case 'p':
System.out.println("Please input ID:");
ID = scanner.nextLine();
departmentInformation.print(ID);
break;
case 'a':
System.out.println("Please input ID:");
ID = scanner.nextLine();
if (departmentInformation.find(ID)) {
System.out.println("The department(id =" + ID
+ ") is already existed.");
} else {
System.out.println("Please input address:");
addr = scanner.nextLine();
System.out.println("Please input info3:");
info3 = scanner.nextLine();
System.out.println("Please input info4:");
info4 = scanner.nextLine();
System.out.println("Please input info5:");
info5 = scanner.nextLine();
System.out.println("Please input info6:");
info6 = scanner.nextLine();
System.out.println("Please input info7:");
info7 = scanner.nextLine();
if (departmentInformation.add(ID, addr, info3, info4,
info5, info6, info7)) {
departmentInformation.save();
}
}
break;
case 'd':
System.out.println("Please input ID:");
ID = scanner.nextLine();
if (departmentInformation.delete(ID)) {
departmentInformation.save();
System.out.println("Delete successfully.");
} else {
System.out.println("Not found.");
}
break;
case 'm':
System.out.println("Please input ID:");
ID = scanner.nextLine();
departmentInformation.print(ID);
if (departmentInformation.find(ID)) {
DepartmentInfo departmentInfo = departmentInformation
.getDepartInfo(ID);
System.out
.println("Please input address(Press <Enter> only not to modify):");
addr = scanner.nextLine();
if (!addr.equals("")) {
departmentInfo.setAddress(addr);
}
System.out
.println("Please input info3(Press <Enter> only not to modify):");
info3 = scanner.nextLine();
if (!info3.equals("")) {
departmentInfo.setInfo3(info3);
}
System.out
.println("Please input info4(Press <Enter> only not to modify):");
info4 = scanner.nextLine();
if (!info4.equals("")) {
departmentInfo.setInfo4(info4);
}
System.out
.println("Please input info5(Press <Enter> only not to modify):");
info5 = scanner.nextLine();
if (!info5.equals("")) {
departmentInfo.setInfo5(info5);
}
System.out
.println("Please input info6(Press <Enter> only not to modify):");
info6 = scanner.nextLine();
if (!info6.equals("")) {
departmentInfo.setInfo6(info6);
}
System.out
.println("Please input info7(Press <Enter> only not to modify):");
info7 = scanner.nextLine();
if (!info7.equals("")) {
departmentInfo.setInfo7(info7);
}
departmentInformation.save();
System.out.println("Modify successfully.");
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
} public boolean find(String id) {
return this.departments.containsKey(id);
} public boolean add(String id, String addr, String info3, String info4,
String info5, String info6, String info7) {
if (this.departments.containsKey(id)) {
System.out.println("The new department(id =" + id
+ ") is already existed.");
return false;
} else {
DepartmentInfo departmentInfo = new DepartmentInfo(id);
departmentInfo.setAddress(addr);
departmentInfo.setInfo3(info3);
departmentInfo.setInfo4(info4);
departmentInfo.setInfo5(info5);
departmentInfo.setInfo6(info6);
departmentInfo.setInfo7(info7);
this.departments.put(id, departmentInfo);
System.out.println("Add successfully.");
return true;
}
} public void print(String id) {
if (this.departments.containsKey(id)) {
DepartmentInfo departmentInfo = this.departments.get(id);
System.out.println(departmentInfo.getId() + "\n"
+ departmentInfo.getAddress() + "\n"
+ departmentInfo.getInfo3() + "\n"
+ departmentInfo.getInfo4() + "\n"
+ departmentInfo.getInfo5() + "\n"
+ departmentInfo.getInfo6() + "\n"
+ departmentInfo.getInfo7() + "\n\n");
} else {
System.out.println("Not found.");
}
} public boolean delete(String id) {
if (this.departments.containsKey(id)) {
this.departments.remove(id);
return true;
}
return false;
} public void save() throws Exception {
if (this.departments.size() > 0) {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(
DepartmentInformation.DATA_FILE_NAME));
Enumeration<String> allKeys = this.departments.keys();
String key;
DepartmentInfo departmentInfo;
while (allKeys.hasMoreElements()) {
key = allKeys.nextElement();
departmentInfo = this.departments.get(key);
bufferedWriter.write(departmentInfo.getId() + "\n");
bufferedWriter.write(departmentInfo.getAddress() + "\n");
bufferedWriter.write(departmentInfo.getInfo3() + "\n");
bufferedWriter.write(departmentInfo.getInfo4() + "\n");
bufferedWriter.write(departmentInfo.getInfo5() + "\n");
bufferedWriter.write(departmentInfo.getInfo6() + "\n");
bufferedWriter.write(departmentInfo.getInfo7() + "\n\n");
bufferedWriter.flush();
}
bufferedWriter.close();
}
} private void readRecord() throws Exception {
BufferedReader bufferedReader = new BufferedReader(new FileReader(
DepartmentInformation.DATA_FILE_NAME));
DepartmentInfo departmentInfo = null;
String key = null;
String readingLine;
int lineCounter = 0;
while ((readingLine = bufferedReader.readLine()) != null) {
lineCounter++;
switch (lineCounter) {
case 1:
key = readingLine;
departmentInfo = new DepartmentInfo(readingLine);
break;
case 2:
departmentInfo.setAddress(readingLine);
break;
case 3:
departmentInfo.setInfo3(readingLine);
break;
case 4:
departmentInfo.setInfo4(readingLine);
break;
case 5:
departmentInfo.setInfo5(readingLine);
break;
case 6:
departmentInfo.setInfo6(readingLine);
break;
case 7:
departmentInfo.setInfo7(readingLine);
break;
case 8:
if (!this.departments.containsKey(key)) {
this.departments.put(key, departmentInfo);
}
lineCounter = 0;
break;
}
}
bufferedReader.close();
} protected DepartmentInfo getDepartInfo(String id) {
if (this.departments.containsKey(id)) {
return this.departments.get(id);
}
return null;
}
}
class DepartmentInfo {
private String id; private String address; private String info3; private String info4; private String info5; private String info6; private String info7; public DepartmentInfo(String id) {
this.id = id;
} public String getAddress() {
return this.address;
} public void setAddress(String address) {
this.address = address;
} public String getInfo3() {
return this.info3;
} public void setInfo3(String info3) {
this.info3 = info3;
} public String getInfo4() {
return this.info4;
} public void setInfo4(String info4) {
this.info4 = info4;
} public String getInfo5() {
return this.info5;
} public void setInfo5(String info5) {
this.info5 = info5;
} public String getInfo6() {
return this.info6;
} public void setInfo6(String info6) {
this.info6 = info6;
} public String getInfo7() {
return this.info7;
} public void setInfo7(String info7) {
this.info7 = info7;
} public String getId() {
return this.id;
}
}
0001
13/5 People St.
Beijin
21500
10
135
Vancant 但是,如果我要找0002就找不到了
0002
15B Nanjin Road
Shanghai
215330
15
160
Kingsoft问题二,add 功能不对,我需要在原有记录后面添加记录,但是以上代码却用输入的记录把原有记录全部覆盖掉了。问题三,delete 功能没有用阿。并没有把.txt文件里的相关记录删除掉。问题四,modify 功能作的和add一样了,新输入的东西覆盖了原有所有记录。请问还能改进吗?
呵呵,一定是.txt文件。
文件记录有以下特征:每个“部门“都由7条记录组成,其中第一条记录是ID,肯定是Integer。
“部门“和“部门”间的记录有空行间隔。谁能给出全功能的代码?谢谢!
问题二,add 功能不对,我需要在原有记录后面添加记录,但是以上代码却用输入的记录把原有记录全部覆盖掉了。
这个其实很简单,FileOutputStream(File file, boolean append)将append设为true,就可以在文件末尾添加东西