首页 > 解决方案 > 创建一个程序来维护库存产品信息的二进制文件,其记录格式为:{int product_id, int product_quantity}

问题描述

创建一个程序来维护库存中产品信息的二进制文件,其记录格式为:{int product_id, int product_quantity}. 实现导入、设置、列出和导出功能:

  • import {file}: 读取一个文本文件,其中每行包含 2 个用空格分隔的整数,第一个数字代表产品代码,第二个数字代表新报价
  • set {id {{q}:将带有代码的产品的报价更改{id}{q}。如果产品代码不存在,则创建
  • list {id}:打印带有代码的产品报价{id}。如果密码不存在,打印“not found”
  • export {file}:创建与导入文件对应的文本文件。

我在 Python 中的代码可以正常运行,但在 java 中我很难,有人可以帮助我吗?

蟒蛇代码:

def import_file(file):
    with open(file,"r") as f:
        lines = f.readlines()
        sline = []
        for line in lines:
          xline=line.split(" ")
          sline.append(xline)
    return sline        
        
def set_file(idf,q,sline):
    for i in sline:
        if idf in i:
            sline[sline.index(i)][1] = q + "\n"
            return "Change succeeded"
    sline.append([idf,q])
    return "New code added"

def list_file(idf,sline):
    for i in sline:
        if idf in i:
            return sline[sline.index(i)][1]
    return "Not found"       

def export_file(name,sline):
    with open(name,'w') as f:
        for lines in sline:
            f.write(lines[0] + ' ' + lines[1])
                                     
loop = 1
while loop == 1:
    choice=int(input("1:Import file\n 2:Set \n 3:List \n 4:Export file \n 5:Quit \n :"))
    if choice == 1:
        print("Importing file...")
        use=import_file(input("Insert the path of the file:"))
    elif choice == 2:
        print("Setting file...")
        print(set_file(input("Insert id:"),input("Insert quantity:"),use))
    elif choice == 3:
        print("Listing file...")
        print(list_file(input("Insert id:"),use))
        print(use)
    elif choice == 4:
        print("Exporting file...")
        print(export_file(input("Name of the exported file:"),use))
    elif choice == 5:
        print("Goodbye")
        loop = 0
    

标签: pythonjava

解决方案


也许像这样?

import java.util.*;
import java.io.*;

class ImportFile{
  public static String[][] importFile(String fileName){
    ArrayList<String> lines = new ArrayList<String>();
    
    try {
      Scanner sc = new Scanner(new File(fileName));
      while (sc.hasNextLine()) {
        lines.add(sc.nextLine());
      }
      sc.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    
    String[][] slines = new String[lines.size()][];
    
    int count = 0;
    for(String line:lines){
      String[] xline = line.split(" ");
      slines[count] = xline;
      count++;
    }
    
    return slines;
  }
  static void printDoubleArray(String[][] arr){
    System.out.print("[");
    for(int i = 0;i<arr.length;i++ ){
      System.out.print("[");
      for(int j = 0;j<arr[i].length;j++ ){
        System.out.print(arr[i][j]);
        if(j != arr[i].length-1) System.out.print(", ");
      }
      System.out.print("]");
      if(i != arr.length-1) System.out.print(", ");
    }
    System.out.println("]");
  }
  public static void main(String[] args){
    String [][] fileContent = importFile("text.txt");
    printDoubleArray(fileContent);
  }
}

推荐阅读