Combined (vector, lists, file) and counter

0

my problem arises from a matter that I must render in the faculty, I am quite stuck, and it is a subject that I have left aside for this very reason. I achieve progress with the process, but the result never fails me, or I do not know what I'm doing wrong. To render it I need to have an affirmed concepts and I can not solve them. Example: perform a combined counter between list and vector, or record the result of a method in a file. I do not know how to make the structure. I pass them my progress, to see if they can help me. In this matter it is not allowed to use almost predefined methods by the program, but the algorithm must be practically realized in its entirety by one. I know it is not complex, but the truth is that for lack of someone who explained me well and added to having left the matter for so long, it is costing me a lot.

I enclose the statement as an example of what they would take me, plus my progress.

Program used: NetBeans .

Classes: Main, Object, Vector, List, Node, File, In.

Statement:

A construction company needs a program to manage the data of the different works it has in charge. For each work, it is known its identification code (an integer), its description (a string), an entire number between 0 and 49 that indicates the type of work (for example: 0: family house, 1: route, 2: building, etc.) and finally the amount of the budget for the construction of the work.

It is requested to implement the classes that are necessary and a complete program with a menu of options that allows Do the following:

a.) Load data from n works (enter the value of n by keyboard) in a one-dimensional array (vector). b.) Show the data of all the works of the vector, ordered by identification code. c.) Determine which is the work of the vector with the lowest amount budgeted. Show all the data of that work. d.) Generate a simple list, containing the data of all works of the vector whose budgeted amount is greater than the average budgeted amount of the entire vector. The insertion in the list must be done at the beginning of the same. Remark: the insertion method is not provided, and must be developed by the student. e.) Show the data of all the works in the list. f.) Using the list, determine and show the amount of works that exists of each possible type. I mean, you want know how many works exist of type 0, how many of type 1, etc. (a total of 50 meters). g.) Record in a file all the data in the list that was created in point d. Apply the technique at this point of file management that you have seen in classes during the course (serialization, RandomAccessFile, File Streams, etc.) h.) Show the file that was just created in the previous point.

Progress:

MAIN Class:

public static void main (String [] args) {

    int n, codigo, monto, tipo;
    String descrip;
    int opcion = 0;
    int prom = 0;
    Obra o;
    int cont[];



    System.out.println("Bienvenido al Sistema de carga Clientes y Presupuestos: ");
    System.out.println("Cantidad de datos a cargar: ");
    n = In.readInt();
    Vector vec = new Vector(n);
    Lista lis = new Lista();
    Archivo a = new Archivo();

    do {
        System.out.println("Menu de opciones: Seleccione OPCION ");
        System.out.println("1.Cargar datos: ");
        System.out.println("2.Mostrar obra con menor presupuesto: ");
        System.out.println("3.LIsta con monto mayor al promedio: ");
        System.out.println("4.Lista con total de cada tipo: ");
        System.out.println("5. Guardar en un archivo y mostrar: ");

    opcion = In.readInt();

    switch (opcion){
        case 1:
            for(int i = 0; i < n; i++){
        System.out.print("Codigo: ");
        codigo = In.readInt();
        System.out.println("Descripcion: ");
        descrip = In.readString();
        System.out.println("Tipo: ");
        tipo = In.readInt();
        System.out.println("Monto: ");
        monto = In.readInt();
        o = new Obra(codigo, descrip, tipo, monto);
        vec.SetElemento(i, o);
            }
            vec.ordenarAsc();
            System.out.println(vec.toString());
            break;
        case 2:
            System.out.println(vec.menor());
            break;
        case 3:                
            lis = vec.ordPorcMayor();
            System.out.println(lis);
            break;
        case 4:
            cont = lis.conPrio();
            System.out.println("Contador por tipo: ");
            for(int i = 1; i <= 50; i++){
                System.out.println("Tipo: " + i + ":" + cont[i]);
            }
            break;
        case 5:  
 }
    }
    while (opcion != 0);
}   

}

Object Class:

public class Obra {     int code;     String descrip;     int type;     int amount;

public Obra(int codigo, String descrip, int tipo, int monto) {
    this.codigo = codigo;
    this.descrip = descrip;
    this.tipo = tipo;
    this.monto = monto;
}

public int getCodigo() {
    return codigo;
}

public void setCodigo(int codigo) {
    this.codigo = codigo;
}

public String getDescrip() {
    return descrip;
}

public void setDescrip(String descrip) {
    this.descrip = descrip;
}

public int getTipo() {
    return tipo;
}

public void setTipo(int tipo) {
    this.tipo = tipo;
}

public int getMonto() {
    return monto;
}

public void setMonto(int monto) {
    this.monto = monto;
}

@Override
public String toString() {
    return "Obra{" + "codigo=" + codigo + ", descrip=" + descrip + ", tipo=" + tipo + ", monto=" + monto + '}';
}    

}

Class Vector:

public class Vector {

public Obra vec[];

public Vector(int n){
    vec = new Obra[n];  
}

public void SetElemento(int pos, Obra info){
    vec[pos] = info;
}

public void ordenarAsc() {  
    for (int i = 0; i < vec.length - 1; i++) {
        for (int j = i + 1; j < vec.length; j++) {
            if (vec[i].getCodigo() > vec[j].getCodigo()) {
                Obra aux = vec[i];
                vec[i] = vec[j];
                vec[j] = aux;
            }
        }
    }
}

@Override
public String toString() {
    String res = "";
    for(int i = 0; i < vec.length; i++){
        res = res + "\n" + vec[i];
    }
    return res;
}

public Obra menor(){
    Obra o = vec[0];
    for (int i = 1; i < vec.length; i++){
        if (vec[i].getMonto() < o.getMonto()){
        o = vec[i];
    }}
    return o;
}    


public Lista ordPorcMayor(){
    Lista lis = new Lista();
    int prom;
    int sum = 0;
    for (int i = 0; i < vec.length; i++){
        sum = sum + vec[i].getMonto();
        prom = sum / vec.length;
        if (vec[i].getMonto() > prom){
            lis.setPrimero((Obra) vec[i]);
        }            
    }
return lis;
}  

}
}

Class List:

public class List {

private Nodo raiz;

public Lista(){
    raiz = null;
}

public void setPrimero(Obra info){
       Nodo nuevo = new Nodo(info, null);
    if (raiz == null){
        raiz = nuevo;
    } else {                    
    nuevo.setProx(raiz);
    raiz = nuevo;
} 

}

@Override
public String toString(){
    String res = "";
    for (Nodo p = raiz; p != null; p = p.getProx()){
        res = res + p.getInfo().toString();
    }
    return res;
}


public int[] conPrio(){
    int vec[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    for (Nodo p = raiz; p != null; p = p.getProx()){
        vec[p.getInfo().getTipo()]++;
    }
    return vec;
}    

}

Class File:

import java.io. *;

public class File {

private File file;
private FileOutputStream fos;
private ObjectOutputStream oos;

public Archivo (){
    file = new File("obra.dat");
}    

public void abrir(){
    try{
    fos = new FileOutputStream(file);
        oos = new ObjectOutputStream(fos);
    } catch (IOException ex) {
        System.out.println("Error abriendo archivo " + ex.getMessage());
    }
}


public void grabar(Obra o){
    try {
        oos.writeObject(o);
    } catch (IOException ex) {
        System.out.println("Error grabando en archivo " + ex.getMessage());
    }
}


public void cerrar(){
    try {
        oos.close();
        fos.close();
    } catch (IOException ex) {
        System.out.println("Error cerrando archivo " + ex.getMessage());
    }
}


@Override
public String toString() {
    String str = "";
    try {
        FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        while (fis.available() > 0) {
            Obra obj = (Obra) ois.readObject();
            str = str + obj.toString();
        }
        ois.close();
        fis.close();
    } 
    catch (ClassNotFoundException ex) {
        System.out.println("Error clase no encontrada " + ex.getMessage());
    }
    catch (IOException ex) {
        System.out.println("Error leyendo archivo " + ex.getMessage());
    }
    return str;
}

My problem lies in the points: D, F and G

Since you can do something, but either throw me an error, or the result is invalid.

Thank you very much, any contribution is welcome and I find it very useful because my progress in my career is depending on this subject. Thanks.

    
asked by Nicolas Figueroa 08.02.2018 в 20:14
source

0 answers