📄 drawfiles.java
字号:
import java.io.*;
import java.util.*;
public class DrawFiles {
public static void underLinedPrint( String value ){
System.out.println(value);
for (int i=0;i<value.length();i++){
System.out.print("=");
}
}// end of underLinePrint method
private static int promptAndReadValue( String value ) {
while (true){
int length;
UserInput.prompt("Enter the "+value+":");
length=UserInput.readInt();
if (length<0){
System.out.println("The value of "+value+" must be positive!");
continue;
}
else {
return length;
}
}// end of while
}// end of promptAndReadVAlue method
private static String promptAndReadFileName (String value) {
while(true) {
UserInput.prompt("Enter the "+value+" file name:");
String name=UserInput.readString();
File inputFile=new File(name);
if(!inputFile.exists()) {
System.out.println("Could not find file named "+name);
} else {
return name;
}
} //end of while
} //end of promptAndReadFileName method
private static void runMainMenu(){
underLinedPrint("Main Menu");
System.out.println("\nH) Create Horizontal Line");
System.out.println("V) Create Vertical Line");
System.out.println("S) Create Square");
System.out.println("R) Create Rectangle");
System.out.println("F) Create from file");
System.out.println("Q) Quit");
System.out.print("Enter choice:");
}// end of runMainMenu method
private static void runOptionsMenu( String name ){
underLinedPrint("\nOptions Menu");
System.out.println("\n1) Print "+name+"'s properties");
System.out.println("2) Draw "+name+" to screen");
System.out.println("3) Draw "+name+" to file");
System.out.println("4) Return to main menu");
System.out.print("Enter choice:");
}// end of runOptionMenu method
private static Line createLine( boolean isHorizontal ){
int linelength=promptAndReadValue("length");
Line newline=new Line(isHorizontal, linelength);
return newline;
}
private static void processLine( Line myLine ) {
while (true){
runOptionsMenu(myLine.getName());
int choice=UserInput.readInt();
switch (choice){
case 1:
myLine.print();
break;
case 2:
drawToScreen(myLine.toASCIIDrawing());
break;
case 3:
drawToFile(myLine.toASCIIDrawing());
break;
case 4:
return;
}
}
}
private static Rectangle createRectangle( boolean isSquare ){
int height;
int width = promptAndReadValue("width");
if (isSquare) {
height=width;
}
else{
height=promptAndReadValue("height");
}
Rectangle newRectangle = new Rectangle(isSquare,width,height);
return newRectangle;
}
private static void processRectangle (Rectangle myRectangle) {
while (true){
runOptionsMenu(myRectangle.getName());
int choice=UserInput.readInt();
switch (choice){
case 1:
myRectangle.print();
break;
case 2:
drawToScreen(myRectangle.toASCIIDrawing());
break;
case 3:
drawToFile(myRectangle.toASCIIDrawing());
break;
case 4:
return;
}
}
}
private static void createFromFile() {
try {
String txtname=promptAndReadFileName("input");
FileReader fr1=new FileReader(txtname);
BufferedReader br1=new BufferedReader(fr1);
String s;
while ((s=br1.readLine())!=null) {
parseLine(s);
}
fr1.close();
} catch(FileNotFoundException e) {
System.out.println(e.getMessage());
}catch(IOException e) {
System.out.println(e.getMessage());
} // end try...catch
} //end createFromFile method
private static void parseLine(String line) {
boolean check=false;
String delimiters="\\s+|,\\s*|\\.\\s*";
String[] tokens=line.split(delimiters);
String str=new String();
for(int i=0;i<tokens.length;i++){
str=str+tokens[i];
}
char[] strArray=str.toCharArray();
if (strArray[0]!='v'&&strArray[0]!='V'&&strArray[0]!='h'&&strArray[0]!='H'&&strArray[0]!='s'&&strArray[0]!='S'&&strArray[0]!='r'&&strArray[0]!='R'){
System.out.println("ERROR: Unknown type found when parsing file");
return;
}
if (strArray[0]=='v'||strArray[0]=='V'){
if (tokens.length!=2){
System.out.println("ERROR: Type structure could not be parsed");
return;
}
if (tokens[1].compareTo("0")<0){
System.out.println("ERROR: Negative number found when parsing file");
return;
}
if (!Character.isDigit(strArray[1])) {
System.out.println("ERROR: Type structure could not be parsed");
return;
}
Line vl=new Line(false,Character.getNumericValue(strArray[1]));
processLine(vl);
}
if (strArray[0]=='h'||strArray[0]=='H'){
if (tokens[1].compareTo("0")<0){
System.out.println("ERROR: Negative number found when parsing file");
return;
}
if (!Character.isDigit(strArray[1])) {
System.out.println("ERROR: Type structure could not be parsed");
return;
}
if (tokens.length!=2){
System.out.println("ERROR: Type structure could not be parsed");
return;
}
Line hl=new Line(true,Character.getNumericValue(strArray[1]));
processLine(hl);
}
if (strArray[0]=='s'||strArray[0]=='S'){
if (tokens.length!=2){
System.out.println("ERROR: Type structure could not be parsed");
return;
}
if (tokens[1].compareTo("0")<0){
System.out.println("ERROR: Negative number found when parsing file");
return;
}
if (!Character.isDigit(strArray[1])) {
System.out.println("ERROR: Type structure could not be parsed");
return;
}
Rectangle sq=new Rectangle(true);
sq.setWidth(Character.getNumericValue(strArray[1]));
processRectangle(sq);
}
if (strArray[0]=='r'||strArray[0]=='R'){
if (tokens.length!=3){
System.out.println("ERROR: Type structure could not be parsed");
return;
}
if (tokens[1].compareTo("0")<0||tokens[2].compareTo("0")<0){
System.out.println("ERROR: Negative number found when parsing file");
return;
}
if (!Character.isDigit(strArray[1])||!Character.isDigit(strArray[2])) {
System.out.println("ERROR: Type structure could not be parsed");
return;
}
Rectangle rect=new Rectangle(false);
rect.setWidth(Character.getNumericValue(strArray[1]));
rect.setHeight(Character.getNumericValue(strArray[2]));
processRectangle(rect);
}
}
private static void drawToScreen(String input){
System.out.println(input);
}
private static void drawToFile(String input) {
try{
String txtname = promptAndReadFileName("output");
FileWriter fr = new FileWriter(txtname);
fr.write (input);
fr.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args){
char choice1;
int choice2;
Out:
while (true){
runMainMenu();
choice1=UserInput.readChar();
switch (choice1){
case 'h':
case 'H':
Line kevin1=createLine(true);
processLine(kevin1);
break;
case 'v':
case 'V':
Line kevin2=createLine(false);
processLine(kevin2);
break;
case 's':
case 'S':
Rectangle kevin3=createRectangle(true);
processRectangle(kevin3);
break;
case 'r':
case 'R':
Rectangle kevin4=createRectangle(false);
processRectangle(kevin4);
break;
case 'f':
case 'F':
createFromFile();
break;
case 'q':
case 'Q':
System.exit(0);
}// end of switch
}//end of while
}//end of main
}//end of class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -