Java练习项目
作者:互联网
目录
练习项目
1.点菜系统
-
界面:
代码:
public class OrderName {
int id;//编号
String orderName;//菜名
Double orderPrice;//价格
public OrderName(int id, String orderName, Double orderPrice) {
this.id = id;
this.orderName = orderName;
this.orderPrice = orderPrice;
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class OrderMain{
//菜单
static List<OrderName> list = new ArrayList<>();
//已点菜品
static List<OrderName> list1 = new ArrayList<>();
//钱
static double yuan=0;
public static void main(String[] args) {
initOrder();
outer:while (true){
mainMenu();
Scanner scanner = new Scanner(System.in);
int serialNumber = scanner.nextInt();
switch (serialNumber){
case 0:
//主菜单
mainMenu();
break;
case 1:
//菜单
menu();
while (true){
int serialNumber1 = scanner.nextInt();
if (serialNumber1==0){
continue outer;
}
OrderName orderName = list.get(serialNumber1 - 1);
System.out.println("您已点:"+orderName.orderName);
list1.add(orderName);
}
case 2:
//已点菜品
haveSome();
int serialNumber2 = scanner.nextInt();
if (serialNumber2==0){
continue outer;
}
case 3:
//买单
pay();
return;
}
scanner.close();
}
}
//菜品
public static void initOrder(){
OrderName orderName1 = new OrderName(1, "鱼香肉丝", 16d);
OrderName orderName2 = new OrderName(2, "宫保鸡丁", 16d);
OrderName orderName3 = new OrderName(3, "口水鸡", 30d);
OrderName orderName4 = new OrderName(4, "地三鲜", 28d);
OrderName orderName5 = new OrderName(5, "炖王八", 59d);
list.add(orderName1);
list.add(orderName2);
list.add(orderName3);
list.add(orderName4);
list.add(orderName5);
}
//主菜单
public static void mainMenu(){
System.out.println("----主菜单----");
System.out.println("菜单\t\t\t1");
System.out.println("已点菜品\t\t2");
System.out.println("买单\t\t\t3");
System.out.println("----按0返回主菜单----");
}
//菜单
public static void menu(){
System.out.println("----请您点菜----");
for (int i = 0; i < list.size(); i++) {
OrderName order = list.get(i);
System.out.println(order.id+"\t"+order.orderName+"\t"+ order.orderPrice);
}
System.out.println("----序列号点菜,按0返回主菜单----");
}
//已点菜品
public static void haveSome(){
System.out.println("----您已点----");
for (int i = 0; i < list1.size(); i++) {
OrderName order = list1.get(i);
System.out.println(order.id+"\t"+order.orderName+"\t"+ order.orderPrice);
}
System.out.println("----按0返回主菜单----");
}
//买单
public static void pay(){
System.out.println("----正在结账----");
for (int i = 0; i < list1.size(); i++) {
OrderName order = list1.get(i);
yuan+=order.orderPrice;
}
System.out.println("共消费了:"+yuan+"元");
}
}
2.疫情健康管理系统
界面:
public class Message {
private String name;//姓名
private String sex;//性别
private int age;//年龄
private long idCard;//身份证
private String time;//时间
public Message() {
}
public Message(String name, String sex, int age, long idCard, String time) {
this.name = name;
this.sex = sex;
this.age = age;
this.idCard = idCard;
this.time = time;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getIdCard() {
return idCard;
}
public void setIdCard(long idCard) {
this.idCard = idCard;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
public class State extends Message{
private String healthState;//健康码状态
private String tripState;//行程码状态
private String region;//途径地区
public State() {
}
public State(String healthState, String tripState, String region) {
this.healthState = healthState;
this.tripState = tripState;
this.region = region;
}
public String getHealthState() {
return healthState;
}
public void setHealthState(String healthState) {
this.healthState = healthState;
}
public String getTripState() {
return tripState;
}
public void setTripState(String tripState) {
this.tripState = tripState;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
}
import java.text.DateFormat;
import java.util.Date;
import java.util.Scanner;
public class EpidemicSystem {
private State[] States;
boolean isExit;
public EpidemicSystem() {
}
public EpidemicSystem(State[] states) {
States = states;
}
public State[] getStates() {
return States;
}
public void setStates(State[] states) {
States = states;
}
Scanner scanner=new Scanner(System.in);
//菜单
public void menu(){
System.out.println("欢迎使用疫情健康管理系统");
while (true){
System.out.print("1.来访登记\t");
System.out.print("2.修改健康码\t");
System.out.print("3.修改行程码\t");
System.out.print("4.删除来访记录\t");
System.out.print("5.查看来访信息\t");
System.out.println("0.退出");
int choose = scanner.nextInt();
isExit=false;
switch (choose){
case 1:{
add();
break;
}
case 2:{
updateHealth();
break;
}
case 3:{
updateTrip();
break;
}
case 4:{
delete();
break;
}
case 5:{
check();
break;
}
case 0:{
isExit=true;
scanner.close();
return;
}
default:{
System.out.println("输入错误");
}
}
}
}
//增加
public void add(){
for (int i = 0; i < States.length; i++) {
if (States[i]==null){
State state = new State();
System.out.println("请输入访客姓名:");
state.setName(scanner.next());
System.out.println("请输入访客性别:");
state.setSex(scanner.next());
System.out.println("请输入访客年龄:");
state.setAge(scanner.nextInt());
System.out.println("请输入访客身份证号:");
state.setIdCard(scanner.nextLong());
// System.out.println("请输入访客来访时间(yyyy-MM-dd HH:mm:ss格式):");
state.setTime(parseStringToDate());
System.out.println("请输入健康码状态:");
state.setHealthState(scanner.next());
System.out.println("请输入行程码状态:");
state.setTripState(scanner.next());
System.out.println("请输入途径地区(多个地区以,分隔):");
state.setRegion(scanner.next());
States[i]=state;
return;
}
}
}
//修改健康码
public void updateHealth(){
System.out.println("请输入要修改的访客身份证号:");
long idCard=printLong();
for (int i = 0; i < States.length; i++) {
if (States[i]!=null){
if (States[i].getIdCard()==idCard){
System.out.println("请输入健康码状态:");
States[i].setHealthState(printString());
System.out.println("修改健康码成功");
return;
}
}
}
System.out.println("没有找到"+idCard+"身份证号");
}
//修改行程码
public void updateTrip(){
System.out.println("请输入要修改的访客身份证号:");
long idCard=printLong();
for (int i = 0; i < States.length; i++) {
if (States[i]!=null){
if (States[i].getIdCard()==idCard){
System.out.println("请输入行程码状态:");
States[i].setTripState(printString());
System.out.println("请输入途径地区(多个地区以,分隔):");
States[i].setRegion(printString());
System.out.println("修改行程码成功");
return;
}
}
}
System.out.println("没有找到"+idCard+"身份证号");
}
//删除来访记录
public void delete(){
System.out.println("请输入要删除的访客身份证号:");
long idCard=printLong();
for (int i = 0; i < States.length; i++) {
if (States[i]!=null){
if (States[i].getIdCard()==idCard){
States[i]=null;
System.out.println("删除成功");
return;
}
}
}
System.out.println("没有找到"+idCard+"身份证号");
}
//查看来访信息
public void check(){
System.out.println("序号\t姓名\t性别\t年龄\t身份证号\t\t\t\t\t来访时间\t\t\t\t健康码\t行程码");
for (int i = 0; i < States.length; i++) {
if (States[i]!=null){
System.out.print((i+1)+"\t");
System.out.print(States[i].getName()+"\t");
System.out.print(States[i].getSex()+"\t");
System.out.print(States[i].getAge()+"\t");
System.out.print(States[i].getIdCard()+"\t\t");
System.out.print(States[i].getTime()+"\t");
System.out.print(States[i].getHealthState()+"\t\t");
System.out.print(States[i].getTripState());
System.out.println("(途经"+States[i].getRegion()+")");
}
}
}
//获取当前时间
public String parseStringToDate() {
Date date = new Date();
DateFormat formatter = DateFormat.getDateTimeInstance();
String str = formatter.format(date);
return str;
}
//输入方法
public String printString(){
String next = scanner.next();
return next;
}
public long printLong(){
long next = scanner.nextLong();
return next;
}
}
public class Demo {
public static void main(String[] args) {
EpidemicSystem epidemicSystem = new EpidemicSystem();
epidemicSystem.setStates(new State[100]);
epidemicSystem.menu();
}
}
标签:Java,String,项目,练习,System,States,println,public,out 来源: https://www.cnblogs.com/yingxu/p/16270466.html