Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #1

Open
wants to merge 10 commits into
base: feedback
Choose a base branch
from
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions M(10,5).csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0,1,0,0,0
0,0,0,0,0
0,0,313,0,0
100,0,0,0,0
101,0,0,0,0
0,565,0,431,0
0,556,0,0,0
522,754,0,0,707
Expand Down
Binary file added out/production/phase2-AlirezaSaei1/Main.class
Binary file not shown.
Binary file added out/production/phase2-AlirezaSaei1/Matrix.class
Binary file not shown.
Binary file added out/production/phase2-AlirezaSaei1/Node.class
Binary file not shown.
11 changes: 11 additions & 0 deletions phase2-AlirezaSaei1.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
146 changes: 146 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import java.io.BufferedReader;
import java.io.FileReader;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
static String pathName;
static int rows;
static int columns;
static Matrix matrix;

static Boolean exit = false;
static Boolean read = false;

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (!exit) {
while (true) {
System.out.println("Please Enter Your Path (Format: M(rows,columns).csv): \n(Type \"0\" To Quit Program)");
pathName = sc.nextLine();
if (pathName.equals("0")) {
exit = true;
break;
}
if (ReadMenu(pathName)) {
System.out.println("Reading Successful!");
read = true;
break;
}
}
while (read) {
System.out.println("Main Menu");
System.out.println("0: Insert\n1: Delete\n2: Search\n3: Update\n4: Print\n5: Save\n6: Back");
System.out.println("Your Input: ");
int input = sc.nextInt();
//------------------------------------------------------------------------------------------------------
if (input == 0) {
System.out.println("Enter Row, Column, Value :");
int row = sc.nextInt(), column = sc.nextInt(), value = sc.nextInt();
if (value == 0) {
System.out.println("Do You Want To Insert 0 To Sparse Matrix?!");
} else {
try {
if (!matrix.exist(row, column)) {
matrix.insert(row, column, value);
System.out.println("Value Successfully Inserted!");
} else {
System.out.println("Location Already Has A Value!");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
//--------------------------------------------------------------------------------------------------
} else if (input == 1) {
System.out.println("Enter Row, Column:");
int row = sc.nextInt(), column = sc.nextInt();
if (matrix.exist(row, column)) {
matrix.delete(row, column);
System.out.println("Value Deleted Successfully!");
} else {
System.out.println("There's No Value To Be Deleted!");
}
//--------------------------------------------------------------------------------------------------
} else if (input == 2) {
System.out.println("Enter Value To Look For:");
int x = sc.nextInt();
if (x == 0) {
System.out.println("Bro! This Matrix Is Full Of 0s!!!");
} else {
matrix.search(x);
}
//--------------------------------------------------------------------------------------------------
} else if (input == 3) {
System.out.println("Enter Row, Column, Value :");
int row = sc.nextInt(), column = sc.nextInt(), value = sc.nextInt();
if (matrix.exist(row, column)) {
matrix.update(row, column, value);
} else {
System.out.println("The Value On (" + row + ", " + column + ") is 0!");
}
//--------------------------------------------------------------------------------------------------
} else if (input == 4) {
System.out.println("Print Option:\n1: Print 2D\n2: Print Compressed\n3: Back");
int x = sc.nextInt();
if(x == 1){
matrix.print2D();
}
if(x == 2){
matrix.printCompressed();
}
//--------------------------------------------------------------------------------------------------
} else if (input == 5) {
matrix.save_file(pathName);
//--------------------------------------------------------------------------------------------------
} else if(input == 6){
read = false;
sc.nextLine();
break;
}else{
System.out.println("Please Enter A Valid Number");
}
}
}
}

private static Boolean ReadMenu(String input) {
try {
ArrayList<Integer> size = new ArrayList<>();
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(input);
while (m.find()) {
size.add(Integer.parseInt(m.group()));
}
rows = size.get(size.size() - 2);
columns = size.get(size.size() - 1);

matrix = new Matrix(rows, columns);

try (BufferedReader br = new BufferedReader(new FileReader(input))) {
String line;
int counter = 0;
while ((line = br.readLine()) != null) {
String[] lineItems = line.split(",");
for (int i = 0; i < columns; i++) {
if (!lineItems[i].equals("0")) {
matrix.add(new Node(counter, i, Integer.parseInt(lineItems[i])));
}
}
counter++;
}
} catch (Exception e) {
System.out.println("There Was An Error Reading File, please Try Again!");
return false;
}
return true;
} catch (Exception e) {
System.out.println("Error Occurred! Please Double-Check Your Path!");
return false;
}
}
}

Loading