OO第二次博客作业

前言:

        这段期间主要学习了正则表达式的使用,类设计。

        这段期间设计的题量较大,图形相关的类设计难度较大。

 

 

 

 

 

  1 import java.math.BigDecimal;
  2 import java.util.*;
  3 
  4 /**
  5  * @author LeGend
  6  */
  7 public class Main {
  8     static Scanner input = new Scanner(System.in);
  9 
 10     public static void main(String[] args) {
 11         Client client = new Client();
 12     }
 13 }
 14 
 15 class Client {
 16     private final HashMap<String, AutomatedTellerMachine> ATMs = new HashMap<>();
 17     private final ArrayList<User> users = new ArrayList<>();
 18 
 19     public Client() {
 20         ATMs.put("01", new AutomatedTellerMachine("01", CCB.getInstance()));
 21         ATMs.put("02", new AutomatedTellerMachine("02", CCB.getInstance()));
 22         ATMs.put("03", new AutomatedTellerMachine("03", CCB.getInstance()));
 23         ATMs.put("04", new AutomatedTellerMachine("04", CCB.getInstance()));
 24         ATMs.put("05", new AutomatedTellerMachine("05", ICBC.getInstance()));
 25         ATMs.put("06", new AutomatedTellerMachine("06", ICBC.getInstance()));
 26 
 27         users.add(new User("杨过"));
 28         users.get(0).addAccount(CCB.getInstance().createAccount(users.get(0).getName(), "3217000010041315709"));
 29         users.get(0).addCard(CCB.getInstance().createCard(users.get(0).getAccount(0),
 30                 "6217000010041315709", "88888888"));
 31         users.get(0).addCard(CCB.getInstance().createCard(users.get(0).getAccount(0),
 32                 "6217000010041315715", "88888888"));
 33         users.get(0).getAccount(0).deposit("10000.00");
 34         users.get(0).addAccount(CCB.getInstance().createAccount(users.get(0).getName(), "3217000010041315715"));
 35         users.get(0).addCard(CCB.getInstance().createCard(users.get(0).getAccount(1),
 36                 "6217000010041315718", "88888888"));
 37         users.get(0).getAccount(1).deposit("10000.00");
 38 
 39         users.add(new User("郭靖"));
 40         users.get(1).addAccount(CCB.getInstance().createAccount(users.get(1).getName(), "3217000010051320007"));
 41         users.get(1).addCard(CCB.getInstance().createCard(users.get(1).getAccount(0),
 42                 "6217000010051320007", "88888888"));
 43         users.get(1).getAccount(0).deposit("10000.00");
 44 
 45         users.add(new User("张无忌"));
 46         users.get(2).addAccount(ICBC.getInstance().createAccount(users.get(2).getName(), "3222081502001312389"));
 47         users.get(2).addCard(CCB.getInstance().createCard(users.get(2).getAccount(0),
 48                 "6222081502001312389", "88888888"));
 49         users.get(2).getAccount(0).deposit("10000.00");
 50         users.get(2).addAccount(ICBC.getInstance().createAccount(users.get(2).getName(), "3222081502001312390"));
 51         users.get(2).addCard(CCB.getInstance().createCard(users.get(2).getAccount(1),
 52                 "6222081502001312390", "88888888"));
 53         users.get(2).getAccount(1).deposit("10000.00");
 54         users.get(2).addAccount(ICBC.getInstance().createAccount(users.get(2).getName(), "3222081502001312399"));
 55         users.get(2).addCard(CCB.getInstance().createCard(users.get(2).getAccount(2),
 56                 "6222081502001312399", "88888888"));
 57         users.get(2).addCard(CCB.getInstance().createCard(users.get(2).getAccount(2),
 58                 "6222081502001312400", "88888888"));
 59         users.get(2).getAccount(2).deposit("10000.00");
 60 
 61         users.add(new User("韦小宝"));
 62         users.get(3).addAccount(ICBC.getInstance().createAccount(users.get(3).getName(), "3222081502051320785"));
 63         users.get(3).addCard(CCB.getInstance().createCard(users.get(2).getAccount(0),
 64                 "6222081502051320785", "88888888"));
 65         users.get(3).getAccount(0).deposit("10000.00");
 66         users.get(3).addAccount(ICBC.getInstance().createAccount(users.get(3).getName(), "3222081502051320786"));
 67         users.get(3).addCard(CCB.getInstance().createCard(users.get(3).getAccount(1),
 68                 "6222081502051320786", "88888888"));
 69         users.get(3).getAccount(1).deposit("10000.00");
 70 
 71 
 72         String str;
 73         do {
 74             str = Main.input.nextLine();
 75             if ("#".equals(str)) {
 76                 break;
 77             }
 78             String[] strings = str.split(" +");
 79 
 80             if (strings.length == 4) {
 81 
 82                 String num = strings[0];
 83                 String password = strings[1];
 84                 String atm = strings[2];
 85                 String amount = strings[3];
 86 
 87                 AutomatedTellerMachine automatedTellerMachine = ATMs.get(atm);
 88                 if (automatedTellerMachine == null) {
 89                     System.out.println("Sorry,the ATM's id is wrong.");
 90                     System.exit(0);
 91                 } else {
 92                     if (Double.parseDouble(amount) < 0) {
 93                         StringBuffer stringBuffer = new StringBuffer(amount);
 94                         stringBuffer.deleteCharAt(0);
 95                         automatedTellerMachine.deposit(num, password, stringBuffer.toString());
 96                     } else {
 97                         automatedTellerMachine.withdraw(num, password, amount);
 98                     }
 99                 }
100             } else {
101                 System.out.println("¥" + ChinaUnionPay.getInstance().getCard(strings[0]).getBankAccount().getBalance());
102             }
103         } while (!"#".equals(str));
104 
105     }
106 
107 
108 }
109 
110 class User {
111     private String name;
112     private final ArrayList<BankAccount> bankAccount = new ArrayList<>();
113     private final ArrayList<Card> cards = new ArrayList<>();
114 
115     public User(String name) {
116         this.name = name;
117     }
118 
119     public String getName() {
120         return name;
121     }
122 
123     public void setName(String name) {
124         this.name = name;
125     }
126 
127     public void addAccount(BankAccount bankAccount) {
128         this.bankAccount.add(bankAccount);
129     }
130 
131     public void addCard(Card card) {
132         this.cards.add(card);
133     }
134 
135     public Card getCard(int index) {
136         return cards.get(index);
137     }
138 
139     public BankAccount getAccount(int index) {
140         return bankAccount.get(index);
141     }
142 
143 }
144 
145 class ChinaUnionPay {
146     private static ChinaUnionPay chinaUnionPay = null;
147     private final HashMap<String, Card> cardNum = new HashMap<>();
148     //卡号 和 卡
149 
150     private ChinaUnionPay() {
151     }
152 
153     public static ChinaUnionPay getInstance() {
154         if (chinaUnionPay == null) {
155             chinaUnionPay = new ChinaUnionPay();
156         }
157         return chinaUnionPay;
158     }
159 
160     public void createCard(Card card) {
161         this.cardNum.put(card.getAccount(), card);
162     }
163 
164     public boolean verify(String account, String password) {
165         return this.cardNum.get(account).verify(password);
166     }
167 
168     public Card getCard(String cardNum) {
169         return this.cardNum.get(cardNum);
170     }
171 
172 }
173 
174 class Card {
175     private final String account;
176     private String password;
177     private final BankAccount bankAccount;
178     private final String bank;
179 
180     public Card(String account, String password, BankAccount bankAccount, String bank) {
181         this.account = account;
182         this.password = password;
183         this.bankAccount = bankAccount;
184         this.bank = bank;
185     }
186 
187     public String getBank() {
188         return bank;
189     }
190 
191     public boolean verify(String password) {
192         return password.equals(this.password);
193     }
194 
195     public void changePassword(String password) {
196         this.password = password;
197     }
198 
199     public String getAccount() {
200         return account;
201     }
202 
203     public BankAccount getBankAccount() {
204         return bankAccount;
205     }
206 
207 
208     @Override
209     public boolean equals(Object o) {
210         if (this == o) {
211             return true;
212         }
213         if (!(o instanceof Card)) {
214             return false;
215         }
216 
217         Card card = (Card) o;
218 
219         return getAccount() != null ? getAccount().equals(card.getAccount()) : card.getAccount() == null;
220     }
221 
222     @Override
223     public int hashCode() {
224         return getAccount() != null ? getAccount().hashCode() : 0;
225     }
226 }
227 
228 class AutomatedTellerMachine {
229     private final String number;
230     private final Bank bank;
231 
232     public AutomatedTellerMachine(String number, Bank bank) {
233         this.number = number;
234         this.bank = bank;
235     }
236 
237     public String getNumber() {
238         return number;
239     }
240 
241     public void deposit(String cardNumber, String password, String amount) {
242         Card card = ChinaUnionPay.getInstance().getCard(cardNumber);
243         if (card == null) {
244             System.out.println("Sorry,this card does not exist.");
245         } else {
246             deposit(card, password, amount);
247         }
248     }
249 
250     public void deposit(Card card, String password, String amount) {
251         if (!card.getBank().equals(this.bank.getBank())) {
252             System.out.println("Sorry,cross-bank withdrawal is not supported.");
253             System.exit(0);
254         }
255         if (!card.verify(password)) {
256             System.out.println("Sorry,your password is wrong.");
257             System.exit(0);
258         } else {
259             card.getBankAccount().deposit(amount);
260             System.out.println(card.getBankAccount().getName() + "在" + bank.getBank()
261                     + "的" + number + "号ATM机上存款¥" + amount + "\n" +
262                     "当前余额为¥" + card.getBankAccount().getBalance());
263 
264         }
265     }
266 
267     public void withdraw(String cardNumber, String password, String amount) {
268         Card card = ChinaUnionPay.getInstance().getCard(cardNumber);
269         if (card == null) {
270             System.out.println("Sorry,this card does not exist.");
271         } else {
272             withdraw(card, password, amount);
273         }
274     }
275 
276     public void withdraw(Card card, String password, String amount) {
277         if (!card.getBank().equals(this.bank.getBank())) {
278             System.out.println("Sorry,cross-bank withdrawal is not supported.");
279             System.exit(0);
280         }
281         if (!card.verify(password)) {
282             System.out.println("Sorry,your password is wrong.");
283             System.exit(0);
284         } else {
285             if (card.getBankAccount().withdraw(amount)) {
286                 System.out.println(card.getBankAccount().getName() + "在" + bank.getBank()
287                         + "的" + number + "号ATM机上取款¥" + amount + "\n" +
288                         "当前余额为¥" + card.getBankAccount().getBalance());
289             } else {
290                 System.out.println("Sorry,your account balance is insufficient.");
291 
292             }
293         }
294     }
295 
296     public void checkBalance(Card card, String password) {
297         if (!card.verify(password)) {
298             System.out.println("Sorry,your password is wrong.");
299         } else {
300             System.out.println("¥" + card.getBankAccount().getBalance());
301         }
302     }
303 
304 }
305 
306 abstract class Bank {
307     private final String bank;
308     private final HashMap<String, BankAccount> accountList;
309 
310     public Bank(String bank) {
311         this.bank = bank;
312         this.accountList = new HashMap<>();
313     }
314 
315     public String getBank() {
316         return bank;
317     }
318 
319     public BankAccount createAccount(String name, String account) {
320         if (accountList.containsKey(account)) {
321             return null;
322         } else {
323             BankAccount bankAccount = new BankAccount(name, bank, account);
324             accountList.put(account, bankAccount);
325             return bankAccount;
326         }
327     }
328 
329     //0为成功创建,1为卡号被占用
330     public Card createCard(BankAccount bankAccount, String cardAccount, String password) {
331         if (ChinaUnionPay.getInstance().getCard(cardAccount) == null) {
332             return bankAccount.createCard(cardAccount, password);
333         } else {
334             return null;
335         }
336 
337     }
338 
339 }
340 
341 class CCB extends Bank {
342     private static CCB ccb = null;
343 
344     private CCB() {
345         super("中国建设银行");
346     }
347 
348     public static CCB getInstance() {
349         if (ccb == null) {
350             ccb = new CCB();
351         }
352         return ccb;
353     }
354 
355 }
356 
357 class ICBC extends Bank {
358     private static ICBC icbc = null;
359 
360     private ICBC() {
361         super("中国工商银行");
362     }
363 
364     public static ICBC getInstance() {
365         if (icbc == null) {
366             icbc = new ICBC();
367         }
368         return icbc;
369     }
370 }
371 
372 class BankAccount {
373     private BigDecimal balance;
374     private final String name;
375     private final HashMap<String, Card> card = new HashMap<>();
376     //卡号和密码
377 
378     private final String account;
379     //银行账号
380 
381     private final String bank;
382     //开卡银行
383 
384     public BankAccount(String name, String bank, String account) {
385         this.name = name;
386         this.bank = bank;
387         this.account = account;
388         this.balance = new BigDecimal(0);
389     }
390 
391     public String getName() {
392         return name;
393     }
394 
395     public String getBank() {
396         return bank;
397     }
398 
399     public Card createCard(String cardNumber, String password) {
400         Card card = new Card(cardNumber, password, this, this.bank);
401         ChinaUnionPay.getInstance().createCard(card);
402         this.card.put(cardNumber, card);
403         return card;
404     }
405 
406     public String getBalance() {
407         return balance.setScale(2).toString();
408     }
409 
410     public void deposit(String num) {
411         balance = balance.add(new BigDecimal(num));
412     }
413 
414     public boolean withdraw(String num) {
415         BigDecimal temp = new BigDecimal(num);
416         temp = temp.abs();
417         if (temp.compareTo(balance) > 0) {
418             return false;
419         } else {
420             balance = balance.subtract(temp);
421             return true;
422         }
423     }
424 }

 

 

 

 

 

 

 

 

 

 

 ATM机的题目尽可能做好了类设计,尽可能降低了耦合性,为之后的扩展打好基础。

 

双向链表:

 

 

 

目前阶段,双向链表不支持迭代器,只能通过单向链表相同的通过循环的方式获取链表中存储的数据。

 

 

 

软件工程专业期中考试(OOP):

 

 

 

import java.util.Scanner;

/**
 * @author LeGend
 * @date 2022/4/26 19:31
 */
public class Main {
    public static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        double x1 = in.nextDouble(), y1 = in.nextDouble(), x2 = in.nextDouble(), y2 = in.nextDouble();
        String color = in.next();

        check(x1);
        check(y1);
        check(x2);
        check(y2);

        Line line = new Line(new Point(x1, y1), new Point(x2, y2), color);
        line.display();

    }

    public static void check(double a) {
        if (a <= 0 || a > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }

}

class Point {
    private double x, y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void display() {
        System.out.printf("(%s,%s)", String.format("%.2f", x), String.format("%.2f", y));
    }

}

class Line {
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getDistance() {
        return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
    }

    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        this.point1.display();
        System.out.println();
        System.out.println("The line's end point's Coordinate is:");
        this.point2.display();
        System.out.println();
        System.out.println("The line's length is:" + String.format("%.2f", this.getDistance()));
    }

}

 

 

 

 

 

这道题目属于比较简单的题目,主要体现面向对象原则中三大特性的封装性,通过getter 和 setter 访问类中的数据

 

 

 

 

import java.util.Scanner;

/**
 * @author LeGend
 * @date 2022/4/26 19:31
 */
public class Main {
    public static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        double x1 = in.nextDouble(), y1 = in.nextDouble(), x2 = in.nextDouble(), y2 = in.nextDouble();
        String color = in.next();

        check(x1);
        check(y1);
        check(x2);
        check(y2);

        Point point1 = new Point(x1, y1);
        Point point2 = new Point(x2, y2);

        Line line = new Line(point1, point2, color);
        Plane plane = new Plane(color);

        Element element;

        element = point1;//起点Point
        element.display();

        element = point2;//终点Point
        element.display();

        element = line;//线段
        element.display();

        element = plane;//
        element.display();
        

    }

    public static void check(double a) {
        if (a <= 0 || a > 200) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }

}

abstract class Element {
    public abstract void display();
}

class Point extends Element {
    private double x, y;

    public Point() {
    }

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    @Override
    public void display() {
        System.out.printf("(%s,%s)\n", String.format("%.2f", x), String.format("%.2f", y));
    }

}

class Line extends Element {
    private Point point1;
    private Point point2;
    private String color;

    public Line() {
    }

    public Line(Point point1, Point point2, String color) {
        this.point1 = point1;
        this.point2 = point2;
        this.color = color;
    }

    public Point getPoint1() {
        return point1;
    }

    public void setPoint1(Point point1) {
        this.point1 = point1;
    }

    public Point getPoint2() {
        return point2;
    }

    public void setPoint2(Point point2) {
        this.point2 = point2;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public double getDistance() {
        return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
    }

    @Override
    public void display() {
        System.out.println("The line's color is:" + this.color);
        System.out.println("The line's begin point's Coordinate is:");
        this.point1.display();

        System.out.println("The line's end point's Coordinate is:");
        this.point2.display();

        System.out.println("The line's length is:" + String.format("%.2f", this.getDistance()));
    }
}

class Plane extends Element {
    private String color;

    public Plane() {
    }

    public Plane(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public void display() {
        System.out.printf("The Plane's color is:%s", this.color);
    }
}

 

 

 

题目主要通过调用Line 与 Point共同的父类Element中的虚方法display来实现多态。

 

 

 

 

 

  1 import java.util.ArrayList;
  2 import java.util.Scanner;
  3 
  4 /**
  5  * @author LeGend
  6  * @date 2022/4/26 19:31
  7  */
  8 public class Main {
  9     public static Scanner in = new Scanner(System.in);
 10 
 11     public static void main(String[] args) {
 12         GeometryObject geometryObject = new GeometryObject();
 13 
 14         int choice = in.nextInt();
 15         while (choice != 0) {
 16             switch (choice) {
 17                 case 1://insert Point object into list 
 18                     double x = in.nextDouble(), y = in.nextDouble();
 19                     check(x);
 20                     check(y);
 21                     geometryObject.add(new Point(x, y));
 22                     break;
 23                 case 2://insert Line object into list
 24                     double x1 = in.nextDouble();
 25                     double y1 = in.nextDouble();
 26                     double x2 = in.nextDouble();
 27                     double y2 = in.nextDouble();
 28                     String color2 = in.next();
 29                     check(x1);
 30                     check(y1);
 31                     check(x2);
 32                     check(y2);
 33                     geometryObject.add(new Line(new Point(x1, y1), new Point(x2, y2), color2));
 34                     break;
 35                 case 3://insert Plane object into list
 36                     String color3 = in.next();
 37                     geometryObject.add(new Plane(color3));
 38                     break;
 39                 case 4://delete index - 1 object from list
 40                     int index = in.nextInt();
 41                     geometryObject.remove(index);
 42                     break;
 43                 default:
 44                     System.out.println("Wrong Format");
 45             }
 46             choice = in.nextInt();
 47         }
 48 
 49 
 50         for (Element element : geometryObject.getList()) {
 51             element.display();
 52         }
 53 
 54     }
 55 
 56     public static void check(double a) {
 57         if (a <= 0 || a > 200) {
 58             System.out.println("Wrong Format");
 59             System.exit(0);
 60         }
 61     }
 62 
 63 }
 64 
 65 abstract class Element {
 66     public abstract void display();
 67 }
 68 
 69 class Point extends Element {
 70     private double x, y;
 71 
 72     public Point() {
 73     }
 74 
 75     public Point(double x, double y) {
 76         this.x = x;
 77         this.y = y;
 78     }
 79 
 80     public double getX() {
 81         return x;
 82     }
 83 
 84     public void setX(double x) {
 85         this.x = x;
 86     }
 87 
 88     public double getY() {
 89         return y;
 90     }
 91 
 92     public void setY(double y) {
 93         this.y = y;
 94     }
 95 
 96     @Override
 97     public void display() {
 98         System.out.printf("(%s,%s)\n", String.format("%.2f", x), String.format("%.2f", y));
 99     }
100 
101 }
102 
103 class Line extends Element {
104     private Point point1;
105     private Point point2;
106     private String color;
107 
108     public Line() {
109     }
110 
111     public Line(Point point1, Point point2, String color) {
112         this.point1 = point1;
113         this.point2 = point2;
114         this.color = color;
115     }
116 
117     public Point getPoint1() {
118         return point1;
119     }
120 
121     public void setPoint1(Point point1) {
122         this.point1 = point1;
123     }
124 
125     public Point getPoint2() {
126         return point2;
127     }
128 
129     public void setPoint2(Point point2) {
130         this.point2 = point2;
131     }
132 
133     public String getColor() {
134         return color;
135     }
136 
137     public void setColor(String color) {
138         this.color = color;
139     }
140 
141     public double getDistance() {
142         return Math.sqrt(Math.pow(point1.getX() - point2.getX(), 2) + Math.pow(point1.getY() - point2.getY(), 2));
143     }
144 
145     @Override
146     public void display() {
147         System.out.println("The line's color is:" + this.color);
148         System.out.println("The line's begin point's Coordinate is:");
149         this.point1.display();
150 
151         System.out.println("The line's end point's Coordinate is:");
152         this.point2.display();
153 
154         System.out.println("The line's length is:" + String.format("%.2f", this.getDistance()));
155     }
156 }
157 
158 class Plane extends Element {
159     private String color;
160 
161     public Plane() {
162     }
163 
164     public Plane(String color) {
165         this.color = color;
166     }
167 
168     public String getColor() {
169         return color;
170     }
171 
172     public void setColor(String color) {
173         this.color = color;
174     }
175 
176     @Override
177     public void display() {
178         System.out.printf("The Plane's color is:%s\n", this.color);
179     }
180 }
181 
182 class GeometryObject {
183     private final ArrayList<Element> list = new ArrayList<>();
184 
185     public GeometryObject() {
186     }
187 
188     public void add(Element element) {
189         list.add(element);
190     }
191 
192     public void remove(int index) {
193         if (index <= list.size() && index > 0) {
194             list.remove(index - 1);
195         }
196     }
197 
198     public ArrayList<Element> getList() {
199         return list;
200     }
201 }

 

 

 

通过容器类存储数据并读取。

 

 

 

点线形题目

 

 

 

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

import static java.math.MathContext.DECIMAL128;
import static java.math.MathContext.UNLIMITED;

/**
 * @author LeGend
 */
public class Main {
    public static Scanner in = new Scanner(System.in);

    public static boolean cmp(Double a, double b) {
        return Math.abs(a - b) < 1e-6;
    }

    public static void main(String[] args) {
        String temp = in.nextLine();
        if (!temp.contains(":")) {
            System.out.println("Wrong Format");
            return;
        }
        String[] strings = temp.split("([:]| )");
        boolean first = true;
        for (String s : strings) {
            if ("".equals(s)) {
                System.out.println("Wrong Format");
                System.exit(0);
            }
        }
        final int[] check = {0, 4, 4, 4, 6, 5};
        int choice = strings[0].charAt(0) - '0';
        if (check[choice] != strings.length - 1) {
            System.out.print("wrong number of points");
            System.exit(0);
        }
        try {
            switch (Integer.parseInt(strings[0])) {
                case 1:
                    Rectangle rectangle1 = new Rectangle(Point.create(strings[1]), Point.create(strings[2]), Point.create(strings[3]), Point.create(strings[4]));
                    rectangle1.checkRepeat();
                    rectangle1.option1();
                    break;
                case 2:
                    Rectangle rectangle2 = new Rectangle(Point.create(strings[1]), Point.create(strings[2]), Point.create(strings[3]), Point.create(strings[4]));
                    rectangle2.checkRepeat();
                    rectangle2.option2();
                    break;
                case 3:
                    Rectangle rectangle3 = new Rectangle(Point.create(strings[1]), Point.create(strings[2]), Point.create(strings[3]), Point.create(strings[4]));
                    rectangle3.checkRepeat();
                    rectangle3.option3();
                    break;
                case 4:
                    Line line4 = new Line(Point.create(strings[1]), Point.create(strings[2]));
                    Rectangle rectangle4 = new Rectangle(Point.create(strings[3]), Point.create(strings[4]), Point.create(strings[5]), Point.create(strings[6]));
                    Shape shape4 = rectangle4.toShape();
                    shape4.option4(line4);
                    break;
                case 5:
                    Point point5 = Point.create(strings[1]);
                    Rectangle rectangle5 = new Rectangle(Point.create(strings[2]), Point.create(strings[3]), Point.create(strings[4]), Point.create(strings[5]));
                    Shape shape5 = rectangle5.toShape();
                    shape5.option5(point5);
                    break;
                default:
                    System.out.println("Wrong Format");
            }
        } catch (java.lang.NumberFormatException c) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
    }


    public static String round(double val) {
        BigDecimal bigDecimal = new BigDecimal(val);
        bigDecimal = bigDecimal.setScale(3, RoundingMode.HALF_UP);
        bigDecimal = bigDecimal.stripTrailingZeros();
        String ret = bigDecimal.toPlainString();
        if (!ret.contains(".")) {
            ret += ".0";
        }
        return ret;
    }
}

abstract class Shape {
    public abstract void option4(Line line);

    public abstract void option5(Point point);
}

class Point extends Shape {
    private double x, y;

    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Point(String s) {
        this.x = x;
        this.y = y;
    }

    public static boolean check(String str) {
        boolean symbol = str.charAt(0) < '0' || str.charAt(0) > '9';

        int temp = str.indexOf('.');
        if (temp == str.length() - 1) {
            System.out.println("Wrong Format");
            System.exit(0);
        }

        StringBuilder stringBuilder = new StringBuilder();

        if (!symbol) {
            stringBuilder.append("+");
        }

        stringBuilder.append(str);

        boolean floatNum = str.contains(".");
        if (floatNum) {//当前字为浮点数
            BigDecimal num = new BigDecimal(str);
            StringBuilder numStr = new StringBuilder();
            if (num.compareTo(BigDecimal.valueOf(0)) > 0) {
                numStr.append("+");
            }

            numStr.append(num);
            if (stringBuilder.toString().equals(numStr.toString())) {
                return true;
            } else {
                return Double.parseDouble(str) == (int) Double.parseDouble(str);
            }

        } else {
            int num = Integer.parseInt(stringBuilder.toString());
            StringBuilder numStr = new StringBuilder();
            if (num >= 0) {
                numStr.append("+");
            }
            numStr.append(num);
            return stringBuilder.toString().equals(numStr.toString());

        }
    }

    public static Point create(String x) {
        String[] tmp = x.split(",");
        try {
            if (tmp.length != 2 || (!(check(tmp[0]) && check(tmp[1])))) {
                System.out.println("Wrong Format");
                System.exit(0);
            }

            return new Point(Double.parseDouble(tmp[0]), Double.parseDouble(tmp[1]));
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException c) {
            System.out.println("Wrong Format");
            System.exit(0);
        }
        return null;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Point point = (Point) o;

        if (Double.compare(point.getX(), getX()) != 0) {
            return false;
        }
        return Double.compare(point.getY(), getY()) == 0;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        temp = Double.doubleToLongBits(getX());
        result = (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(getY());
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    public double distance(Point point) {
        return Math.sqrt(Math.pow(this.x - point.x, 2) + Math.pow(this.y - point.y, 2));
    }


    @Override
    public void option4(Line line) {

    }

    @Override
    public void option5(Point point) {

    }
}

class Line {
    private Point point0;
    private Point point1;
    private double k;
    private double c;
    private double length;

    public Line() {
    }

    public Line(Point point0, Point point1) {
        if (point0.equals(point1)) {
            System.out.println("points coincide");
            System.exit(0);
        }
        this.point0 = point0;
        this.point1 = point1;
        k = (point0.getY() - point1.getY()) / (point0.getX() - point1.getX());
        c = point0.getY() - k * point0.getX();
        length = point0.distance(point1);
    }

    public Point getPoint0() {
        return point0;
    }

    public Point getPoint1() {
        return point1;
    }

    public double getK() {
        return k;
    }

    public double getLength() {
        return length;
    }

    public boolean isParallel(Line line) {
        if ((Double.isInfinite(this.k) || Double.isNaN(this.k)) && (Double.isInfinite(line.k) || Double.isNaN(this.k))) {
            return true;
        }
        return Math.abs(line.k - this.k) < 1e-6;
    }

    public boolean vertical(Line line) {
        if ((Double.isInfinite(this.k) && Main.cmp(line.k, 0)) || (Double.isInfinite(line.k) && Main.cmp(this.k, 0))) {
            return true;
        }
        return Math.abs(this.k * line.k + 1) < 1e-6;
    }

    public boolean inLine(Point point) {
        if (point == null) {
            return false;
        }
        if (point.equals(point1) || point.equals(point0)) {
            return false;
        }
        double distance0 = point0.distance(point1);
        double distance1 = point.distance(point0) + point.distance(point1);
        return Math.abs(distance0 - distance1) < 1e-6;
    }

    public boolean isOnLine(Point point) {
        if (Double.isInfinite(k) || Double.isNaN(k)) {
            if (Main.cmp(point.getX(), this.point0.getX()) && (point.getY() < Math.max(point0.getY(), point1.getY()) && point.getY() > Math.min(point0.getY(), point1.getY()))) {
                return true;
            }
        }
        return Main.cmp(point.getY(), point.getX() * k + c);
    }


    public Point intersection(Line line) {
        if (this.isParallel(line)) {
            return null;
        }
        if (Double.isInfinite(line.k) || Double.isNaN(line.k)) {
            return new Point((point0.getY() - c) / this.k, line.point0.getY());
        } else if (Double.isInfinite(this.k) || Double.isNaN(this.k)) {
            return new Point((point0.getY() - c) / this.k, this.point0.getY());
        } else {
            double x = -(this.c - line.c) / (this.k - line.k);
            return new Point(x, this.k * x - this.c);
        }
    }

    public double distance(Point point) {
        if (Double.isInfinite(k)) {
            return Math.abs(point.getX() - point1.getX());
        } else {
            return Math.abs(k * point.getX() - point.getY() + c) / Math.sqrt(k * k + 1);
        }
    }

    @Override
    public String toString() {
        if (Double.isInfinite(k) || Double.isNaN(k)) {
            return "Slope does not exist";
        } else {
            return String.format("%.1f", k);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Line line = (Line) o;

        if ((Double.isInfinite(this.k) || Double.isNaN(this.k)) && (Double.isInfinite(line.k) || Double.isNaN(line.k))) {
            return Main.cmp(this.getPoint0().getX(), line.getPoint0().getX());
        }

        if (!Main.cmp(this.k, line.k)) {
            return false;
        }
        return Main.cmp(line.c, this.c);
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        temp = Double.doubleToLongBits(getK());
        result = (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(c);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}

class Rectangle extends Shape {
    private final Point[] points;
    private final Line[] lines;

    public Rectangle(Point point0, Point point1, Point point2, Point point3) {
        this.points = new Point[]{point0, point1, point2, point3};
        this.lines = new Line[]{new Line(point0, point1), new Line(point1, point2), new Line(point2, point3), new Line(point3, point0)};
    }

    private boolean isRectangle() {
        if (!lines[0].equals(lines[1])) {
            if (!lines[0].equals(lines[2])) {
                if (!lines[0].equals(lines[3])) {
                    if (!lines[1].equals(lines[2])) {
                        if (!lines[1].equals(lines[3])) {
                            if (!lines[2].equals(lines[3])) {
                                Point point0 = lines[0].intersection(lines[2]);
                                Point point1 = lines[3].intersection(lines[1]);


                                return !lines[1].inLine(point0) && !lines[2].inLine(point1) && !lines[3].inLine(point0) && !lines[0].inLine(point1);
                            }
                        }
                    }
                }
            }
        }
        return false;


    }

    public void checkRepeat() {
        if (!points[0].equals(points[1])) {
            if (!points[0].equals(points[2])) {
                if (!points[0].equals(points[3])) {
                    if (!points[1].equals(points[2])) {
                        if (!points[1].equals(points[3])) {
                            if (!points[2].equals(points[3])) {
                                return;
                            }
                        }
                    }
                }
            }
        }
        System.out.println("points coincide");
        System.exit(0);

    }

    private boolean isParallelogram() {
        return lines[0].isParallel(lines[2]) && lines[1].isParallel(lines[3]);
    }

    private boolean isDiamond() {
        return Main.cmp(lines[0].getLength(), lines[1].getLength()) && Main.cmp(lines[1].getLength(), lines[2].getLength()) && Main.cmp(lines[2].getLength(), lines[3].getLength());
    }

    private boolean isRec() {
        return lines[0].vertical(lines[1]) && lines[1].vertical(lines[2]) && lines[2].vertical(lines[3]) && lines[3].vertical(lines[0]);
    }


    private boolean isSquare() {
        return isDiamond() && isRec();
    }

    private boolean bump() {
        double t1 = (points[3].getX() - points[0].getX()) * (points[1].getY() - points[0].getY()) - (points[3].getY() - points[0].getY()) * (points[1].getX() - points[0].getX());
        double t2 = (points[0].getX() - points[1].getX()) * (points[2].getY() - points[1].getY()) - (points[0].getY() - points[1].getY()) * (points[2].getX() - points[1].getX());
        double t3 = (points[1].getX() - points[2].getX()) * (points[3].getY() - points[2].getY()) - (points[1].getY() - points[2].getY()) * (points[3].getX() - points[2].getX());
        double t4 = (points[2].getX() - points[3].getX()) * (points[0].getY() - points[3].getY()) - (points[2].getY() - points[3].getY()) * (points[0].getX() - points[3].getX());

        return t1 * t2 * t3 * t4 > 0;

    }

    public double perimeter() {
        return lines[0].getLength() + lines[1].getLength() + lines[2].getLength() + lines[3].getLength();
    }

    public double getArea() {
        Vector vector0 = new Vector(points[0], points[1]);
        Vector vector1 = new Vector(points[0], points[3]);

        double angle = vector0.getAngle(vector1);
        double radians = Math.toRadians(angle);

        double a = lines[0].getLength();
        double b = lines[1].getLength();
        double c = lines[2].getLength();
        double d = lines[3].getLength();

        Line line0 = new Line(points[0], points[2]);
        Line line1 = new Line(points[1], points[3]);

        double e = line0.getLength();
        double f = line1.getLength();


        double p = (a + b + c + d) / 2;

        return Math.sqrt(4 * e * e * f * f - Math.pow(a * a - b * b + c * c - d * d, 2)) / 4;

    }

    public void option1() {
        System.out.println(this.isRectangle() + " " + this.isParallelogram());
    }


    public void option2() {
        if (!isRectangle()) {
            System.out.println("not a quadrilateral");
        } else {
            System.out.println(isDiamond() + " " + isRec() + " " + isSquare());
        }
    }

    public void option3() {
        if (!this.isRectangle()) {
            System.out.println("not a quadrilateral");
        } else {
            System.out.println(bump() + " " + Main.round(this.perimeter()) + " " + Main.round(this.getArea()));
        }
    }

    public Shape toShape() {
        if (lines[0].equals(lines[1])) {
            return new Triangle(points[0], points[2], points[3]);
        } else if (lines[1].equals(lines[2])) {
            return new Triangle(points[0], points[1], points[3]);
        } else if (lines[2].equals(lines[3])) {
            return new Triangle(points[0], points[1], points[2]);
        } else if (lines[3].equals(lines[0])) {
            return new Triangle(points[1], points[2], points[3]);
        } else {
            return this;
        }
    }

    @Override
    public void option4(Line line) {
        for (Line l : lines) {
            if (line.equals(l)) {
                System.out.println("The line is coincide with one of the lines");
                return;
            }
        }

        for (Point p : points) {
            if (line.isOnLine(p)) {
                System.out.println("1");
                return;
            }
        }
        System.out.print("2 ");


    }

    @Override
    public void option5(Point point) {
        if (!this.isRectangle()) {
            System.out.println("not a quadrilateral or triangle");
            System.exit(0);
        }
        if (lines[0].inLine(point) || lines[1].inLine(point) || lines[2].inLine(point) || lines[3].inLine(point)) {
            System.out.println("on the quadrilateral");
            return;
        }
        double degree = 0;
        Vector vector0 = new Vector(point, points[0]);
        Vector vector1 = new Vector(point, points[1]);
        Vector vector2 = new Vector(point, points[2]);
        Vector vector3 = new Vector(point, points[3]);


        degree += vector0.getAngle(vector1);
        degree += vector1.getAngle(vector2);
        degree += vector2.getAngle(vector3);
        degree += vector3.getAngle(vector0);

        if (Main.cmp(degree, 360)) {
            System.out.println("in the quadrilateral");
        } else {
            System.out.println("outof the quadrilateral");
        }
    }
}

class Triangle extends Shape {
    private final Point[] points;
    private final Line[] lines;

    public Triangle(Point point0, Point point1, Point point2) {
        this.points = new Point[]{point0, point1, point2};
        this.lines = new Line[]{new Line(point0, point1), new Line(point1, point2), new Line(point2, point0)};
    }

    public boolean check() {
        if (lines[0].isParallel(lines[1]) || lines[0].isParallel(lines[2]) || lines[1].isParallel(lines[2])) {
            return false;
        }
        return !points[0].equals(points[1]) && !points[0].equals(points[2]) && !points[1].equals(points[2]);
    }

    public double getArea() {
        double p = lines[0].getLength() + lines[1].getLength() + lines[2].getLength();
        return Math.sqrt(p * (p - lines[0].getLength()) * (p - lines[1].getLength()) * (p - lines[2].getLength()));
    }

    @Override
    public void option4(Line line) {
        for (Line l : lines) {
            if (line.equals(l)) {
                System.out.println("The line is coincide with one of the lines");
                return;
            }
        }

        for (Point p : points) {
            if (line.isOnLine(p)) {
                System.out.println("1");
                return;
            }
        }

        System.out.print("2 ");

        double distance0 = line.distance(this.points[0]);
        double distance1 = line.distance(this.points[1]);
        double distance2 = line.distance(this.points[2]);

//        if(distance2 > distance1 ||)


    }

    @Override
    public void option5(Point point) {
//        if (this.check()) {
//            System.out.println("not a quadrilateral or triangle");
//            return;
//        }
        if (lines[0].inLine(point) || lines[1].inLine(point) || lines[2].inLine(point)) {
            System.out.println("on the triangle");
            return;
        }
        double degree = 0;
        Vector vector0 = new Vector(point, points[0]);
        Vector vector1 = new Vector(point, points[1]);
        Vector vector2 = new Vector(point, points[2]);


        degree += vector0.getAngle(vector1);
        degree += vector1.getAngle(vector2);
        degree += vector2.getAngle(vector0);


        if (Main.cmp(degree, 360)) {
            System.out.println("in the triangle");
        } else {
            System.out.println("outof the triangle");
        }
    }
}

class Vector {
    private double x;
    private double y;

    public Vector() {
    }

    public Vector(Point start, Point end) {
        this.x = end.getX() - start.getX();
        this.y = end.getY() - start.getY();
    }


    public double getLength() {
        return Math.sqrt(x * x + y * y);
    }

    public double getAngle(Vector vector) {
        return Math.toDegrees(Math.acos((vector.x * this.x + vector.y * this.y) / vector.getLength() / this.getLength()));
    }

}

 

 

 

posted @ 2022-05-01 18:32  LeGend_wLw  阅读(20)  评论(0编辑  收藏  举报