从String [java]创建创建的类的ArrayList

时间:2016-03-05 04:34:30

标签: java class arraylist import

所以我有一个名为MusicSelection的类,我必须用5个字符串参数创建2个MusicSelection的ArrayLists。我有一个存储在play中的.txt文件中的100个字符串列表。我需要使用这100个字符串创建20个MusicSelection对象并将它们存储在lib中。但我正在努力弄清楚如何做到这一点。请帮忙。谢谢!

#include <iostream>
#include <iomanip>
#include <conio.h>
#include <string>
using namespace std;

const double BEST = 6.99,
MEDIUM = 4.59,
BASIC = 3.50,
INSTALL = 129.99;
const int NINE = 9;


int selectCarpet(int type, int unitPrice);

double oneRoom(double pricePerSqYd, int count, double ftLength, double    ftWidth, int numRooms, double ftSq, double ydSq, int squareYd, double materialCost, double unitPrice, double totalCost);

int main()
{
    double ftLength,        // room length in feet
    ftWidth,            // room width in feet
    ftSq,           // square footage
    ydSq,           // square yard
    materialCost,   // carpet material cost
    totalCost,      // material cost plus install
    grandTotal,
    unitPrice;
int squareYd,       // square yards, round off
    type,           // carpet type
    count,
    numRooms;
type = 0;
unitPrice = 0;
double pricePerSqYd,
    oneRoom;

type = selectCarpet(type, unitPrice);

totalCost = 0;
cout << "\nEnter number of rooms: ";
cin >> numRooms;

unitPrice = oneRoom(pricePerSqYd, count, ftLength, ftWidth, ftSq, ydSq, squareYd, materialCost, totalCost, unitPrice);

// step 11
grandTotal = 0;
grandTotal += totalCost;

cout << "\n\nThe grand total price is "
    << grandTotal << endl;

// step 13
do
{
    cout << "\n\t\t*** CARPET INSTALLATION ***\n\n";
    cout << "Select carpet type:\n"
        << "1 - Best Quality, Unit Price $6.99\n"
        << "2 - Medium Quality, unit price $4.59\n"
        << "3 - Basic Quality, Unit price $3.50\n"
        << "4 - exit\n"
        << "Enter your choice --> ";
    cin >> type;
} while (type != 1 && type != 2 && type != 3 && type != 4);

return 0;
}


int selectCarpet(int type, int unitPrice)
{
do
{
    cout << "\n\t\t*** CARPET INSTALLATION ***\n\n";
    cout << "Select carpet type:\n"
        << "1 - Best Quality, Unit Price $6.99\n"
        << "2 - Medium Quality, unit price $4.59\n"
        << "3 - Basic Quality, Unit price $3.50\n"
        << "4 - exit\n"
        << "Enter your choice --> ";
    cin >> type;
} while (type != 1 && type != 2 && type != 3 && type != 4);

while (type != 4)
{
    // step 2
    if (type == 1)  unitPrice = BEST;
    else if (type == 2) unitPrice = MEDIUM;
    else if (type == 3) unitPrice = BASIC;
}

return unitPrice;
}

double oneRoom(double pricePerSqYd, int count, double ftLength, double ftWidth, int numRooms, double ftSq, double ydSq, int squareYd, double materialCost, double unitPrice, double totalCost)
{
    for (count = 0; count < numRooms; count++)
    {
    cout << "Enter room length in feet: ";
    cin >> ftLength;
    cout << "Enter room diwth in feet: ";
    cin >> ftWidth;

    // step 5
    ftSq = ftLength * ftWidth;

    // step 6
    ydSq = ftSq / NINE;

    // step 7
    squareYd = int(ydSq + .5);

    // step 8
    materialCost = squareYd * unitPrice;

    // step 9
    totalCost = materialCost + INSTALL;

    // step 10
    cout << setiosflags(ios::fixed | ios::showpoint)
        << setprecision(2);
    cout << "\n\nSquare footage of the room is: " << ftSq
        << "\nSquare yard of the room is:\t" << ydSq
        << "\nSquare yards priced for: " << squareYd
        << "\nMaterial Cost:\t$" << materialCost
        << "\nInstallation\t$" << INSTALL
        << "\nTotal Cost\t$" << totalCost
        << endl << endl;
}
return pricePerSqYd;
}

1 个答案:

答案 0 :(得分:1)

假设您的文件如下所示:

进步摇滚,Pink Floyd,时间,月亮的黑暗面,1974年 Progressive Rock,Pink Floyd,On the Run,The Dark Side of the Moon,1974

package fileio;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;

public class ReadingFiles {
String g;
String a;
String t;
String al;
String d;
ArrayList<ReadingFiles> pList = new ArrayList<ReadingFiles>();

public ReadingFiles(){

}

public ReadingFiles(String[] ar){
    this(ar[0], ar[1], ar[2], ar[3], ar[4]);
}

public ReadingFiles (String genre, String artist, String title, String album, String date){
    g = genre;
    a = artist;
    t = title;
    al = album;
    d = date;
}

public void addData(ReadingFiles file){
    pList.add(file);
}

public void deserializeFile(){
    try{
        BufferedReader br = new BufferedReader(new FileReader("/Users/droy/var/musicplist.txt"));
        String line = null; 
        while ((line = br.readLine()) != null) {
            String ar[] = line.split(",");
            addData(new ReadingFiles(ar));
        }
    }
    catch (Exception ex){
        ex.printStackTrace();
    }
}


public static void main(String[] args) {
    ReadingFiles file = new ReadingFiles();
    file.deserializeFile();
}
}
相关问题