十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
wowo我又写了一半的代码,完成了添加用的是窗口的图形化界面,要么??
创新互联专注于企业营销型网站、网站重做改版、安仁网站定制设计、自适应品牌网站建设、HTML5建站、商城网站建设、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为安仁等各大城市提供网站开发制作服务。
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class AddList {
private String filePath = "";
private String bakPath = "";
private String content = "";
Scanner sc = new Scanner(System.in);
public String readFile(){
content = "";
if (isNull(filePath)) {
System.out.println("文件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileReader fr = null;
try {
if (file.exists()) {
fr = new FileReader(file);
char[] chars = new char[1024];
int n = 0;
while((n = fr.read(chars)) != -1){
String string = new String(chars, 0, n);
content = content + string;
}
} else {
System.out.println("文件不存在");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content;
}
public void writeFile(String path){
File file = new File(path);
FileOutputStream fos = null;
mkDirs(path);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void writeFile(){
if (isNull(filePath)) {
System.out.println("文件存储路径:");
filePath = sc.nextLine();
}
File file = new File(filePath);
FileOutputStream fos = null;
mkDirs(filePath);
try {
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
PrintWriter pw = new PrintWriter(bos, true);
pw.print(content);
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void mkDirs(String filepath){
if (filepath.indexOf("\\") != -1) {
filepath = filepath.replaceAll("\\", "/");
}
int n = filepath.indexOf("//");
String path = filepath.substring(0, n) + "//";
filepath = filepath.substring(filepath.indexOf("//") + 1, filepath.length());
String[] files = filepath.split("/");
for (int i = 0; i files.length - 1; i++) {
path = path + files[i];
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
}
}
public void addImfor(){
System.out.println("--------增加记录---------");
String name = "";
String tel = "";
String email = "";
content = readFile();
while(true){
System.out.println("姓名:");
name = sc.next();
System.out.println("电话:");
tel = sc.next();
System.out.println("Email:");
email = sc.next();
content = content + name + "" + tel + "" + email +"==";
System.out.println("0、Exit 1、继续");
int i = sc.nextInt();
if (i == 0) {
break;
}
}
writeFile();
}
public void deleteImfor(){
System.out.println("---------删除记录---------");
String name = "";
String[] imfors = null;
content = readFile();
while(true){
System.out.println("你要删除的姓名是:");
name = sc.next();
if (content.indexOf(name) != -1) {
imfors = content.split("==");
for (int i = 0; i imfors.length; i++) {
if (imfors[i].indexOf(name) != -1) {
imfors[i] = "";
}
}
import java.util.Arrays;
public class AddressBook
{
private int totalContacts; // 现在通讯录中的总数
private int maxContacts; // 通讯录的最大值
private Contact[] contacts; // 通讯录的array
/**
* Constructor. 设定这个通讯录最初的大小为10人上限.
*/
public AddressBook()
{
totalContacts = 0;
maxContacts = 10;
contacts = new Contact[totalContacts];
}
/**
* 得到一个联系通过通讯录中的名字
*
* @param name
* 要去得到的联系人名字
* @return 这个联系人, 没有的话为null
*/
public Contact getContactByName(String name)
{
for(int i = 0; i totalContacts; i++)
{
Contact c = contacts[i];
if(name.equals(c.getName()))
{
return c;
}
}
return null;
}
/**
* 在这个contacts array的最后给这个通讯录添加联系人.
* 如果联系人已经存在且号码参数不为0的话,联系人的号码会更新或者地址参数不是null的话,地址会更新。.
* 如果totalContacts达到maxContacts的值的话,在新的联系人被添加前,这个contacts
* array的size会乘以双倍并且maxContacts也乘以双倍。
*
* @param contact
* 要加到这个通讯录的联系人
* @return true 如果这个联系人添加成功, 否则 false;
*/
public boolean addContact(Contact contact)
{
for(int i = 0; i contacts.length; i++)
{
Contact ct = contacts[i];
if(ct.equals(contact))
{
if(ct.getNumber() != 0)
{
ct.setNumber(contact.getNumber());
}
if(null != ct.getAddress())
{
ct.setAddress(contact.getAddress());
}
return true;
}
}
if(totalContacts == maxContacts)
{
totalContacts *= 2;
maxContacts *= 2;
}
Contact[] cs = new Contact[totalContacts + 1];
System.arraycopy(contacts, 0, cs, 0, totalContacts);
cs[cs.length - 1] = contact;
contacts = cs;
totalContacts++;
return true;
}
/**
* 检查是否联系人已经存在
*
* @param contact
* 要去检查的contact
* @return true 如果这个联系人被找到,否则false
*/
public boolean contains(Contact contact)
{
for(int i = 0; i totalContacts; i++)
{
if(contacts[i].equals(contact))
{
return true;
}
}
return false;
}
public void printAddressBook()
{
for(int i = 0; i totalContacts; i++)
{
System.out.println(contacts[i]);
}
}
/**
* 在AddressBook中还有一个方法!
* 从通讯录中移除一个指定的联系人,之后把其他的入口左移,这样在联系人间就没有空隙了,
* For example:
* before - {C1, C2, C3, C4, C5, C6, C7, C8, C9, null} remove C6
* after - {C1, C2, C3, C4, C5, C7, C8, C9, null, null}
*
* @param contact
* 要移除的联系人,
* @return true 如果这个联系人被成功移除了,否则false;
*/
public boolean removeContact(Contact contact)
{
if(totalContacts == 0)
{
return false;
}
for(int i = 0; i totalContacts; i++)
{
Contact ct = contacts[i];
if(ct.equals(contact))
{
Contact[] cs = new Contact[totalContacts];
System.arraycopy(contacts, 0, cs, 0, i);
System.arraycopy(contacts, i + 1, cs, i, cs.length - i - 1);
contacts = cs;
totalContacts--;
return true;
}
}
return false;
}
@Override
public String toString()
{
return String.format("AddressBook [totalContacts=%s, maxContacts=%s, contacts=%s]", totalContacts, maxContacts,
Arrays.toString(contacts));
}
}
//--------------------------------------------------
public class Contact
{
private String name;
private long number;
private String address;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public long getNumber()
{
return number;
}
public void setNumber(long number)
{
this.number = number;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
@Override
public boolean equals(Object obj)
{
if(!(obj instanceof Contact))
{
return false;
}
Contact contact = (Contact) obj;
if(this.getName().equals(contact.getName())
this.getAddress().equals(contact.getAddress())
this.getNumber() == contact.getNumber())
{
return true;
}
return false;
}
@Override
public String toString()
{
return String.format("Contact [name=%s, number=%s, address=%s]", name, number, address);
}
}
//---------------------------------------------
public class Test
{
public static void main(String[] args)
{
AddressBook ab = new AddressBook();
for(int i = 0; i 10; i++)
{
Contact contact = new Contact();
int x = i + 1;
contact.setName("C" + x);
contact.setAddress("china");
contact.setNumber(x);
ab.addContact(contact);
}
System.out.println(ab);
Contact c = new Contact();
c.setName("C6");
c.setAddress("china");
c.setNumber(6);
ab.removeContact(c);
System.out.println(ab);
}
}
翻了半天找到以前写的,只写了部分,你可以参考一下
public class Data {
private long stuNumber;
private String stuName;
private long stuScore;
public Data(){}
public Data(long stuNumber,String stuName,long stuScore){
this.stuNumber=stuNumber;
this.stuName=stuName;
this.stuScore=stuScore;
}
public void setStuNumber(long stuNumber){this.stuNumber=stuNumber;}
public void setStuName(String stuName){this.stuName=stuName;}
public void setStuScore(long stuScore){this.stuScore=stuScore;}
public long getStuNumber(){return stuNumber;}
public String getStuName(){return stuName;}
public long getStuScore(){return stuScore;}
}
public interface LinkListIntf {
public void initList();//初始化
public int size();//获得长度
public void insertData(int n,Data listdata);
public void getData(int n);
public void deleteData(int n);
public void printList();
}
public class ListTest {
public static void main(String[] args) {
int i;
Sqlist sq = new Sqlist();
Data pdata;
System.out.println("--------Start----------");
System.out.println("--------Init------------");
sq.initList();
System.out.println("--------Init Over--------");
Scanner sc = new Scanner(System.in);
System.out.println("输入元素:");
for (int m = 0; m 2; m++) {
Data data = new Data();
System.out.print("学号 姓名 分数:");
data.setStuNumber(sc.nextLong());
data.setStuName(sc.next());
data.setStuScore(sc.nextLong());
sq.insertData(sq.size()+1, data);
}
while (true) {
System.out.println("----请选择操作------");
System.out.print("1--插入;2--打印;3--删除;4--查找;5--结束");
int choose = sc.nextInt();
switch (choose){
case 1:
System.out.print("插入位置:");
int n = sc.nextInt();
Data data = new Data();
System.out.print("学号 姓名 分数:");
data.setStuNumber(sc.nextLong());
data.setStuName(sc.next());
data.setStuScore(sc.nextLong());
sq.insertData(n,data);
break;
case 2:
System.out.print("打印结果: ");
sq.printList();
break;
case 3:
System.out.print("请输入要删除的元素的位置: ");
int deleteLocation = sc.nextInt();
sq.deleteData(deleteLocation);
break;
case 4:
System.out.print("请输入要查询的元素的位置: ");
int findLocation = sc.nextInt();
System.out.print("元素内容是: ");
sq.getData(findLocation);
break;
case 5:
return;
}
}
}
}
public class Sqlist implements LinkListIntf{
public static final int MAX_LEN = 100;
public int listLength;
private Data[] listdata = new Data[MAX_LEN];
//初始化顺序表
public void initList(){this.listLength=0;}
public int size(){return this.listLength;}
public void insertData(int n,Data data){
int i;
if(listLengthMAX_LEN)
System.out.println("表满!");
if(n1 || nlistLength+1)
System.out.println("插入位置越界!");
for(i=listLength-1;i=n-1;i--)
listdata[i+1]=listdata[i];
listdata[n-1]=data;
listLength++;
return;
}
public void deleteData(int n){
if(n1 || nlistLength-1){System.out.println("删除位置不正确!");return;}
for(int i=n;ilistLength;i++)
listdata[i-1]=listdata[i];
listLength--;
System.out.println("----删除成功!-----");
return;
}
public void getData(int n){
if(n1 || nlistLength){System.out.println("查找不正确!");}
System.out.println(listdata[n-1].getStuNumber()+" "+listdata[n-1].getStuName()+" "+listdata[n-1].getStuScore());
//return 0;
}
public void printList(){
int i;
System.out.println("表中信息为:");
for(i=0;ilistLength;i++) {
String format = String.format(listdata[i].getStuName()+" "+listdata[i].getStuNumber()+" "+listdata[i].getStuScore());
System.out.println(format);
}
//return 0;
}
}
1)java form,jlable 、textField、button 神马 的 GridBagLayout布局搞一发
2)JDBC 往数据库搞一搞;
3)数据库排序取出,或者取出后代码排个序
4)步骤2已经写入数据库,下次从数据库区,不会丢失
5)搞一个方法,专门用于显示,每计算一次,调用这个方法
另;应该搞个登陆的,管理总要有权限控制的
Friend类:public class Friend {
/*
* 姓名
*/
private String name;
/*
* 电话
*/
private String telephone;
/*
* 邮箱
*/
private String email;
/*
* 公司
*/
private String company; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTelephone() {
return telephone;
} public void setTelephone(String telephone) {
this.telephone = telephone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getCompany() {
return company;
} public void setCompany(String company) {
this.company = company;
} public String toString() {
StringBuffer str = new StringBuffer(); str.append("姓名:" + name).append("\n");
str.append("电话:" + telephone).append("\n");
str.append("邮箱:" + email).append("\n");
str.append("公司:" + company).append("\n");
str.append("-----------------------------------------\n");
return str.toString();
}
}AddFriend类:public class AddFriend { /**
* 主方法 程序的入口
*/
public static void main(String[] args) {
ListFriend friendList = new ArrayListFriend();
char isGo = 'Y';
int i = 0;
do {
Friend friend = new Friend();
System.out.println("请输入第" + (i + 1) + "位朋友的姓名:");
InputStreamReader reader = new InputStreamReader(System.in);
String str = "";
try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setName(str); System.out.println("请输入第" + (i + 1) + "位朋友的电话:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (str.matches("\\d*") str.length() == 11) {// 判断用户输入的电话是否符合标准
friend.setTelephone(str);
} else {
System.out.println("电话号码输入有误,请重新输入!");
continue;
} System.out.println("请输入第" + (i + 1) + "位朋友的邮箱:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setEmail(str); System.out.println("请输入第" + (i + 1) + "位朋友的公司:"); try {
str = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
friend.setCompany(str); friendList.add(friend); i++; System.out.println("是否继续添加?(Y/N):");
String go = "";
try {
go = (new BufferedReader(reader)).readLine();
} catch (IOException e) {
e.printStackTrace();
}
isGo = go.charAt(0);
} while (isGo == 'Y' || isGo == 'y'); for (int j = 0; j friendList.size(); j++) {
System.out.println(friendList.get(j).toString());
}
}
}