import java.util.Scanner;
import java.io.*;
class Driver {
public static void main(String[]args) throws IOException{
CDCollection music = new CDCollection();
String nameEntered;
boolean runChoices = true;
int input = 0;
String name;
String singer;
double cost;
int numTracks;
Scanner sc = new Scanner(System.in);
System.out.println("******************************************");
System.out.println("*Hello and Welcome to Your CD Collection.*");
System.out.println("******************************************");
while (runChoices = true){
music.menu();
input = sc.nextInt();
if (input == 1){
System.out.println("Enter title of song.");
name = sc.nextLine();
System.out.println("Enter the artist name.");
singer = sc.nextLine();
System.out.println("Enter the cost of CD.");
cost = sc.nextInt();
System.out.println("Enter the number of tracks on the CD.");
numTracks = sc.nextInt();
music.addCD(name, singer, cost, numTracks);
}
else if (input == 2){
//makes deposite and show amount
}
else if (input == 3){
music.showCds();
}
else {
runChoices = false;
System.out.println("***************************************");
System.out.println("*Thank You For Using MyMusic, GoodBye!*");
System.out.println("***************************************");
}
}//Main
}
}//Driver
import java.util.Scanner;
import java.io.*;
public class CDCollection {
private double totalValue;
int currentsize = 30;
private CD [] collection = new CD [ currentsize ];
int count = 0;
public CDCollection () throws IOException{
Scanner textIn = new Scanner (new File ("CDs.txt"));
int i = 0;
while (textIn.hasNext()){
String line = textIn.nextLine();
String [] details = line.split("," , 4);
collection [i] = new CD (details [0] , details [1], Double.parseDouble (details [2]), Integer.parseInt (details [3]));
i++;
}
}//IO Exception CDCollection method
public void showCds(){
for (int i = 0; i < collection.length; i++){
System.out.println(collection[i].toString());
int a = i;
}
}
public void menu(){
System.out.println("Please enter one of the following numbers.");
System.out.println("1. Add new CD.");
System.out.println("2. Sort Collection.");
System.out.println("3. Display Collection.");
System.out.println("4. Exit the System.");
}//menu method
public void addCD (String title, String artist, double value, int tracks){
for (int i = 0; i < collection.length; i++){
if (collection [i]. equals ("")){
collection[i] = new CD (title, artist, value, tracks);
}
}
System.out.println("");
for (int i = 0; i < collection.length; i++){
System.out.println(collection[i].toString());
}
}
public void increaseSize(){
private CD [] temp = new CD [ currentsize * 2 ];
}
}
public class CD{
private String title, artist;
private double value;
private int tracks;
public CD (String title, String artist, double value, int tracks){
this.title = title;
this.artist = artist;
this.value = value;
this.tracks = tracks;
}
public String toString(){
return title + " | " + artist + " | " + value + " | " + tracks;
}
}
ok i have to make a program that wear it asks the user if it wants to see all the cds add a new cd or sort the cds. My problem is that i dont kno how to add something to an array and also if the array is full how i expand it. All i know is that you have to make a new array and copy everything from the old array into there. Could someone please teach me how to do that. I would be most greatful.

New Topic/Question
Reply




MultiQuote








|