java读写文件并排序
在D盘跟目录下操作文件根据1.txt中的账户余额和2.txt中的消费日志,重新计算账户余额.(用账户余额减去每笔消费)员工姓名唯一,不会有重名的情况发生.并将余额为0的人...
在D盘跟目录下操作文件
根据1.txt中的账户余额和2.txt中的消费日志,重新计算账户余额.(用账户余额减去每笔消费)
员工姓名唯一, 不会有重名的情况发生.
并将余额为0的人员姓名保存至c.txt.(人员名单按升序排列)
员工账户余额及单次消费额不会超过5000.00
文件格式定义如下:
1.txt: 总计2列, 列分隔符为",". 第一列是姓名, 第二列是账户余额.
2.txt: 总计3列, 第一列为姓名, 第二列为消费项名称, 第三列为消费金额.
3.txt: 员工姓名列表, 每个员工姓名单独一行.
1.txt:
张三,200.01
李四,300.00
...
王五,1000.01
2.txt:
张三,牛肉面,7.01
张三,鸡蛋,2.09
王五,牛肉面,7.01
张三,牛肉面,7.01
李四,冷面,4.11
王五,水煮肉片,20.00
一小时内有效,分可以再给,体现面向对象思想
两小时有效
并将余额为0的人员姓名保存至3.txt 展开
根据1.txt中的账户余额和2.txt中的消费日志,重新计算账户余额.(用账户余额减去每笔消费)
员工姓名唯一, 不会有重名的情况发生.
并将余额为0的人员姓名保存至c.txt.(人员名单按升序排列)
员工账户余额及单次消费额不会超过5000.00
文件格式定义如下:
1.txt: 总计2列, 列分隔符为",". 第一列是姓名, 第二列是账户余额.
2.txt: 总计3列, 第一列为姓名, 第二列为消费项名称, 第三列为消费金额.
3.txt: 员工姓名列表, 每个员工姓名单独一行.
1.txt:
张三,200.01
李四,300.00
...
王五,1000.01
2.txt:
张三,牛肉面,7.01
张三,鸡蛋,2.09
王五,牛肉面,7.01
张三,牛肉面,7.01
李四,冷面,4.11
王五,水煮肉片,20.00
一小时内有效,分可以再给,体现面向对象思想
两小时有效
并将余额为0的人员姓名保存至3.txt 展开
2个回答
展开全部
import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.Scanner;
class Test{
private List<String> name;
private Map<String,Double> balance;
private Map<String,Double> consume;
private Map<String,Double> count;
public List getEmployeeFromBalance(){ //也可以从余额表中获得员工姓名列表
List<String> name=new ArrayList<String>();
Set set=balance.keySet();
Object[] list=set.toArray();
for(int i=0;i<list.length;i++)
name.add((String)list[i]);
this.name=name;
return name;
}
public List readEmployee(String filename)throws Exception{ //读取员工文件
List<String> name=new ArrayList<String>();
File file=new File(filename);
InputStream in=new FileInputStream(file);
Scanner read=new Scanner(in);
String line="";
for(int i=0;;i++){
if(!read.hasNextLine()) break;
line=read.next();
//if(line==null || line.equals("")) break;
name.add(line);
}
in.close();
this.name=name;
return name;
}
public Map readBalanceFile(String filename)throws Exception{ //读取余额文件
Map<String,Double> map=new HashMap<String,Double>();
File file=new File(filename);
InputStream in=new FileInputStream(file);
Scanner read=new Scanner(in);
String line="";
String[] sp=null;
Double balance=null;
for(;;){
if(!read.hasNextLine()) break;
line=read.next();
if(line==null || line=="") break;
sp=line.split(",");
balance=Double.valueOf(sp[1]);
map.put(sp[0],balance);
}
in.close();
this.balance=map;
return map;
}
public Map readConsumeFile(String filename)throws Exception{ //读取消费文件
Map<String,Double> map=new HashMap<String,Double>();
File file=new File(filename);
InputStream in=new FileInputStream(file);
Scanner read=new Scanner(in);
String line="";
String[] sp=null;
Double balance=null;
double temp=0;
for(;;){
if(!read.hasNextLine()) break;
line=read.next();
if(line==null || line.equals("") || line.equals("\n")) break;
sp=line.split(",");
if(map.get(sp[0])!=null){ //解决一人多次消费时Map--key有重复
temp=(Double)(map.get(sp[0])).doubleValue();
temp=temp+Double.valueOf(sp[2]);
balance=Double.valueOf(temp);
temp=0;
}
else balance=Double.valueOf(sp[2]);
map.put(sp[0],balance);
}
in.close();
this.consume=map;
return map;
}
public Map countConsume()throws Exception{ //计算余额
Map<String,Double> count=new HashMap<String,Double>();
double formerly=0,user=0;
for(int i=0;i<name.size();i++){
formerly=Double.parseDouble(balance.get(name.get(i)).toString());
user=Double.parseDouble(consume.get(name.get(i)).toString());
count.put((String)name.get(i),Double.valueOf(formerly-user));
}
this.count=count;
return count;
}
public void writeCountFile(String filename)throws Exception{ //打印结果
File file=new File(filename);
PrintWriter print=new PrintWriter(file);
for(int i=0;i<name.size();i++){
print.print(name.get(i)+",");
print.print(Double.parseDouble(count.get(name.get(i)).toString()));
print.println();
}
print.close();
}
public void writeBalanceZero(String filename)throws Exception{ //将余额为0就打印出来
File file=new File(filename);
PrintWriter print=new PrintWriter(file);
int p=0;
for(int i=0;i<name.size();i++){
if(Double.parseDouble(count.get(name.get(i)).toString())==0){
p++;
print.print(name.get(i)+",");
print.print(Double.parseDouble(count.get(name.get(i)).toString()));
print.println();
}
}
if(p==0) print.println("没有人余额为0");
print.close();
}
static public void main(String[] str)throws Exception{
Test t=new Test();
t.readBalanceFile("1.txt");
t.getEmployeeFromBalance();
t.readConsumeFile("2.txt");
t.countConsume();
t.writeBalanceZero("c.txt");
for(int i=0;i<t.count.size();i++){
System.out.print(t.name.get(i)+",");
System.out.print(t.count.get(t.name.get(i))+"\n");
}
System.out.println("获得结束,结果保存在c.txt");
}
}
///////////////////////////////////
/*
1.txt
王五,1000
李四,2000
张三,3000
2.txt
张三,牛肉面,500
张三,鸡蛋,400
王五,牛肉面,300
张三,牛肉面,400
李四,冷面,600
王五,水煮肉片,100
*/
/////////////////////////////////////
import java.util.*;
import java.lang.*;
import java.util.Scanner;
class Test{
private List<String> name;
private Map<String,Double> balance;
private Map<String,Double> consume;
private Map<String,Double> count;
public List getEmployeeFromBalance(){ //也可以从余额表中获得员工姓名列表
List<String> name=new ArrayList<String>();
Set set=balance.keySet();
Object[] list=set.toArray();
for(int i=0;i<list.length;i++)
name.add((String)list[i]);
this.name=name;
return name;
}
public List readEmployee(String filename)throws Exception{ //读取员工文件
List<String> name=new ArrayList<String>();
File file=new File(filename);
InputStream in=new FileInputStream(file);
Scanner read=new Scanner(in);
String line="";
for(int i=0;;i++){
if(!read.hasNextLine()) break;
line=read.next();
//if(line==null || line.equals("")) break;
name.add(line);
}
in.close();
this.name=name;
return name;
}
public Map readBalanceFile(String filename)throws Exception{ //读取余额文件
Map<String,Double> map=new HashMap<String,Double>();
File file=new File(filename);
InputStream in=new FileInputStream(file);
Scanner read=new Scanner(in);
String line="";
String[] sp=null;
Double balance=null;
for(;;){
if(!read.hasNextLine()) break;
line=read.next();
if(line==null || line=="") break;
sp=line.split(",");
balance=Double.valueOf(sp[1]);
map.put(sp[0],balance);
}
in.close();
this.balance=map;
return map;
}
public Map readConsumeFile(String filename)throws Exception{ //读取消费文件
Map<String,Double> map=new HashMap<String,Double>();
File file=new File(filename);
InputStream in=new FileInputStream(file);
Scanner read=new Scanner(in);
String line="";
String[] sp=null;
Double balance=null;
double temp=0;
for(;;){
if(!read.hasNextLine()) break;
line=read.next();
if(line==null || line.equals("") || line.equals("\n")) break;
sp=line.split(",");
if(map.get(sp[0])!=null){ //解决一人多次消费时Map--key有重复
temp=(Double)(map.get(sp[0])).doubleValue();
temp=temp+Double.valueOf(sp[2]);
balance=Double.valueOf(temp);
temp=0;
}
else balance=Double.valueOf(sp[2]);
map.put(sp[0],balance);
}
in.close();
this.consume=map;
return map;
}
public Map countConsume()throws Exception{ //计算余额
Map<String,Double> count=new HashMap<String,Double>();
double formerly=0,user=0;
for(int i=0;i<name.size();i++){
formerly=Double.parseDouble(balance.get(name.get(i)).toString());
user=Double.parseDouble(consume.get(name.get(i)).toString());
count.put((String)name.get(i),Double.valueOf(formerly-user));
}
this.count=count;
return count;
}
public void writeCountFile(String filename)throws Exception{ //打印结果
File file=new File(filename);
PrintWriter print=new PrintWriter(file);
for(int i=0;i<name.size();i++){
print.print(name.get(i)+",");
print.print(Double.parseDouble(count.get(name.get(i)).toString()));
print.println();
}
print.close();
}
public void writeBalanceZero(String filename)throws Exception{ //将余额为0就打印出来
File file=new File(filename);
PrintWriter print=new PrintWriter(file);
int p=0;
for(int i=0;i<name.size();i++){
if(Double.parseDouble(count.get(name.get(i)).toString())==0){
p++;
print.print(name.get(i)+",");
print.print(Double.parseDouble(count.get(name.get(i)).toString()));
print.println();
}
}
if(p==0) print.println("没有人余额为0");
print.close();
}
static public void main(String[] str)throws Exception{
Test t=new Test();
t.readBalanceFile("1.txt");
t.getEmployeeFromBalance();
t.readConsumeFile("2.txt");
t.countConsume();
t.writeBalanceZero("c.txt");
for(int i=0;i<t.count.size();i++){
System.out.print(t.name.get(i)+",");
System.out.print(t.count.get(t.name.get(i))+"\n");
}
System.out.println("获得结束,结果保存在c.txt");
}
}
///////////////////////////////////
/*
1.txt
王五,1000
李四,2000
张三,3000
2.txt
张三,牛肉面,500
张三,鸡蛋,400
王五,牛肉面,300
张三,牛肉面,400
李四,冷面,600
王五,水煮肉片,100
*/
/////////////////////////////////////
展开全部
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ShopAccount {
ArrayList <customerAccount> customerList = new ArrayList <customerAccount>();
ArrayList <consumeAction> consumeList = new ArrayList <consumeAction> ();
public static void main(String[] args) {
ShopAccount myShop = new ShopAccount();
try {
Scanner inAcount = new Scanner(new FileReader("D:/1.txt"));
Scanner consumeAcount = new Scanner(new FileReader("D:/2.txt"));
while(inAcount.hasNextLine()){
myShop.addCustomer(inAcount.nextLine());
}
System.out.println(myShop.customerList);
while(consumeAcount.hasNextLine()){
myShop.addConsume(consumeAcount.nextLine());
}
for(int i=0;i<myShop.customerList.size();i++){
for(int j=0;j<myShop.consumeList.size();j++){
String name1=myShop.customerList.get(i).getName();
String name2=myShop.consumeList.get(j).getName();
if(name1.equals(name2)){
myShop.customerList.get(i).consume(myShop.consumeList.get(j).getPrice());
}
}
}
for(consumeAction a:myShop.consumeList){
System.out.println(a.toString());
}
System.out.print(myShop.customerList);
try {
FileWriter saver = new FileWriter("D:/3.txt");
for(customerAccount a:myShop.customerList){
if(a.getMoney()<=0){
saver.write(a.toString()+"\n");
}
}
saver.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void addCustomer(String infor){
String information []=infor.split(",");
String name = information [0];
float money = Float.parseFloat(information [1]);
customerList.add(new customerAccount(name,money));
}
public void addConsume(String infor){
String information []=infor.split(",");
String name = information [0];
String itemName = information [1];
float price = Float.parseFloat(information [2]);
consumeList.add(new consumeAction(name,itemName,price));
}
class customerAccount{
String name;
float money;
public customerAccount(String name, float money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public float getMoney() {
return money;
}
public void consume(float pay){
this.money -=pay;
}
public String toString(){
return name+","+money;
}
}
class consumeAction {
String name;
String itemName;
float price;
public consumeAction(String name,String itemName, float price) {
this.name = name;
this.itemName = itemName;
this.price = price;
}
public String getName(){
return name;
}
public float getPrice() {
return price;
}
public String toString(){
return name+","+itemName+","+price;
}
}
}
我的比较像面向对象思想把。。。。
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ShopAccount {
ArrayList <customerAccount> customerList = new ArrayList <customerAccount>();
ArrayList <consumeAction> consumeList = new ArrayList <consumeAction> ();
public static void main(String[] args) {
ShopAccount myShop = new ShopAccount();
try {
Scanner inAcount = new Scanner(new FileReader("D:/1.txt"));
Scanner consumeAcount = new Scanner(new FileReader("D:/2.txt"));
while(inAcount.hasNextLine()){
myShop.addCustomer(inAcount.nextLine());
}
System.out.println(myShop.customerList);
while(consumeAcount.hasNextLine()){
myShop.addConsume(consumeAcount.nextLine());
}
for(int i=0;i<myShop.customerList.size();i++){
for(int j=0;j<myShop.consumeList.size();j++){
String name1=myShop.customerList.get(i).getName();
String name2=myShop.consumeList.get(j).getName();
if(name1.equals(name2)){
myShop.customerList.get(i).consume(myShop.consumeList.get(j).getPrice());
}
}
}
for(consumeAction a:myShop.consumeList){
System.out.println(a.toString());
}
System.out.print(myShop.customerList);
try {
FileWriter saver = new FileWriter("D:/3.txt");
for(customerAccount a:myShop.customerList){
if(a.getMoney()<=0){
saver.write(a.toString()+"\n");
}
}
saver.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void addCustomer(String infor){
String information []=infor.split(",");
String name = information [0];
float money = Float.parseFloat(information [1]);
customerList.add(new customerAccount(name,money));
}
public void addConsume(String infor){
String information []=infor.split(",");
String name = information [0];
String itemName = information [1];
float price = Float.parseFloat(information [2]);
consumeList.add(new consumeAction(name,itemName,price));
}
class customerAccount{
String name;
float money;
public customerAccount(String name, float money) {
this.name = name;
this.money = money;
}
public String getName() {
return name;
}
public float getMoney() {
return money;
}
public void consume(float pay){
this.money -=pay;
}
public String toString(){
return name+","+money;
}
}
class consumeAction {
String name;
String itemName;
float price;
public consumeAction(String name,String itemName, float price) {
this.name = name;
this.itemName = itemName;
this.price = price;
}
public String getName(){
return name;
}
public float getPrice() {
return price;
}
public String toString(){
return name+","+itemName+","+price;
}
}
}
我的比较像面向对象思想把。。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |